<template>
  <div>
    <BaseDialog
      width="400px"
      :isShow.sync="isShow"
      :title="activeData.costCatId ? '修改' : '新增'"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="成本分类：" prop="categoryName">
          <el-input
            clearable
            v-model="formData.categoryName"
            placeholder="输入成本分类名称"
          ></el-input>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    activeData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      formData: {
        categoryName: "",
      },
      rules: {
        categoryName: [
          { required: true, message: "请输入成本分类名称", trigger: "blur" },
        ],
      },
      days: [],
    };
  },
  methods: {
    init() {
      this.formData = JSON.parse(JSON.stringify(this.activeData));
      if (!this.formData.costCatId) {
        this.formData = {
          categoryName: "",
        };
      }
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api[
            this.activeData.costCatId
              ? "edittopinstcategory"
              : "addtopinstcategory"
          ](this.formData).then((res) => {
            this.$message.success("成功");
              this.$store.commit("COSTCAT");
            this.isShow = false;
            this.$emit("search");
          });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        if (this.dialogVisible) {
          this.init();
        }
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 220px;
  margin-right: 10px;
}
.el-select {
  width: 220px;
  margin-right: 10px;
}
</style>
