<template>
  <div class="voice">
    <template v-if="memberType !== 1">
      <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" active-color="#FC7200" inactive-color="#999" />
        </div>
      </div>
      <div class="top-set">
        <div class="t-left">
          自动配送(暂不支持预约单)
          <span @click="centerDialogVisible = true">[时长/运力设置]</span>
        </div>
        <div class="t-right">
          <span>默认下单{{autodeliveryOrderTime}}分钟后</span>
          <el-switch @change="autodelivery" v-model="openAutodelivery" :active-value="1" :inactive-value="0" active-color="#FC7200" inactive-color="#999" />
        </div>
      </div>
      <div class="top-set">
        <div class="t-left">
          自动打印
          <span style="color: #999;">(开启后将为您自动打印小票)</span>
        </div>
        <div class="t-right">
          <el-switch @change="updateConfig" v-model="openPrintSwitch" :active-value="1" :inactive-value="0" active-color="#FC7200" inactive-color="#999" />
        </div>
      </div>
    </template>
    <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"  active-color="#FC7200" inactive-color="#999" />
        </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 @click="play(index)" 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" active-color="#FC7200" inactive-color="#999" />
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- // 选择自动配送运力 -->
    <el-dialog title="自动配送设置" @close="getConfig" :visible.sync="centerDialogVisible" width="800px" center>
      <el-form label-width="120px">
        <el-form-item label="自动接单时间">
          <el-input size="small" class="auto-time" v-model="autodeliveryOrderTime">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="自动配送运力">
          <el-checkbox-group v-model="autodeliveryIds">
            <el-checkbox :label="String(v.deliveryId)" name="type" v-for="(v,i) in deliveryList" :key="i">{{v.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="getConfig">取 消</el-button>
        <el-button size='medium' type="primary" @click="confirm">确 定</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,
      autodeliveryOrderTime: 0,
      msasterSwitch: 1,
      centerDialogVisible: false,
      autodeliveryIds: [],
      deliveryList: [], // 运力列表
      deliveryIdList: [],
      memberType: this.$store.state.userInfo.memberType,
    };
  },
  watch:{
    "$store.state.userInfo": {
      handler(newVal, oldVal) {
        this.memberType = newVal.memberType;
      },
      deep: true,
    },
  },
  created() {
    this.getConfig();
    this.queryPushMessageStatus();
    this.floorListDelivery();
  },
  methods: {
    play(index) {
      let src = "https://pc.liebaoai.cn/audio/alone.mp3";
      switch (index) {
        case 0:
          src = "https://pc.liebaoai.cn/audio/new-order.mp3";
          break;
        case 1:
          src = "https://pc.liebaoai.cn/audio/order-received.mp3";
          break;
        case 2:
          src = "https://pc.liebaoai.cn/audio/missed-orders.mp3";
          break;
        case 3:
          src = "https://pc.liebaoai.cn/audio/rider-cancel.mp3";
          break;
        case 4:
          src = "https://pc.liebaoai.cn/audio/user-go-cancel.mp3";
          break;
        case 5:
          src = "https://pc.liebaoai.cn/audio/not-enough.mp3";
          break;
        case 6:
          src = "https://pc.liebaoai.cn/audio/lower-than.mp3";
          break;
        case 7:
          src = "https://pc.liebaoai.cn/audio/over-time.mp3";
          break;
        case 8:
          src = "https://pc.liebaoai.cn/audio/rider-go-cancel.mp3";
          break;

        default:
          break;
      }
      let mp3 = new Audio(src);
      mp3.play(); //播放 mp3这个音频对象
    },
    confirm() {
      if (!this.autodeliveryIds.length) {
        return this.$message({
          type: "error",
          message: "开启自动配送必须选择至少一个运力平台！",
        });
      }
      this.centerDialogVisible = true;
      this.updateConfig();
    },
    floorListDelivery() {
      floorListDelivery().then((res) => {
        if (res.code === 200) {
          this.deliveryList = res.data;
          this.deliveryIdList = this.deliveryList.map((v) => {
            return String(v.deliveryId);
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    changeMessageStatus() {
      this.voiceList = this.voiceList.map((v) => {
        v.msasterSwitch = this.msasterSwitch;
        return v;
      });
      this.updateSavePushMessageStatus();
    },
    getConfig() {
      this.centerDialogVisible = false;
      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
            ? res.data.autodeliveryIds.split(",")
            : [];
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    queryPushMessageStatus() {
      queryPushMessageStatus().then((res) => {
        if (res.code === 200) {
          this.voiceList = res.data;
        } 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;
        this.autodeliveryIds = this.deliveryIdList
      } else {
        this.autodeliveryIds = "";
        this.autodeliveryOrderTime = 5;
        this.updateConfig();
      }
    },
    updateConfig() {
      let params = {
        autodeliveryOrderTime: this.autodeliveryOrderTime * 60,
        autodeliveryIds: this.autodeliveryIds.toString(),
        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,
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.voice {
  /deep/ .el-checkbox__input.is-checked .el-checkbox__inner,
  /deep/ .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background-color: #fc7200;
    border-color: #fc7200;
  }
  /deep/ .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #fc7200;
  }
  /deep/ .el-button--primary {
    color: #fff;
    background-color: #fc7200;
    border-color: #fc7200;
  }
  .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;
            cursor: pointer;
          }
          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;
    }
    .active {
      border: 1px solid #fc7200;
      color: #fff;
      background: #fc7200;
    }
  }
  .auto-time {
    width: 150px;
  }
}
</style>
