<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="algo">
        <el-select v-model="form.algo">
          <el-option
            v-for="item in algooptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="视频" prop="vchannel">
        <el-select v-model="form.vchannel">
          <el-option
            v-for="item in vchanneloptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="始终开启" prop="always_on">
        <el-switch v-model="form.always_on"></el-switch>
      </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: {
        algo: "",
        vchannel: "",
        always_on: false,
      },
      //验证规则
      rules: {
        algo: [{ required: true, message: "请选择算法" }],
        vchannel: [{ required: true, message: "请选择视频" }],
      },
      algooptions: [],
      vchanneloptions: [],
    };
  },
  mounted() {
    this.getAlgo();
    this.getVchannel();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
     //视频列表
    getAlgo() {
      this.$API.ecm.event_cate.list.req({ self_algo:true,page: 0 }).then((res) => {
        this.algooptions = res;
      });
    },
    //视频列表
    getVchannel() {
      this.$API.third.tdevice.list.req({ type: 60, page: 0 }).then((res) => {
        this.vchanneloptions = 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.vchannel.create.req(this.form);
        } else if (this.mode == "edit") {
          res = await this.$API.ecm.vchannel.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>
