<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="Visible"
      :destroy-on-close="true"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事项简称" prop="matterName">
          <el-input
            size="small"
            v-model="form.matterName"
            placeholder="请输入事项简称"
          ></el-input>
        </el-form-item>
        <el-form-item label="事项全称" prop="matterFullName">
          <el-input
            size="small"
            v-model="form.matterFullName"
            placeholder="请输入事项全称"
          ></el-input>
        </el-form-item>
        <el-form-item label="事项编号" prop="matterNo">
          <el-input
            size="small"
            v-model="form.matterNo"
            placeholder="请输入事项编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择部门" prop="deptCode">
          <el-select
            size="small"
            ref="myselected"
            v-model="form.deptCode"
            placeholder="请选择部门"
            @change="handleChange"
            filterable
          >
            <el-option
              v-for="v in departmentList"
              :key="v.deptNumber"
              :label="v.name"
              :value="v.deptNumber"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleRest">重 置</el-button>
        <el-button size="small" type="primary" @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addMatter } from "@/api/matter";
import local from "@/utils/local";
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: "",
    },
    addMatterVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
    departmentList: {
      type: Array,
      required: true,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      form: {
        matterFullName: "", // 事项全称
        matterName: "", // 事项简称
        deptId: "", // 部门id
        deptCode: "", // 部门编号
        matterNo: "", // 事项编号
        siteId: local.getLocal("sampleSiteId")
          ? local.getLocal("sampleSiteId")
          : "",
        source: 1, // 1为手动添加,
        deptName: "",
      },
      rules: {
        matterFullName: [
          { required: true, message: "请输入事项全称", trigger: "blur" },
        ],
        matterName: [
          { required: true, message: "请输入事项简称", trigger: "blur" },
        ],
        deptCode: [
          { required: true, message: "请输选择部门", trigger: "change" },
        ],
        matterNo: [
          { required: true, message: "请输入事项编号", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.addMatterVisible;
      },
      set(val) {
        this.$emit("update:addMatterVisible", val);
      },
    },
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await addMatter(this.form);
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("addMatter");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
    },
    // 编辑
    onEdit(row) {
      this.form = { ...row };
    },
    // 重置
    handleRest() {
      this.$refs.form.resetFields();
    },
    // 关闭
    handleClose() {
      this.$refs.form.resetFields();
      this.Visible = false;
    },
    handleChange(val) {
      let name = this.departmentList.find((v) => v.deptNumber === val).name;
      this.form.deptName = name;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-select {
  width: 100%;
}
</style>