<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
    :draggable="true"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="100px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="标识" prop="code">
        <el-input v-model="form.code" clearable></el-input>
      </el-form-item>
      <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>
      <sc-title title="音响配置"></sc-title>
      <el-form-item label="音响开启" prop="speaker_on">
        <el-switch v-model="form.speaker_on"></el-switch>
      </el-form-item>
      <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-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
      },
      //验证规则
      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: "定位",
        },
      ],
      //所需数据选项
      groups: [],
      groupsProps: {
        value: "id",
        emitPath: false,
        checkStrictly: true,
      },
    };
  },
  mounted() {},
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //表单提交方法
    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);
    },
  },
};
</script>

<style>
</style>
