<template>
  <div class="voice">
    <div class="top-set">
      <div class="t-left">
        自动接单
      </div>
      <div class="t-right">
        <el-switch @change="updateConfig" v-model="openAutoorder" :active-value="1" :inactive-value="0" />
      </div>
    </div>
    <div class="top-set">
      <div class="t-left">
        自动配送(暂不支持预约单)
        <span @click="open">[时长设置]</span>
      </div>
      <div class="t-right">
        <span>默认下单{{autodeliveryOrderTime}}分钟后</span>
        <el-switch @change="autodelivery" v-model="openAutodelivery" :active-value="1" :inactive-value="0" />
      </div>
    </div>
    <div class="top-set">
      <div class="t-left">
        自动打印
        <span>(开启后将为您自动打印小票)</span>
      </div>
      <div class="t-right">
        <el-switch @change="updateConfig" v-model="openPrintSwitch" :active-value="1" :inactive-value="0" />
      </div>
    </div>
    <div class="voice-content">
      <div class="top-set" style="border-bottom: 1px solid #eee;">
        <div class="t-left">
          语音播报
        </div>
        <div class="t-right">
          <span>一键开启</span>
          <el-switch @change="changeMessageStatus" v-model="msasterSwitch" :active-value="1" :inactive-value="0" />
        </div>
      </div>
      <div class="voice-list">
        <el-row>
          <el-col :span="8" v-for="(item, index) in voiceList" :key="index">
            <div class="item">
              <div class="left">
                <img src="../../../static/image/voice-icon.png" class="voice-icon" />
                <span>{{ item.name }}</span>
              </div>
              <div class="right">
                <el-switch @change="updateSavePushMessageStatus" v-model="item.msasterSwitch" :active-value="1" :inactive-value="0" />
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- // 选择自动配送运力 -->
    <el-dialog title="自动配送设置" :visible.sync="centerDialogVisible" width="800px" center>
      <el-input placeholder="请输入自动配送时长" v-model="input" clearable>
      </el-input>
      <div class="delivery-list">
        <div class="delivery" v-for="(v,i) in deliveryList" :key="i">{{v.name}}</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mideum" @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getConfig,
  updateConfig,
  queryPushMessageStatus,
  updateSavePushMessageStatus,
} from "../../api/setting.js";
import { floorListDelivery } from '../../api/shop.js'
export default {
  data() {
    return {
      voiceList: [],
      openAutodelivery: 0,
      openAutoorder: 0,
      openPrintSwitch: 0,
      input: 300,
      autodeliveryOrderTime: 0,
      msasterSwitch: 0,
      centerDialogVisible: false,
      autodeliveryIds: [],
      deliveryList: [], // 运力列表
    };
  },
  created() {
    this.getConfig();
    this.queryPushMessageStatus();
    this.floorListDelivery()
  },
  methods: {
    floorListDelivery(){
      floorListDelivery().then(res=>{
        if(res.code === 200){
          this.deliveryList = res.data
        }else{
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    changeMessageStatus() {
      this.voiceList = this.voiceList.map((v) => {
        v.msasterSwitch = this.msasterSwitch;
        return v;
      });
      this.updateSavePushMessageStatus();
    },
    getConfig() {
      getConfig().then((res) => {
        if (res.code === 200) {
          this.autodeliveryOrderTime = res.data.autodeliveryOrderTime / 60;
          this.openAutodelivery = res.data.openAutodelivery;
          this.openAutoorder = res.data.openAutoorder;
          this.openPrintSwitch = res.data.openPrintSwitch;
          this.autodeliveryIds = res.data.autodeliveryIds.split(',')
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    queryPushMessageStatus() {
      queryPushMessageStatus().then((res) => {
        if (res.code === 200) {
          this.voiceList = res.data;
          let msasterSwitchList = res.data.map((v) => {
            return v.msasterSwitch;
          });
          let msasterSwitch = [...new Set(msasterSwitchList)];
          this.msasterSwitch =
            msasterSwitch.length === 1 && msasterSwitch[0] === 1 ? 1 : 0;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    updateSavePushMessageStatus() {
      updateSavePushMessageStatus({ orderPushMessages: this.voiceList }).then(
        (res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "修改成功！",
            });
            this.queryPushMessageStatus();
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        }
      );
    },
    autodelivery() {
      if (this.openAutodelivery === 1) {
        this.centerDialogVisible = true;
      }
    },
    updateConfig() {
      let params = {};
      if (time) {
        params = {
          autodeliveryOrderTime: time * 60,
          openAutodelivery: this.openAutodelivery,
          openAutoorder: this.openAutoorder,
          openPrintSwitch: this.openPrintSwitch,
        };
      } else {
        params = {
          openAutodelivery: this.openAutodelivery,
          openAutoorder: this.openAutoorder,
          openPrintSwitch: this.openPrintSwitch,
        };
      }
      updateConfig(params).then((res) => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "修改成功！",
          });
          this.getConfig();
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    open() {
      this.$prompt("请设置时长（分钟）", "开启自动配送", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValue: this.autodeliveryOrderTime,
      })
        .then(({ value }) => {
          this.updateConfig(value);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.voice {
  .top-set {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    background-color: #fff;
    .t-left {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 600;
      color: #333333;
      span {
        cursor: pointer;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #175199;
      }
    }
    .t-right {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #b1b1b1;
    }
  }
  .voice-content {
    margin-top: 10px;
    .voice-list {
      background-color: #fff;
      .item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 17px 22px 19px 18px;
        .left {
          display: flex;
          align-items: center;
          .voice-icon {
            width: 15px;
            height: 12px;
            margin-right: 4px;
            margin-top: 2px;
          }
          span {
            font-size: 14px;
            font-family: PingFang SC;
            font-weight: 400;
            color: #333333;
          }
        }
      }
    }
  }
  .delivery-list {
    display: flex;
    flex-wrap: wrap;
    margin-top: 20px;
    .delivery {
      margin: 0 20px 20px 0;
      width: 100px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 25px;
      border: 1px solid #999;
      color: #999999;
      cursor: pointer;
    }
  }
}
</style>
