<template>
  <div>
    <el-dialog
      title="新增文件夹"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form
        size="medium"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="所属事项">
          <el-input disabled :value="form.matterName"></el-input>
        </el-form-item>
        <el-form-item label="文件夹名称" prop="categoryName">
          <el-input
            v-model="form.categoryName"
            placeholder="请输入文件夹名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="文件夹编码" prop="categoryCode">
          <el-input
            v-model="form.categoryCode"
            placeholder="请输入文件夹编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleRest">重 置</el-button>
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addMatterFolder } from "@/api/matter";
export default {
  props: {
    addFolderVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      form: {
        categoryName: "", // 名称
        categoryCode: "", // 编码
        matterId: "", // 事项id
        matterName: "", // 事项名称
        sort: 99, // 排序
      },
      loading: false,
      rules: {
        categoryName: [
          { required: true, message: "请输入文件夹名称", trigger: "blur" },
        ],
        categoryCode: [
          { required: true, message: "请输入文件夹编码", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.addFolderVisible;
      },
      set(val) {
        this.$emit("update:addFolderVisible", val);
      },
    },
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let res = await addMatterFolder(this.form);
          let { code, msg } = res.data;
          this.loading = false;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("addSuccess");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    onAdd(row) {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
      this.form.matterId = row.id;
      this.form.matterName = row.matterName;
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = { ...row };
      }, 10);
    },
    // 重置
    handleRest() {
      this.resetForm("form");
    },
    // 关闭
    handleClose() {
      this.loading = false;
      this.resetForm("form");
      this.Visible = false;
    },
  },
};
</script>

<style lang="less" scoped></style>
