<template>
  <div>
    <BaseDialog
      width="400px"
      :isShow.sync="isShow"
      :title="activeData.instId ? '修改' : '新增'"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="供应商：" prop="instName">
          <el-input
            clearable
            v-model="formData.instName"
            placeholder="输入职能部门全称"
          ></el-input>
        </el-form-item>
        <el-form-item label="收款账号：" prop="bankName"
          ><el-input
            clearable
            v-model="formData.bankName"
            placeholder="输入开户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="bank"
          ><el-input
            clearable
            v-model="formData.bank"
            placeholder="输入开户行"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="bankAccount"
          ><el-input
            clearable
            v-model="formData.bankAccount"
            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: {
        instName: "",
        bankName: "",
        bank: "",
        bankAccount: "",
      },
      rules: {
        instName: [
          { required: true, message: "请输入机构名称", trigger: "blur" },
        ],
        bankName: [
          { required: true, message: "请输入开户名", trigger: "blur" },
        ],
        bank: [
          { required: true, message: "请输入开户行", trigger: "blur" },
        ],
        bankAccount: [
          { required: true, message: "请输入账号", trigger: "blur" },
        ],
      },
      days: [],
    };
  },
  methods: {
    init() {
      this.formData = JSON.parse(JSON.stringify(this.activeData));
      if (!this.formData.instId) {
        this.formData = {
          instName: "",
          bankName: "",
          bank: "",
          bankAccount: "",
        };
      }
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api[
            this.activeData.instId ? "edittopinstitution" : "addtopinstitution"
          ](this.formData).then((res) => {
            this.$message.success("操作成功");
            this.$store.commit("INSTITUTION");
            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>
