<template>
  <div class="rule-type">
    <el-dialog
      :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 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>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
import * as api from "@/api/resumption/protection";
import orgDropDown from "../../../components/orgTree/orgDropDown.vue";
export default {
  data() {
    return {
      id: null,
      isShow: false,
      formData: this.reset(),
      // 机构树选项
      formDataRules: {
        orgId: [{ required: true, message: "请选择所属机构" }],
        name: [{ 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: 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();     
    },
    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.name;
    },
  },
  created() {},
  mounted() {},
  components: { orgDropDown },
};
</script>

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