<template>
  <div>
    <p-modal
      title="新增(无账存商品)"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      width="80%"
      :enlargeable="true"
      :draggable="true"
      :destroyOnClose="true"
      :maskClosable="false"
    >
      <div class="gridClass">
        <p-grid-form :span="6" ref="forms" :labelCol="{ span: 5 }" :items="items"></p-grid-form>
      </div>
    </p-modal>
  </div>
</template>

<script>
import API from '../../api/default'
import { adjustAdd } from '../../api/api'
import { genCargoownerAuthSearchModal } from '../../../SearchModal'

const items = function (that) {
  return [
    {
      type: 'select',
      name: 'adjustType',
      allowClear: false,
      label: '损溢来源',
      value: '1',
      options: [
        { label: '无计划盘点', value: '1' },
        // { label: 'WMS调整', value: '2' }
      ],
    },
    { type: 'input', name: 'warehouseId', label: '仓库ID', hidden: true },
    { type: 'input', name: 'dcId', label: '仓配中心Id', hidden: true },
    {
      type: 'search',
      name: 'warehouseName',
      label: '仓库',
      destroyOnClose: true,
      required: true,
      targetClick: true,
      span: 6,
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseName',
            label: '仓库名称',
            span: 8,
            value: '',
          },
          {
            type: 'input',
            name: 'warehouseCode',
            label: '仓库编码',
            span: 8,
            value: '',
          },
        ],
      },
      tableConfig: {
        rowKey: 'warehouseName',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName' },
          { dataIndex: 'warehouseCode', title: '仓库编码', key: 'warehouseCode' },
          { dataIndex: 'warehouseId', title: '仓库ID', key: 'warehouseId' },
        ],
      },
      remoteUrl: API.warehouse,
      on: {
        ok(event, value, rowData, form) {
          that.warehouseId = rowData[0].warehouseId
          console.log(rowData[0].warehouseId, 'rowDatarowData')
          form.setFieldsValue({
            warehouseId: rowData[0].warehouseId,
            dcId: rowData[0].dcId,
            itemId: '',
            itemName: '',
            pieceUom: '',
            pieceConversionRate: '',
            conversionRate: '',
            itemCode: '',
            warehouseLocatorId: '',
            warehouseLocatorCode: '',
            batchId: '',
            batchNum: '',
            batchProductionDate: '',
            batchExpiredDate: '',
          })
        },
        change(event, value, rowData, form) {
          console.log(arguments, '8888888')
          if (!value) {
            form.setFieldsValue({
              itemId: '',
              itemName: '',
              pieceUom: '',
              pieceConversionRate: '',
              conversionRate: '',
              itemCode: '',
              warehouseLocatorId: '',
              warehouseLocatorCode: '',
              batchId: '',
              batchNum: '',
              batchProductionDate: '',
              batchExpiredDate: '',
            })
          }
        },
      },
    },
    ...genCargoownerAuthSearchModal(
      1,
      false,
      false,
      null,
      function (modalRowData, form) {
        form.setFieldsValue({
          itemId: '',
          itemName: '',
          pieceUom: '',
          pieceConversionRate: '',
          itemCode: '',
          warehouseLocatorId: '',
          warehouseLocatorCode: '',
          batchId: '',
          batchNum: '',
          batchProductionDate: '',
          batchExpiredDate: '',
        })
      },
      { span: 6 }
    ),
    { type: 'input', name: 'itemId', label: '商品Id', hidden: true },
    {
      name: 'itemName',
      label: '商品名称',
      type: 'search',
      targetValueKey: 'itemName',
      targetClick: true,
      required: true,
      modalOpenChange: (open) => {
        console.log(that.$refs, 'openopenopenopen')
        let values = that.$refs.forms.form.getFieldsValue()
        console.log(values, 'openopenopenopen')
        if (!values.warehouseId) {
          // 仓库
          that.$message.warn('请选择仓库')
          return false
        } else if (!values.cargoOwnerId) {
          // 货主
          that.$message.warn('请选择货主')
          return false
        } else {
          return true
        }
      },
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'itemCode',
            label: '商品编码',
            span: 8,
            value: '',
          },
          {
            type: 'input',
            name: 'manufacturerLotNum',
            label: '批号',
            span: 8,
            value: '',
          },
        ],
      },
      remoteUrl: API.itemBatchInfiPage,
      tableConfig: {
        rowKey: 'batchNum',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        remoteBefore: (res, remoteParams) => {
          console.log(res, remoteParams, 'that.$refs.formsthat.$refs.forms')
          let values = that.$refs.forms.form.getFieldsValue()
          remoteParams.params = {
            ...remoteParams.params,
            cargoOwnerId: values.cargoOwnerId,
            warehouseId: values.warehouseId,
          }
          console.log(remoteParams, 'remoteParamsremoteParamsremoteParams')
          return remoteParams
        },
        columns: [
          {
            dataIndex: 'itemName',
            title: '商品名称',
            width: 250,
            key: 'itemName',
          },
          {
            dataIndex: 'itemCode',
            title: '商品编码',
            key: 'itemCode',
          },
          {
            dataIndex: 'manufacturerLotNum',
            title: '批号',
            key: 'manufacturerLotNum',
          },
          {
            dataIndex: 'batchProductionShow',
            title: '生产日期',
            key: 'batchProductionShow',
          },
          {
            dataIndex: 'batchExpiredShow',
            title: '有效期至',
            key: 'batchExpiredShow',
          },
        ],
      },
      on: {
        ok(event, value, rowData, form, name, index, tableRowData) {
          form.enableItem(['pieceAccountFact'])
          form.setFieldsValue({
            itemId: rowData[0].itemId,
            itemName: rowData[0].itemName,
            pieceUom: rowData[0].uom,
            uom: rowData[0].uom,
            pieceConversionRate: rowData[0].conversionRate,
            conversionRate: rowData[0].conversionRate,
            itemCode: rowData[0].itemCode,
            batchProductionDateShow: rowData[0].batchProductionShow,
            batchExpiredDateShow: rowData[0].batchExpiredShow,
            manufacturerLotNum: rowData[0].manufacturerLotNum,
            manufacturerName: rowData[0].manufacturerName,
            batchProductionDate: rowData[0].batchProductionDate,
            batchExpiredDate: rowData[0].batchExpiredDate,
            batchId: rowData[0].batchId,
            batchNum: rowData[0].batchNum,
          })
          let val = form.getFieldsValue()
          if (val.pieceAccountFact) {
            // pieceConversionRate
            let values = val.pieceAccountFact
            let pieceConversionRate = form.getFieldsValue().pieceConversionRate
            let pieceCRate1 = Math.floor(values / pieceConversionRate)
            let pieceCRate2 = 0
            if (pieceConversionRate) {
              pieceCRate2 = values - pieceCRate1 * pieceConversionRate
            }
            form.setFieldsValue({
              pieceCRate1: pieceCRate1,
              pieceCRate2: pieceCRate2,
            })
          }
        },
        change(event, value, rowData, form) {
          console.log(arguments, '8888888')
          if (!value) {
            form.setFieldsValue({
              itemId: '',
              itemName: '',
              pieceUom: '',
              pieceConversionRate: '',
              manufacturerLotNum: '',
              conversionRate: '',
              itemCode: '',
              warehouseLocatorId: '',
              warehouseLocatorCode: '',
              manufacturerName: '',
              batchExpiredDateShow: '',
              batchExpiredDateShow: '',
              uom: '',
              batchId: '',
              batchNum: '',
              batchProductionDate: '',
              batchExpiredDate: '',
            })
          }
        },
      },
    },
    { type: 'input', name: 'pieceUom', label: '包装单位', disabled: true },
    { type: 'input', name: 'uom', label: '包装', hidden: true },
    { type: 'input', name: 'conversionRate', label: '包装', hidden: true },
    { type: 'input', name: 'pieceConversionRate', label: '包装数', disabled: true },
    { type: 'input', name: 'pieceConversionRate', label: '商品规格', disabled: true },
    { type: 'input', name: 'itemCode', label: '商品编码', disabled: true },
    { type: 'input', name: 'manufacturerName', label: '生产厂家', disabled: true },
    // {
    //   type: 'select',
    //   name: 'code7',
    //   allowClear: false,
    //   label: '库别',
    //   value: '1',
    //   options: [
    //     { label: '零货库', value: '1' },
    //     { label: '整件库', value: '2' },
    //     { label: '退货库', value: '3' },
    //     { label: '不合格库', value: '4' }
    //   ],
    //   on: {
    //     change() {
    //       console.log(arguments, '777777777777777777')
    //     }
    //   }
    // },
    { type: 'input', name: 'whLocatorId', label: '货位Id', hidden: true },
    {
      name: 'warehouseLocatorCode',
      label: '货位',
      type: 'search',
      required: true,
      remoteUrl: API.locatorWHPage,
      targetClick: true,
      destroyOnClose: true,
      targetValueKey: 'warehouseLocatorCode',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseLocatorName',
            label: '货位名称',
            span: 6,
            value: '',
          },
          {
            type: 'input',
            name: 'warehouseLocatorCode',
            label: '货位编码',
            span: 6,
            value: '',
          },
        ],
      },
      modalOpenChange: (open) => {
        console.log(that.$refs, 'openopenopenopen')
        let values = that.$refs.forms.form.getFieldsValue()
        console.log(values, 'openopenopenopen')
        if (!values.warehouseId) {
          // 仓库
          that.$message.warn('请选择仓库')
          return false
        } else if (!values.cargoOwnerId) {
          // 货主
          that.$message.warn('请选择货主')
          return false
        } else if (!values.itemId) {
          // 商品
          that.$message.warn('请选择商品')
          return false
        } else {
          return true
        }
      },
      tableConfig: {
        rowKey: 'warehouseLocatorId',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'warehouseLocatorName', title: '货位名称', key: 'warehouseLocatorName' },
          { dataIndex: 'warehouseLocatorCode', title: '货位编码', key: 'warehouseLocatorCode' },
        ],
        remoteBefore(res, remoteParams) {
          console.log(arguments, '66666666')
          let values = that.$refs.forms.form.getFieldsValue()
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId
            remoteParams.params.itemId = values.itemId
            remoteParams.params.cargoOwnerId = values.cargoOwnerId
          } else {
            remoteParams.params = {
              warehouseId: values.warehouseId,
              itemId: values.itemId,
              cargoOwnerId: values.cargoOwnerId,
            }
          }

          return remoteParams
        },
      },
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            whLocatorId: rowData[0].warehouseLocatorId,
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              whLocatorId: '',
              warehouseLocatorCode: '',
            })
          }
        },
      },
    },
    { type: 'input', name: 'batchId', label: '批次Id', hidden: true },
    { type: 'input', name: 'batchNum', label: '批次号', hidden: true },
    // {
    //   type: 'search',
    //   name: 'batchNum',
    //   targetValueKey: 'batchNum',
    //   label: '批次号',
    //   required: true,
    //   destroyOnClose: true,
    //   targetClick: true,
    //   span: 6,
    //   value: '',
    //   toolBar: {
    //     items: [
    //       {
    //         type: 'input',
    //         name: 'batchNum',
    //         label: '批次号',
    //         span: 8,
    //         value: ''
    //       },
    //       {
    //         type: 'input',
    //         name: 'batchId',
    //         label: '批次Id',
    //         span: 8,
    //         value: ''
    //       }
    //     ]
    //   },
    //   modalOpenChange: open => {
    //     console.log(that.$refs, 'openopenopenopen')
    //     let values = that.$refs.forms.form.getFieldsValue()
    //     console.log(values, 'openopenopenopen')
    //     if (!values.warehouseId) {
    //       // 仓库
    //       that.$message.warn('请选择仓库')
    //       return false
    //     } else if (!values.cargoOwnerId) {
    //       // 货主
    //       that.$message.warn('请选择货主')
    //       return false
    //     } else if (!values.itemId) {
    //       // 商品
    //       that.$message.warn('请选择商品')
    //       return false
    //     } else {
    //       return true
    //     }
    //   },
    //   tableConfig: {
    //     rowKey: 'batchId',
    //     rowSelectType: 1,
    //     hasRowSelectCheck: true,
    //     remoteBefore: (res, remoteParams, form, val1, val2) => {
    //       console.log('33333333')
    //       let values = that.$refs.forms.form.getFieldsValue()
    //       console.log(remoteParams, 'remoteParamsremoteParamsremoteParams')
    //       remoteParams.params = {
    //         ...remoteParams.params,
    //         cargoOwnerId: values.cargoOwnerId,
    //         warehouseId: values.warehouseId,
    //         itemId: values.itemId
    //       }
    //       return remoteParams
    //     },

    //     columns: [
    //       { dataIndex: 'batchNum', title: '批次号', key: 'batchNum' },
    //       { dataIndex: 'batchId', title: '批次Id', key: 'batchId' },
    //       { dataIndex: 'batchProductionDate', title: '生产日期', key: 'batchProductionDate' },
    //       { dataIndex: 'productionPlace', title: '产地', key: 'productionPlace' }
    //     ]
    //   },
    //   value: '',
    //   remoteUrl: API.batchPage,
    //   on: {
    //     ok(event, value, rowData, form) {
    //       console.log(rowData, 'rowDatarowData')
    //       form.setFieldsValue({
    //         batchId: rowData[0].batchId,
    //         batchProductionDate: rowData[0].batchProductionDate,
    //         batchExpiredDate: rowData[0].batchExpiredDate
    //       })
    //     },
    //     change(event, value, rowData, form) {
    //       if (!value) {
    //         form.setFieldsValue({
    //           batchId: '',
    //           batchNum: '',
    //           batchProductionDate: '',
    //           batchExpiredDate: ''
    //         })
    //       }
    //     }
    //   }
    // },
    { type: 'input', name: 'manufacturerLotNum', label: '批号', disabled: true },
    { type: 'input', name: 'batchProductionDateShow', label: '生产日期', disabled: true },
    { type: 'input', name: 'batchExpiredDateShow', label: '有效期至', disabled: true },
    { type: 'input', name: 'batchProductionDate', label: '生产日期', hidden: true },
    { type: 'input', name: 'batchExpiredDate', label: '有效期至', hidden: true },
    { type: 'input', name: 'quantity', label: '调整数量', hidden: true },
    {
      type: 'number',
      name: 'pieceAccountFact',
      disabled: true,
      label: '实盘数量',
      on: {
        change(values, form) {
          console.log(arguments, 'argumentsargumentsarguments')
          // pieceConversionRate
          let pieceConversionRate = form.getFieldsValue().pieceConversionRate
          let pieceCRate1 = Math.floor(values / pieceConversionRate)
          let pieceCRate2 = 0
          if (pieceConversionRate) {
            pieceCRate2 = values - pieceCRate1 * pieceConversionRate
          }
          form.setFieldsValue({
            pieceCRate1: pieceCRate1,
            pieceCRate2: pieceCRate2,
            quantity: values,
          })
        },
      },
    },
    // { type: 'input', name: 'code12', label: '托盘条码', disabled: true },
    { type: 'input', name: 'pieceCRate2', label: '零散数', disabled: true },
    { type: 'input', name: 'pieceCRate1', label: '件数', disabled: true },
  ]
}

export default {
  name: 'check',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      items: items(this),
      confirmLoading: false,
      visibleData: false,
    }
  },
  created() {
    // this.visibleData = this.visible
  },
  watch: {
    visible(newVal) {
      // this.visibleData = newVal    PIECE_QUANTITY
    },
  },
  methods: {
    handleOk() {
      this.$refs.forms.form.validateFields((err, values) => {
        console.log(err, values)
        if (!err) {
          values.pieceAccount = 0
          values.isAdd = 1 // 是否新增
          this.confirmLoading = true
          adjustAdd(values).then((res) => {
            this.confirmLoading = false
            console.log(res)
            if (res.success) {
              this.$emit('successBtn', false)
            } else {
              this.$exception(res)
            }
          })
        }
      })
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.$emit('cancelBtn', false)
    },
  },
}
</script>

<style>
.gridClass {
  margin-bottom: 12px;
}
</style>
