<template>
  <div class="voice">
    <div class="model">
      <div class="title">通知设置</div>
      <div class="list">
        <div class="item">
          <div class="left">
            <div class="left-top">
              <div class="name">语音开启</div>
            </div>
          </div>
          <div class="right">
            <el-switch @change="changeMessageStatus" v-model="open" :active-value="1" :inactive-value="0" active-color="#FC7200" inactive-color="#999" />
          </div>
        </div>
      </div>
    </div>
    <div class="model">
      <div class="title">播报类型选择</div>
      <div class="list">
        <div class="item" v-for="(v,i) in voiceList" :key=i>
          <div class="left">
            <div class="left-top">
              <img @click="play(i)" src="../../../static/image/voice-icon.png" class="voice-icon" />
              <div class="name">{{v.name}}</div>
            </div>
          </div>
          <div class="right">
            <el-switch @change="updateSavePushMessageStatus" v-model="v.open" :active-value="1" :inactive-value="0" active-color="#FC7200" inactive-color="#999" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryPushMessageStatus,
  updateSavePushMessageStatus,
} from "../../api/setting.js";
export default {
  name: "voiceSetting",
  data() {
    return {
      voiceList: [],
      open: 1,
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {
    voiceList: {
      handler(newVal, oldVal) {
        if (!newVal.length) return;
        let arr = newVal.map((v) => {
          return v.open;
        });
        let arr1 = [...new Set(arr)];
        if (arr1.length === 1) {
          this.open = arr1[0];
        } else {
          this.open = 1;
        }
      },
      deep: true,
    },
  },
  // 方法集合
  methods: {
    changeMessageStatus() {
      this.voiceList = this.voiceList.map((v) => {
        v.open = this.open;
        return v;
      });
      this.updateSavePushMessageStatus();
    },
    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这个音频对象
    },
    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,
            });
          }
        }
      );
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.queryPushMessageStatus();
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  // 生命周期 - 创建之前
  beforeCreate() {},
  // 生命周期 - 挂载之前
  beforeMount() {},
  // 生命周期 - 更新之前
  beforeUpdate() {},
  // 生命周期 - 更新之后
  updated() {},
  // 生命周期 - 销毁之前
  beforeDestroy() {},
  // 生命周期 - 销毁完成
  destroyed() {},
  // 如果页面有keep-alive缓存功能，这个函数会触发
  activated() {},
};
</script>

<style lang="scss" scoped  type="text/css">
.model {
  .title {
    position: relative;
    font-size: 16px;
    font-weight: bold;
    line-height: 22px;
    color: #333333;
    padding-left: 10px;
    margin-top: 24px;
    .tips {
      font-size: 14px;
      font-weight: 400;
      line-height: 22px;
      color: #999999;
    }
  }
  .title::before {
    width: 3px;
    height: 16px;
    background: rgba(252, 114, 0, 0.39);
    border-radius: 2px;
    content: "";
    position: absolute;
    top: 2px;
    left: 0;
  }
  .list {
    .item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #ffffff;
      margin-top: 10px;
      padding: 16px 20px;
      .left {
        display: flex;
        flex-direction: column;
        font-size: 14px;
        font-weight: bold;
        line-height: 20px;
        color: #000000;
        .left-top {
          display: flex;
          align-items: center;
          .voice-icon {
            width: 15px;
            height: 12px;
            margin-right: 10px;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>