<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false">
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <row-list :col="2">
        <row-item>

          <a-form-item
            label="编号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.no"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.name"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="规格"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.specs"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="单位"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.unit"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="指导单价"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.price"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="数量"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.quantity"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="指导总价"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.totalMoney"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="品牌"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-model="record.brand"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="供应商"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              style="width: 70%"
              v-decorator="['supplierId', {rules: [{required: true, message: '供应商不能为空'}]}]"
              @change="changeSupplier"
              placeholder="请选择">
              <a-select-option
                v-for="item in suppliers"
                :key="item.id"
                :label="item.name"
                :value="item.id">{{ item.name }}
              </a-select-option>
            </a-select>
            <a-button style="width: 30%" type="primary" @click="shift()">{{ shiftTitle }}</a-button>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="供应商单价"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              @keyup="handleSupplierTotalMoney"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['supplyPrice', {rules: [{required: true, message: '供应商单价不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="供应商总价"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['supplyTotalMoney', {rules: [{required: true, message: '供应商总价不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-textarea
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import DictCache from '@/utils/dict'
import { querySupplier } from '@/api/purchase/supplier'

export default {
  name: 'BasePurchaseOrderListEnquiry',
  components: { },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      suppliers: [],
      noSuppliers: [{ id: '-1', name: '零售门店' }],
      originSuppliers: [],
      type: '-1',
      // 下拉框map
      unitMap: {},
      shiftTitle: '无供应商',
      shiftValue: 1,
      record: {},
      originRecord: {},
      areaMap: {},
      // 下拉框map
      statusMap: {},
      supplierName: null

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.areaMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
  },
  methods: {
    base (record, type) {
      this.shiftValue = 1
      this.shiftTitle = '无供应商'
      this.suppliers = []
      this.originRecord = {
        ...record
      }
      this.visible = true
      this.type = type
      const { form: { setFieldsValue } } = this
      this.modalTitle = '询价'
      this.record = {
        ...record
      }
      this.record.price = this.BaseTool.Amount.formatter(this.record.price)
      this.record.totalMoney = this.BaseTool.Amount.formatter(this.record.totalMoney)
      this.supplierName = record.supplierName
      if (record.supplierId === '-1') {
        this.shiftValue = 0
        this.shiftTitle = '选择供应商'
        this.suppliers = this.noSuppliers
        this.supplierName = record.supplierName
      }
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'supplierId',
          'supplierName',
          'supplyPrice',
          'supplyTotalMoney',
          'remark'
        ])))
      })
      this.getSuppliers()
    },
    getSuppliers () {
      querySupplier({ supplyType: this.type, statusList: [2, 3], projectId: this.record.projectId }).then(res => {
        this.suppliers = res.data
        this.originSuppliers = res.data
      })
    },
    changeSupplier (value, option) {
      this.supplierName = option.componentOptions.propsData.label
    },
    shift () {
      const { form: { setFieldsValue } } = this
      if (this.shiftValue === 1) {
        this.shiftValue = 0
        this.shiftTitle = '选择供应商'
        this.suppliers = this.noSuppliers
        this.supplierName = this.suppliers[0].name
        this.$nextTick(() => {
          setFieldsValue({
            supplierId: this.suppliers[0].id
          })
        })
      } else {
        this.shiftValue = 1
        this.shiftTitle = '无供应商'
        this.suppliers = this.originSuppliers
        this.$nextTick(() => {
          setFieldsValue({
            supplierId: ''
          })
        })
      }
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        const record = {
          ...this.originRecord,
          ...values
        }
        const exchangeRate = this.$store.getters.exchangeRate
        record.accountTotalMoney = this.BaseTool.Number.div(record.supplyTotalMoney, exchangeRate, 2)
        record.accountPrice = this.BaseTool.Number.div(record.supplyPrice, exchangeRate, 2)
        record.supplierName = this.supplierName
        this.BaseTool.ListForm.pushOneListUpdate(this, record)
        this.handleCancel(values)
        // updatePurchaseOrderList(values)
        //   .then(() => {
        //     this.handleCancel(values)
        //   }).catch(() => {
        //     this.confirmLoading = false
        //   })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    },
    handleSupplierTotalMoney () {
      const { form: { setFieldsValue, getFieldValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'supplyTotalMoney': getFieldValue('supplyPrice') * this.record.quantity
        }))
      })
    }

  }
}
</script>
