<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1200"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{ top: '93px', left: '154px' }"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top: 0; height: 95%"
    >
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel"> 取消 </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="vendorModal">
          <a-row class="form-row" :gutter="24">
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="供应商编码">
                <a-input placeholder="自动生成" v-decorator.trim="['serialNo']" disabled />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
                <a-input placeholder="请输入名称" v-decorator.trim="['supplier', validatorRules.supplier]" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
                <a-input placeholder="请输入联系人" v-decorator.trim="['contacts']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="手机号码">
                <a-input placeholder="请输入手机号码" v-decorator.trim="['telephone']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
                <a-input placeholder="请输入联系电话" v-decorator.trim="['phoneNum']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="纳税人识别号">
                <a-input placeholder="请输入纳税人识别号" v-decorator.trim="['taxNum']" />
              </a-form-item>
            </a-col>

            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开户行">
                <a-input placeholder="请输入开户行" v-decorator.trim="['bankName']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="账号">
                <a-input placeholder="请输入账号" v-decorator.trim="['accountNumber']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
                <a-input placeholder="请输入地址" v-decorator.trim="['address']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
                <a-input
                  placeholder="请输入排序"
                  oninput="value=value.replace(/[^\d]/g, '')"
                  v-decorator.trim="['sort']"
                />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="供应商分级">
                <a-input placeholder="请输入供应商分级" v-decorator.trim="['supplierLevel']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="结算周期">
                <a-select
                  v-decorator.trim="['settlementMethod']"
                  :options="settleOptions"
                  placeholder="请选择结算周期"
                ></a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="付款方式">
                <a-select
                  v-decorator.trim="['billingCycleDays']"
                  placeholder="请选择付款方式"
                >
                  <a-select-option value="先款后货">先款后货</a-select-option>
                  <a-select-option value="先货后款">先货后款</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="采购对接人">
                <a-input placeholder="请输入采购对接人" v-decorator.trim="['procurementContact']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="到货天数">
                <a-input
                  placeholder="请输入到货天数"
                  oninput="value=value.replace(/[^\d]/g, '')"
                  v-decorator.trim="['deliveryDays']"
                />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开具发票情况">
                <a-select
                  v-decorator.trim="['invoiceType']"
                  :options="invoiceOptions"
                  placeholder="请选择发票类型"
                ></a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="税率(%)">
                <a-input-number
                  style="width: 100%"
                  oninput="value=value.replace(/[^\d.]/g, '')"
                  placeholder="请输入税率"
                  v-decorator.trim="['taxRate']"
                />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否支持售后">
                <a-select
                  v-decorator.trim="['isAfterSales']"
                  placeholder="请选择是否支持售后"
                >
                <a-select-option value="1">是</a-select-option>
                <a-select-option value="0">否</a-select-option>
              </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="售后规则">
                <a-textarea :rows="2" placeholder="请输入售后规则" v-decorator.trim="['afterSalesRules']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
                <a-textarea :rows="2" placeholder="请输入备注" v-decorator.trim="['description']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同附件">
                <j-upload v-decorator.trim="['contractUpload']" bizPath="bill"></j-upload>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { addSupplier, editSupplier, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
import JUpload from '@/components/jeecg/JUpload'

export default {
  components: { JUpload },
  name: 'VendorModal',
  mixins: [mixinDevice],
  data() {
    return {
      model: {},
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        supplier: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' },
            { validator: this.validateSupplierName },
          ],
        },
      },
      settleOptions: [
        {
          value: '0',
          label: '周结',
        },
        {
          value: '1',
          label: '现结/按单',
        },
        {
          value: '2',
          label: '预付款',
        },
        {
          value: '3',
          label: '月结',
        },
      ],
      invoiceOptions: [
        {
          value: '1',
          label: '增值税专用发票',
        },
        {
          value: '0',
          label: '普通发票',
        },
      ],
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'supplier',
            'contacts',
            'telephone',
            'email',
            'phoneNum',
            'fax',
            'beginNeedPay',
            'allNeedPay',
            'taxNum',
            'taxRate',
            'bankName',
            'accountNumber',
            'address',
            'sort',
            'description',
            'supplierLevel',
            'settlementMethod',
            'billingCycleDays',
            'procurementContact',
            'deliveryDays',
            'invoiceType',
            'contractUpload',
            'serialNo',
            'isAfterSales',
            'afterSalesRules',
          )
        )
        autoJumpNextInput('vendorModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          formData.type = '供应商'
          let obj
          if (!this.model.id) {
            obj = addSupplier(formData)
          } else {
            obj = editSupplier(formData)
          }
          obj
            .then((res) => {
              if (res.code === 200) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.data.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '供应商',
        id: this.model.id ? this.model.id : 0,
      }
      checkSupplier(params).then((res) => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    },
  },
}
</script>
<style scoped></style>
