// 添加/修改用户库
<template>
  <el-dialog
    :title="isEdit ? '编辑购买方公司' : '添加购买方公司'"
    width="500px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="120px"
    >
      <el-form-item prop="id" style="margin-bottom: 0">&nbsp;</el-form-item>
      <el-form-item label="公司名称" prop="companyName" required>
        <el-input v-model="form.companyName" />
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddress" required>
        <el-input v-model="form.companyAddress" />
      </el-form-item>
      <el-form-item label="公司负责人" prop="companyHeadName" required>
        <el-input v-model="form.companyHeadName" />
      </el-form-item>
      <el-form-item label="负责人手机号" prop="companyHeadPhone" required>
        <el-input
          v-model="form.companyHeadPhone"
          maxlength="11"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取消 </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { purchaserEdit } from "@/api/configuration";

export default {
  data() {
    return {
      form: {
        companyName: null,
        companyAddress: null,
        companyHeadName: null,
        companyHeadPhone: null,
      },
      rules: {
        companyName: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        companyAddress: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        companyHeadName: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        companyHeadPhone: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
      },
      visible: false,
      submitLoading: false,
      isEdit: false,
    };
  },
  created() {
    this.form_copy = JSON.parse(JSON.stringify(this.form));
  },
  methods: {
    open(obj) {
      this.visible = true;
      this.submitLoading = false;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj;
          this.isEdit = true;
        } else {
          this.form = JSON.parse(JSON.stringify(this.form_copy));
          this.isEdit = false;
        }
      });
    },
    submit(e) {
      e.preventDefault();
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        if (this.submitLoading) {
          return;
        }
        this.submitLoading = true;
        try {
          await purchaserEdit(this.form);
          this.visible = false;
          this.$emit("success", this.isEdit);
        } catch (e) {
          console.info("add", e);
        } finally {
          this.submitLoading = false;
        }
      });
    },
  },
};
</script>
