<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    destroy-on-close
    @closed="$emit('closed')"
    :draggable="true"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="100px"
    >
      <el-row>
        <el-col :md="12" :sm="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="标识" prop="code">
            <el-input v-model="form.code" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="触发" prop="trigger">
            <el-select v-model="form.trigger">
              <el-option
                v-for="item in trigger_options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="自主算法" prop="self_algo">
            <el-switch v-model="form.self_algo"></el-switch>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="处理时间" prop="hanle_minute">
            <el-input-number
              v-model="form.hanle_minute"
              :min="0"
              :max="32767"
              placeholder="分钟"
              controls-position="right"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <sc-title title="音响配置"></sc-title>
      <el-row>
        <el-col :md="12" :sm="24">
          <el-form-item label="事件优先级" prop="priority" placeholder="数字越小级别越高">
            <el-input-number
              v-model="form.priority"
              :min="1"
              :max="99"
              controls-position="right"
            ></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="喇叭" prop="speakers">
            <el-select v-model="form.speakers" multiple="true">
              <el-option
                v-for="item in speakersptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="播放次数" prop="voice_num">
            <el-input-number
              v-model="form.voice_num"
              :min="2"
              :max="10"
              controls-position="right"
            ></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="喇叭声音" prop="voice_person">
            <el-select v-model="form.voice_person">
              <el-option
                v-for="item in voiceoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="允许时间" prop="same_allow_minute">
            <el-input-number
              v-model="form.same_allow_minute"
              :min="0"
              controls-position="right"
            ></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="喇叭开启" prop="speaker_on">
            <el-switch v-model="form.speaker_on"></el-switch>
          </el-form-item>
        </el-col>

        <el-col :md="12" :sm="24">
          <el-form-item label="开启区域" prop="filter_area_level">
            <el-select v-model="form.filter_area_level">
              <el-option
                v-for="item in filter_area_level_options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ["success", "closed"],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
        show: "查看",
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        speaker_on: true,
        self_algo: false,
        speakers: [],
      },
      //验证规则
      rules: {
        name: [{ required: true, message: "请输入" }],
      },
      filter_area_level_options: [
        {
          value: 10,
          label: "办公生活区以上",
        },
        {
          value: 20,
          label: "生产一般区以上",
        },
        {
          value: 30,
          label: "生产重点区以上",
        },
      ],
      trigger_options: [
        {
          value: 10,
          label: "监控",
        },
        {
          value: 20,
          label: "定位",
        },
      ],
      voiceoptions: [
        {
          value: 3,
          label: "男生",
        },
        {
          value: 0,
          label: "女生",
        },
      ],
      speakersptions: [],
      //所需数据选项
      groups: [],
      groupsProps: {
        value: "id",
        emitPath: false,
        checkStrictly: true,
      },
    };
  },
  mounted() {
    this.getSpeakers();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //固定喇叭列表
    getSpeakers() {
      this.$API.third.tdevice.list.req({ type: 50, page: 0 }).then((res) => {
        this.speakersptions = res;
      });
    },

    //表单提交方法
    async submit() {
      var valid = await this.$refs.dialogForm.validate().catch(() => {});
      if (!valid) {
        return false;
      }
      this.isSaveing = true;
      try {
        var res;
        if (this.mode == "add") {
          res = await this.$API.ecm.event_cate.create.req(this.form);
        } else if (this.mode == "edit") {
          res = await this.$API.ecm.event_cate.update.req(
            this.form.id,
            this.form
          );
        }
        this.isSaveing = false;
        this.$emit("success", this.form, this.mode);
        this.visible = false;
        this.$message.success("操作成功");
        return res;
      } catch (err) {
        //可以处理校验错误
        this.isSaveing = false;
        return err;
      }
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
      debugger;
      console.log(this.form);
    },
  },
};
</script>

<style>
</style>
