<template>
  <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="供应商编号" prop="supplierCode">
        <el-input v-model="form.supplierCode" placeholder="请输入供应商编号" />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
      </el-form-item>
      <el-form-item label="简称" prop="shortName">
        <el-input v-model="form.shortName" placeholder="请输入简称" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="经营范围" prop="businessScope">
        <el-textarea v-model="form.businessScope" placeholder="请输入经营范围" :rows="3" />
      </el-form-item>
      <el-form-item label="资质等级" prop="qualification">
        <el-input v-model="form.qualification" placeholder="请输入资质等级" />
      </el-form-item>
      <el-form-item label="注册资本" prop="registerCapital">
        <el-input v-model="form.registerCapital" placeholder="请输入注册资本" />
      </el-form-item>
      <el-form-item label="成立日期" prop="establishDate">
        <el-date-picker v-model="form.establishDate" type="date" placeholder="请选择成立日期" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option label="正常" value="normal" />
          <el-option label="禁用" value="disabled" />
          <el-option label="黑名单" value="blacklist" />
        </el-select>
      </el-form-item>
      <el-form-item label="合作状态" prop="cooperationStatus">
        <el-select v-model="form.cooperationStatus" placeholder="请选择合作状态">
          <el-option label="合作中" value="cooperating" />
          <el-option label="已终止" value="terminated" />
          <el-option label="待审核" value="pending" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-textarea v-model="form.remark" placeholder="请输入备注" :rows="3" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确定</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSupplier, updateSupplier, getSupplier } from '@/api/engineering/supplier'

export default {
  name: 'SupplierAddOrEdit',
  data() {
    return {
      open: false,
      title: '',
      form: {},
      rules: {
        supplierCode: [
          { required: true, message: '供应商编号不能为空', trigger: 'blur' }
        ],
        supplierName: [
          { required: true, message: '供应商名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open(row) {
      this.open = true
      this.form = {
        supplierId: null,
        supplierCode: '',
        supplierName: '',
        shortName: '',
        contactPerson: '',
        contactPhone: '',
        email: '',
        address: '',
        businessScope: '',
        qualification: '',
        registerCapital: '',
        establishDate: '',
        status: 'normal',
        cooperationStatus: 'cooperating',
        remark: ''
      }
      if (row) {
        this.title = '修改供应商'
        Object.assign(this.form, row)
      } else {
        this.title = '新增供应商'
      }
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.supplierId) {
            updateSupplier(this.form).then(() => {
              this.$message.success('修改成功')
              this.open = false
              this.$emit('refresh')
            })
          } else {
            addSupplier(this.form).then(() => {
              this.$message.success('新增成功')
              this.open = false
              this.$emit('refresh')
            })
          }
        }
      })
    },
    cancel() {
      this.open = false
    }
  }
}
</script>