<template>
  <div class="rule-type">
    <DialogCom
      :title="id ? '编辑报警控制器' : '新增报警控制器'"
      :visible.sync="isShow"
      @close="onHide"
      width="500px"
    >
      <div class="page-body">
        <el-form
          :model="formData"
          :rules="formDataRules"
          size="small"
          ref="form"
          label-position="right"
          label-width="130px"
          label-prefix=":"
        >
          <el-form-item label="所属机构" prop="orgId">
            <orgDropDown
              v-model="formData.orgId"
              placeholder="选择所属机构"
              @select="onOrgSelect"
            />
          </el-form-item>
          <el-form-item label="报警主机" prop="alarmHostId">
            <el-select
              label="报警主机"
              v-model="formData.alarmHostId"
              placeholder="请选择报警主机"
              clearable
              style="width:330px"
            >
              <el-option
                v-for="host in alarmHostOptions"
                :key="host.id"
                :label="host.name"
                :value="host.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="name" label="报警控制器名称:">
            <el-input
              v-model="formData.name"
              :maxlength="50"
              name="name"
              placeholder="请输入报警控制器名称"
              clearable
            />
          </el-form-item>

          <el-form-item prop="type" label="24小时报警控制器:">
            <el-switch
              v-model="formData.allHour"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </div>
    </DialogCom>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
import * as api from "@/api/resumption/protection";
import { listIdName } from "@/api/system/device";
import orgDropDown from "../../../components/orgTree/orgDropDown.vue";
export default {
  data() {
    return {
      id: null,
      isShow: false,
      formData: this.reset(),
      alarmHostOptions: [],
      // 机构树选项
      formDataRules: {
        orgId: [{ required: true, message: "请选择所属机构" }],
        name: [{ required: true, message: "请输入报警控制器名称" }],
        alarmHostId: [{ required: true, message: "请选择报警主机" }],
      },
    };
  },
  props: {},
  watch: {
    // "formData.orgId":{
    //   handler(v){
    //     console.info("formData.orgId",v)
    //   }
    // }
  },
  computed: {
    ...mapGetters(["orgId"]),
  },
  methods: {
    ...mapMutations([]),
    reset() {
      return {
        id: null,
        orgId: null,
        orgPath: null,
        orgName: null,
        name: null,
        allHour: 0,
        alarmHostId: null,
      };
    },
    async refresh(id, other) {
      this.formData = id ? (await api.get(id)).data : this.reset(other);
    },
    async show(id, other = {}) {
      this.id = id;
      await this.refresh(id, other);
      this.isShow = true;
    },

    // 事件
    onHide() {
      this.$refs.form.resetFields();
      this.formData = this.reset();
    },
    onSubmit() {
      this.$refs.form.validate(async (isValidate) => {
        if (!isValidate) return;
        await api.update(this.formData);
        this.$emit("success");
        this.isShow = false;
      });
    },
    onOrgSelect(node) {
      this.formData.orgPath = node.path;
      this.formData.orgName = node.shortName;
      listIdName({ orgId: node.id, deviceType: 1, assetType: 2 }).then((d) => {
        this.alarmHostOptions = d.data;
        if(!d.data.find(i=>i.id==this.formData.alarmHostId)){
          this.formData.alarmHostId  = null;
        }       
      });
    },
  },
  created() {},
  mounted() {},
  components: { orgDropDown },
};
</script>

<style lang="scss" scoped>
.brand_info {
  .el-form {
    width: 600px;
    padding-top: 40px;
  }
}
</style>
