<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogShow"
      width="800px"
      :before-close="close"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="供应商" prop="name">
          <el-input
            v-model="form.name"
            style="width: 240px"
            size="small"
            placeholder="请输入供应商公司名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="联系人">
          <el-input
            v-model.number="form.contact"
            style="width: 240px"
            size="small"
            placeholder="请输入联系人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model.number="form.phone"
            style="width: 240px"
            size="small"
            placeholder="请输入联系人手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model.number="form.remark"
            style="width: 240px"
            size="small"
            placeholder="请输入备注信息"
          ></el-input>
        </el-form-item>
      </el-form>

      <!-- 按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :disabled="loading" @click="saveClick"
        >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getSupplierDetail, setSupplierList, addSupplierList, removeSupplierList} from "@/api/business/supplier";
export default {
  props: {
    // 弹框显示
    dialogShow: {
      type: Boolean,
      default: false,
    },
    // ID
    id: {
      type:[Number, String],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      form: {
        type: 'link',
        sort: 0
      },

      rules: {
        name: [
          { required: true, message: "请输入供应商名称", trigger: "blur" },
        ],
        phone: [
          { pattern: /^([1-9]\d*)$/, message: "请输入正确的数字", trigger: ["blur", "change"] }
        ]
      },
    };
  },
  computed: {
    title() {
      return this.id ? '编辑供应商' : '添加供应商';
    },
  },
  created() {
    if(this.id){
      getSupplierDetail(this.id).then(res => {
        const { name, phone, contact, remark} = res.data
        this.form = { name, phone, contact, remark}
      })
    }
  },
  methods: {
    // 关闭弹框
    close() {
      this.$emit("close");
      this.loading = false;
      this.$refs["form"].resetFields();
    },

    // 确认
    saveClick() {
      this.loading = true;
      const subForm = this.$refs["form"];
      subForm.validate((valid) => {
        if (valid) {
          this.submitForm(this.form);
        } else {
          this.loading = false;
          return false;
        }
      });
    },

    // 提交
    submitForm(form) {
      let data = {
        ... form,
      };
      let request = this.id ? setSupplierList : addSupplierList
      if(this.id){
        data.id = this.id
      }
      request(data)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess(this.id ? "修改成功" : '添加成功');
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped>
.channel {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tip {
  font-size: 12px;
  color: #999;
}
</style>
