<template>
  <div>
    <!-- 弹窗 -->
    <a-modal
      v-model="isShow"
      @cancle="isShow=false"
      :footer="null"
      width="60%"
      :centered="true"
      :title="titleText">
      <a-form id="components-form-demo-validate-other" ref="form" :form="form" v-bind="formItemLayout" @submit="handleSubmit">
        <a-form-item label="商户名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
          <a-input
            id="addSupplierModal-name"
            :maxLength="30"
            v-decorator="['formData.name', { initialValue: formData.name,getValueFromEvent: $filterEmpty, rules: [{ required: true, message: '请输入供应商名称(最多30个字符)!' }] }]"
            placeholder="请输入商户名称(最多30个字符)"
            allowClear />
        </a-form-item>
        <a-form-item label="负责人名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
          <a-input
            id="addSupplierModal-contactName"
            :maxLength="30"
            v-decorator="['formData.contactName', { initialValue: formData.contactName,getValueFromEvent: $filterEmpty, rules: [{ required: true, message: '请输入联系人!' }] }]"
            placeholder="请输入联系人"
            allowClear />
        </a-form-item>
        <a-form-item label="负责人电话" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
          <a-input
            id="addSupplierModal-contactPhone"
            type="number"
            v-decorator="['formData.contactPhone', { initialValue: formData.contactPhone,getValueFromEvent: $filterEmpty, rules: [{ required: true, message: '请输入负责人电话!' }] }]"
            placeholder="请输入负责人电话"
            allowClear />
        </a-form-item>
        <a-form-item :wrapper-col="{ span: 12, offset: 6}" style="text-align: center;">
          <a-button type="primary" html-type="submit" id="addSupplierModal-submit" style="margin-right: 15px">保存</a-button>
          <a-button @click="isShow=false" style="margin-left: 15px" id="addSupplierModal-close">取消</a-button>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import {
  SellerDetails,
  saveSeller
} from '@/api/businessManage.js'
export default {
  name: 'AddPartnerModal',
  props: {
    openSupplierModal: {
      type: Boolean,
      default: false
    },
    itemId: {
      type: String,
      default: ''
    },
    itemData: {
	  type: Object,
	  default: function () {
	    return {}
	  }
    }
  },
  data () {
    return {
      formItemLayout: {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 14
        }
      },
	  titleText: '新增',
      isShow: this.openSupplierModal,
      form: this.$form.createForm(this, { name: 'AddPartnerModal' }),
      formData: {
        name: '', // 名称
        contactName: '', // 联系人
        contactPhone: '' // 手机号码
      }
    }
  },
  methods: {
    // 查看详情
    getPageInfo () {
      SellerDetails({
        id: this.itemId
      }).then(res => {
        if (res.status == 200) {
          // this.form.setFieldsValue({ 'formData': res.data })
          this.form.setFieldsValue({ 'formData.name': res.data.name })
          this.form.setFieldsValue({ 'formData.contactName': res.data.contactName })
          this.form.setFieldsValue({ 'formData.contactPhone': res.data.contactPhone })
        }
      })
    },
    // 点击保存;
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.form.validateFields((err, values) => {
        if (!err) {
          const formData = Object.assign({}, this.itemData, this.formData, values.formData)
          formData.id = _this.itemId
          saveSeller(formData).then(res => {
            console.log(res, 'rrrrrrrrr')
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('refresh')
              setTimeout(function () {
                _this.isShow = false
              }, 300)
            }
          })
        }
      })
    }
  },
  watch: {
    openSupplierModal (newValue, oldValue) {
      this.isShow = newValue
    },
    isShow (val) {
      if (!val) {
        this.$emit('close')
      } else {
   	  this.form.resetFields()
      }
    },
    itemId (newValue, oldValue) {
      console.log(newValue, '----------id')
      if (newValue && this.isShow) {
        this.titleText = '编辑'
        this.getPageInfo(newValue)
      } else {
   	  this.titleText = '新增'
      }
    }
  }
}
</script>

<style>
</style>
