<template>
  <div>
    <detail-page
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :tableAutoLoad="false"
      :items="items"
      :ajaxId="ajaxId"
      @save="save"
      @selectChange="selectChange"
      ref="detial"
    />
  </div>
</template>

<script>
import searchTable from '../../components/searchTable'
// import { detailConfig } from './config/planCheck'
import API from '../../api/default'
import { sorter } from '../../../utils/sort'
import { diffOption } from '@/views/kzyWMS/utils/index.js'

// inv_stockcheck_h_checkgroup2
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from '../../../SearchModal'
import { SERVICE_DATA_API_BASE } from '@/config/serviceSettings'
import { getCargoOwner } from '../../../utils/index'
let warehouseId = ''
let bldgId = ''
let warehouseRoomId = ''
let warehouseAreaId = ''
let warehouseLocatorId = ''
const items = function (that) {
  return [
    ...genWarehouseAuthSearchModal(
      1,
      true,
      false,
      null,
      function (modalRowData, form) {
        if (modalRowData) {
          warehouseId = modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId
          form.setFieldsValue({
            warehouseId: modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId,
            warehouseLocatorId: '',
            warehouseLocatorName: '',
          })
        } else {
          form.setFieldsValue({
            warehouseId: '',
            warehouseLocatorId: '',
            warehouseLocatorName: '',
          })
          warehouseLocatorName = ''
          warehouseId = ''
          warehouseLocatorId = ''
        }
      },
      { span: 8 }
    ),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, {
      span: 8,
    }),

    { type: 'input', name: 'itemId', label: '商品Id', hidden: true },
    {
      name: 'itemName',
      label: '商品名称',
      type: 'search',
      targetValueKey: 'itemName',
      targetClick: true,
      modalOpenChange: (open) => {
        console.log(that.$refs, 'openopenopenopen')
        let values = that.$refs.detial.$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: 'itemName',
            label: '商品名称',
            span: 8,
            value: '',
          },
          {
            type: 'input',
            name: 'itemCode',
            label: '商品编码',
            span: 8,
            value: '',
          },
        ],
      },
      tableConfig: {
        rowKey: 'itemId',
        remoteUrl: (res) => {
          return {
            ...API.commonUrl,
          }
        },
        remoteParams: {
          cargoOwnerId: that.cargoOwnerId,
          warehouseId: that.warehouseId,
        },
        remoteBefore: (res, remoteParams, form, val1, val2) => {
          console.log(arguments, '33333333')
          let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
          remoteParams.data = {
            ...remoteParams.data,
            num: 'item-storage',
            params: {
              cargoOwnerId: values.cargoOwnerId,
              warehouseId: values.warehouseId,
              itemCode: remoteParams.data.itemCode ? remoteParams.data.itemCode : '',
              itemName: remoteParams.data.itemName ? remoteParams.data.itemName : '',
            },
          }
          console.log(remoteParams, 'remoteParamsremoteParamsremoteParams')
          return remoteParams
        },
        columns: [
          {
            dataIndex: 'itemName',
            title: '商品名称',
            key: 'itemName',
          },
          {
            dataIndex: 'itemCode',
            title: '商品编码',
            key: 'itemCode',
          },
          {
            dataIndex: 'itemId',
            title: '商品id',
            key: 'itemId',
          },
        ],
      },
      on: {
        ok(event, value, rowData, form, name, index, tableRowData) {
          console.log(arguments, '6666666666')
          form.setFieldsValue({
            itemId: rowData[0].itemId,
          })
        },
        change(event, value, rowData, form) {
          console.log(arguments, '8888888')
          if (!value) {
            form.setFieldsValue({
              itemId: '',
            })
          }
        },
      },
    },
    { type: 'input', name: 'warehouseLocatorId', label: '货位ID', hidden: true },
    {
      type: 'search',
      name: 'warehouseLocatorName',
      label: '货位',
      targetValueKey: 'warehouseLocatorName',
      destroyOnClose: true,
      targetClick: true,
      span: 8,
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseLocatorName',
            label: '货位名称',
            span: 8,
            value: '',
          },
          {
            type: 'input',
            name: 'warehouseLocatorCode',
            label: '货位编码',
            span: 8,
            value: '',
          },
        ],
      },
      modalOpenChange: (open) => {
        console.log(that.$refs, 'openopenopenopen')
        let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
        console.log(values, 'openopenopenopen')
        if (!values.warehouseId) {
          // 仓库
          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' },
          { dataIndex: 'warehouseLocatorId', title: '货位ID', key: 'warehouseLocatorId' },
        ],
        remoteBefore(res, remoteParams) {
          console.log(arguments, '66666666')
          let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId
          } else {
            remoteParams.params = { warehouseId: values.warehouseId }
          }

          return remoteParams
        },
      },
      remoteUrl: API.locatorWHPage,
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            warehouseLocatorId: rowData[0].warehouseLocatorId,
          })
        },
        change(event, value, options, form) {
          if (!value) {
            form.setFieldsValue({ warehouseLocatorId: '' })
          }
        },
      },
    },
    { type: 'input', name: 'manufacturerLotNum', label: '批号' },
  ]
}
const columns = function (self) {
  return [
    { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName' },
    { dataIndex: 'cargoOwnerName', title: '货主名称', key: 'cargoOwnerName' },
    { dataIndex: 'itemCode', title: '商品编号', key: 'itemCode' },
    { dataIndex: 'itemName', title: '商品名称', key: 'itemName' },
    { dataIndex: 'manufacturerLotNum', title: '批号', key: 'manufacturerLotNum' },
    { dataIndex: 'warehouseLocatorCode', title: '货位编码', key: 'warehouseLocatorCode' },
    { dataIndex: 'quantity', title: '可用库存数量', key: 'quantity' },
    { dataIndex: 'oddCaseQuantity', title: '可锁定件数', key: 'oddCaseQuantity' },
    { dataIndex: 'oddPieceQuantity', title: '可锁零散数', key: 'oddPieceQuantity' },
    { dataIndex: 'batchProductionDate', title: '生产日期', key: 'batchProductionDate' },
    { dataIndex: 'batchExpiredDate', title: '有效期至', key: 'batchExpiredDate' },
    { dataIndex: 'batchNum', title: '批次', key: 'batchNum' },
    {
      title: '操作',
      key: 'operate',
      align: 'center',
      fixed: 'right',
      width: '175px',
      scopedSlots: { customRender: 'action' },
    },
  ]
}

export default {
  components: { searchTable },
  data() {
    return {
      items: items(this),
      columns: columns(this),
      title: '提取库存',
      tableRowKey: 'id',
      selectedRowKeys: [],
      ajaxId: '581848808753860608',
      tableRemoteUrl: {
        url: SERVICE_DATA_API_BASE + '/search/page',
        method: 'post',
        headers: {
          'Content-Type': 'application/JSON',
        },
        ajaxData: {
          status: 'N',
        },
      },
      btnOptions: [
        // {
        //   name: '锁定',
        //   icon: '',
        //   action: 'save',
        // },
      ],
    }
  },
  created() {
    console.log()
    diffOption(columns(this)).then((res) => {
      this.columns = sorter(res)
    })
    this.getAuthBycargoOwner()
  },
  mounted() {
    // warehouseId = this.$store.state.user.warehouse.warehouseId
    this.$refs.detial.$refs.forms.form.setFieldsValue({
      warehouseId: this.$store.state.user.warehouse.warehouseId,
      warehouseName: this.$store.state.user.warehouse.warehouseName,
    })
    console.log(this.$refs, 'this.$refs.remoteParams')
  },
  methods: {
    async getAuthBycargoOwner() {
      await getCargoOwner().then((res) => {
        if (res.success) {
          this.$nextTick(() => {
            this.$refs.detial.$refs.forms.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerName: res.data.list[0].companyName,
              cargoOwnerCode: res.data.list[0].companyCode,
            })
            this.$refs.detial.remoteParams = { ...this.$refs.detial.remoteParams }
          })
        }
        if (!res.success) {
          this.$exception(res)
          return
        }
      })
    },
    save() {
      console.log(this.$refs.detial.$refs.forms.form.getFieldsValue(), '333333')
      // 生成计划
      if (this.selectedRowKeys.length < 1) return this.$message.warn('请选择锁定商品!')
      return this.selectedRows
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>

<style></style>
