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

<script>
import DetailPage from '../../../../bound/components/searchTable'
import { adjustExamine } from './api/index'
import API from './api/index'
import {
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
  genWarehouseLocatorSearchModal,
} from '../../../../SearchModal'
import { sorter } from '../../../../utils/sort'
import { SERVICE_DATA_API_BASE } from '@/config/serviceSettings'
import { getCargoOwner } from '../../../../utils/index'

let warehouseId = ''
let bldgId = ''
let warehouseRoomId = ''
let warehouseAreaId = ''
let warehouseLocatorId = ''
// inv_stockcheck_h_checkgroup2
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: '',
          })
          warehouseId = ''
          bldgId = ''
          warehouseRoomId = ''
          warehouseAreaId = ''
          warehouseLocatorId = ''
        }
      },
      { span: 8 }
    ),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, {
      span: 8,
    }),
    {
      type: 'select',
      name: 'adjustType',
      label: '损溢来源',
      span: 8,
      allowClear: false,
      value: '0',
      options: [
        { label: '计划盘点', value: '0' },
        { label: '无计划盘点', value: '1' },
        // { label: 'WMS调整', value: '2' }
      ],
    },
    {
      type: 'select',
      name: 'status',
      span: 8,
      label: '状态',
      allowClear: false,
      value: '0',
      options: [
        // { label: '刚生成(WMS二次审核)', value: '0' },
        { label: '刚生成(WMS一次审核)', value: '0' },
        // { label: '已上传', value: '10' },
        // { label: 'ERP已审核', value: '30' },
        // { label: 'ERP驳回', value: '40' }
        // { label: 'WMS记账完毕', value: '50' },
        // { label: '作废成功', value: '60' }
      ],
    },
    { type: 'input', name: 'itemId', label: '商品Id', hidden: true },
    {
      name: 'itemName',
      label: '商品名称',
      type: 'search',
      targetValueKey: 'itemName',
      targetClick: true,
      modalOpenChange: (open) => {
        let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
        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) => {
          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 : '',
              // itemCondition: remoteParams.data.itemCondition
            },
          }
          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) {
          form.setFieldsValue({
            itemId: rowData[0].itemId,
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              itemId: '',
            })
          }
        },
      },
    },
    { type: 'input', name: 'adjustHeaderId', label: '单据编号' },
    { 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) => {
        let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
        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) {
          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
        },
      },
      value: '',
      remoteUrl: API.locatorPage,
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            warehouseLocatorId: rowData[0].warehouseLocatorId,
          })
        },
        change(event, value, options, form) {
          if (!value) {
            form.setFieldsValue({ warehouseLocatorId: '' })
          }
        },
      },
    },
    {
      span: 24,
      type: 'buttonGroup',
      style: { textAlign: 'right' },
      options: [
        {
          value: '查询',
          icon: 'search',
          btnType: 'primary',
          style: 'margin-left:30px;margin-right:10px;',
          htmlType: 'submit',
          on: {
            click: (event, form) => {
              form.validateFields((error, values) => {
                if (!error) {
                  that.$refs.detial.reload(values)
                }
              })
            },
          },
        },
        {
          value: '重置',
          name: 'ccz',
          icon: 'redo',
          on: {
            click(event, form) {
              form.resetFields()
            },
          },
        },
      ],
    },
  ]
}
const columns = [
  // { dataIndex: 'code1', title: '库别', key: 'code1' },
  { dataIndex: 'adjustHeaderId', title: '单据编号', key: 'adjustHeaderId' },
  { dataIndex: 'warehouseLocatorCode', title: '货位编码', key: 'warehouseLocatorCode' },
  { dataIndex: 'itemCode', title: '商品编号', key: 'itemCode' },
  { dataIndex: 'itemName', title: '商品名称', key: 'itemName' },
  // { dataIndex: 'code5', title: '批号', key: 'code5' },
  { dataIndex: 'pieceAccount', title: '库存数量', key: 'pieceAccount' },
  { dataIndex: 'quantity', title: '调整数量', key: 'quantity' },
  { dataIndex: 'pieceAccountFact', title: '盘点数量', key: 'pieceAccountFact' },
  // { dataIndex: 'code8', title: '零散数', key: 'code8' },
  { dataIndex: 'conversionRate', title: '包装数量', key: 'conversionRate' },
  // { dataIndex: 'code13', title: '商品大类', key: 'code13' },
  // { dataIndex: 'code14', title: '是否器械', key: 'code14' },
  { dataIndex: 'manufacturerLotNum', title: '批号', key: 'manufacturerLotNum' },
  // { dataIndex: 'code16', title: '注册证号', key: 'code16' },
  { dataIndex: 'cargoOwnerName', title: '货主名称', key: 'cargoOwnerName' },
  { dataIndex: 'batchProductionDateShow', title: '生产日期', key: 'batchProductionDateShow' },
  { dataIndex: 'batchExpiredDateShow', title: '有效期', key: 'batchExpiredDateShow' },
  { dataIndex: 'rcvQuantity', title: '入库已分配数量', key: 'rcvQuantity' },
  { dataIndex: 'outQuantity', title: '出库已分配数量', key: 'outQuantity' },
  { dataIndex: 'repQuantity', title: '补货已分配数量', key: 'repQuantity' },
]

export default {
  components: { DetailPage },
  data() {
    return {
      items: items(this),
      columns: null,
      title: '损溢单审核',
      tableRowKey: 'adjustLineId',
      selectedRowKeys: [],
      selectedRows: [],
      detialRecord: {},
      ajaxId: '580759567772815360',
      tableRemoteUrl: {
        url: SERVICE_DATA_API_BASE + '/search/page',
        method: 'post',
        headers: {
          'Content-Type': 'application/JSON',
        },
      },
      btnOptions: [
        {
          name: '审核通过',
          icon: '',
          action: 'save',
        },
        {
          name: '审核驳回',
          icon: '',
          action: 'del',
        },
      ],
    }
  },
  created() {
    this.columns = sorter(columns)
    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,
    })
  },
  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,
            })
          })
        }
        if (!res.success) {
          this.$exception(res)
          return
        }
      })
    },
    save() {
      // 审核通过
      this.adjustExamine('0')
    },
    del() {
      // 驳回
      this.adjustExamine('1')
    },
    adjustExamine(status) {
      // 审核
      if (this.selectedRowKeys.length < 1) return this.$message.warn('请选择审核商品')
      let params = {
        status: status,
        adjustLineIds: this.selectedRowKeys.join(','),
      }
      adjustExamine(params).then((res) => {
        if (res.success) {
          this.$message.success(status == '1' ? '成功驳回！' : '成功通过！')
          this.$refs.detial.$refs.table.reload()
        } else {
          this.$exception(res)
        }
      })
    },

    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
  },
}
</script>

<style></style>
