<template>
  <div class="rule-type">
    <DialogCom
      width="600px"
      :title="index >= 0 ? '编辑检查内容' : '新增检查内容'"
      :visible.sync="dataVisible"
      :show-close="false"
      @close="
        () => {
          this.$refs.form.resetFields();
        }
      "
      append-to-body
    >
      <div>
        <el-form
          ref="form"
          :model="formData"
          :rules="formDataRules"
          label-width="110px"
          class="formbox"
        >
          <!-- <el-form-item label="检查要点编号" prop="pointNum">
            <el-input
              v-model="formData.pointNum"
              type="number"
              laceholder="请输入检查要点编号"
              @blur="check()"
              @input="formData.pointNum = Number(formData.pointNum)"
            ></el-input>
          </el-form-item> -->
          <el-form-item label="检查内容" prop="name">
            <el-input
              v-model.trim="formData.name"
              placeholder="请输入检查内容"
              maxlength="100"
            ></el-input>
          </el-form-item>
          <el-form-item label="检查区域" prop="areaId">
            <el-select
              v-model="formData.areaId"
              placeholder="请选择检查区域"
              @change="onAreaChanged"
              clearable
            >
              <el-option
                v-for="item in areas"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="数据来源" prop="businessType">
            <el-select
              v-model="formData.businessType"
              placeholder="请选择数据来源"
              clearable
            >
              <el-option
                v-for="item in pointDataSource"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              type="textarea"
              v-model="formData.remark"
              maxlength="200"
              :rows="3"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dataVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit()">{{
          index >= 0 ? "保 存" : "添 加"
        }}</el-button>
      </div>
    </DialogCom>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { allAreaByOrgtype } from "@/api/system/area";
export default {
  data() {
    return {
      index: null,
      dataVisible: false,
      formData: this.reset(),
      formDataRules: {
        name: [{ required: true, message: "请输入检查内容" }],
        areaId: [{ required: true, message: "请选择检查区域" }],
      },
      currentOrgType: null,
      areas: [],
    };
  },
  props: {
    rule: {
      type: Object,
    },
    pointDataSource: {
      type: Array,
      default: [],
    },
  },
  watch: {},
  computed: {
    ...mapState([]),
  },
  methods: {
    ...mapMutations([]),
    reset() {
      return {
        id: null,
        name: null,
        areaId: null,
        areaName: null,
        businessType: null,
        remark: null,
      };
    },
    async show(index, data) {
      if (index >= 0) {
        this.index = index;
        this.formData = data;
      } else {
        this.index = -1;
        this.formData = this.reset();
      }

      // if (this.currentOrgType != this.rule.orgType) {
      allAreaByOrgtype(this.rule.orgType).then((d) => {
        this.currentOrgType = this.rule.orgType;
        this.areas = d.data;
      });
      // }

      this.dataVisible = true;
    },
    onSubmit() {
      this.$refs.form.validate((isValidate) => {
        if (!isValidate) return;
        let obj = {};
        obj = { ...this.formData };
        if (obj.businessType == "") {
          obj.businessType = null;
        }

        this.$emit("submit", this.index, obj);
        this.dataVisible = false;
      });
    },
    // 事件
    onHide() {
      this.isShow = false;
    },
    onAreaChanged(val) {
      let area = this.areas.find((a) => a.id === val);
      if (area) {
        this.formData.areaName = area.name;
      }
    },
  },
  mounted() {},
  components: {},
};
</script>

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