<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 prop="name" label="检查内容库名称:">
            <el-input
              v-model="formData.name"
              :maxlength="50"
              placeholder="请输入检查内容库名称"
              clearable
              style="width:280px;"
            />
          </el-form-item>
          <el-form-item prop="orgType" label="受检机构类型:">
            <el-select
              prop="orgType"
              label="受检机构类型"
              v-model="formData.orgType"
              placeholder="请选择受检机构类型"
              clearable
              style="width:280px;"
            >
              <el-option
                v-for="item in orgTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item prop="status" label="状态:">
            <el-select
              prop="status"
              label="状态"
              v-model="formData.status"
              placeholder="请选择状态"
              clearable
              style="width:280px;"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="remark" label="备注">
            <el-input type="textarea" v-model="formData.remark" :maxlength="255" clearable  style="width:280px;" :rows="3"/>
          </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 } from "vuex";
import * as api from "@/api/safetycheck/rule";

export default {
  data() {
    return {
      id: null,
      isShow: false,
      formData: this.reset(),
      formDataRules: {
        name: [{ required: true, message: "请输入检查内容库名称" }],
        type: [{ required: true, message: "请选择检查类型" }],
        orgType: [{ required: true, message: "请选择受检机构类型" }],
        status: [{ required: true, message: "请选择状态" }],
      },
    };
  },
  props: {
    orgTypeOptions: {
      type: Array,
    },
    ruleTypeOptions: {
      type: Array,
    },
    statusOptions: {
      type: Array,
    },
  },
  watch: {},
  computed: {
    ...mapState([]),
  },
  methods: {
    ...mapMutations([]),
    reset() {
      return {
        id: null,
        name: null,
        type: null,
        orgType: null,
        status: null,
        remark: 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.formData=this.reset();
      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;
      });
    },

    // 事件
    //apimark//
  },
  mounted() {},
  components: {},
};
</script>

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