<template>
  <div>
    <detail-page
      v-show="!detialShow"
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :actionOption="true"
      :tableAutoLoad="false"
      :queryStatus="false"
      :items="items"
      :ajaxId="ajaxId"
      @changeClick="changeClick"
      @add="add"
      @selectChange="selectChange"
      ref="detial"
    >
    </detail-page>
    <!-- <p class="footDes">注释：计划盘点与无计划盘点需要上传ERP，WMS调整不上传ERP</p> -->
    <detialinStock v-if="detialShow" @requestUrl="requestUrl" :detialRecord="detialRecord" />
    <addStock :visible="addStatus" @successBtn="successBtn" @cancelBtn="cancelBtn"></addStock>
  </div>
</template>

<script>
import DetailPage from '../../../../bound/components/searchTable'
import detialinStock from './components/detialinStock'
import addStock from './components/addStock'
import API from './api/index'
import { remote } from './api/index'
import { sorter } from '../../../../utils/sort'
import { SERVICE_DATA_API_BASE } from '@/config/serviceSettings'

import {
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
  genWarehouseLocatorSearchModal,
} from '../../../../SearchModal'
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({
            warehouseLocatorId: '',
            warehouseLocatorCode: '',
            warehouseLocatorName: '',
          })
        } else {
          form.setFieldsValue({
            warehouseId: '',
            warehouseLocatorId: '',
            warehouseLocatorCode: '',
            warehouseLocatorName: '',
          })
          warehouseId = ''
          warehouseLocatorId = ''
          warehouseLocatorId = ''
        }
      },
      { span: 8 }
    ),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, {
      span: 8,
    }),

    {
      type: 'select',
      name: 'adjustType',
      label: '损溢来源',
      value: '1',
      allowClear: false,
      options: [
        // { label: '计划盘点', value: '0' },
        { label: '无计划盘点', value: '1' },
        // { label: 'WMS调整', value: '2' }
      ],
    },
    { type: 'input', name: 'itemId', label: '商品Id', hidden: true },
    {
      name: 'itemName',
      label: '商品名称',
      type: 'search',
      targetValueKey: 'itemName',
      targetClick: true,
      remoteUrl: API.commonUrl,
      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',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        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,
              lotStatus: '5',
              itemCode: remoteParams.data.itemCode ? remoteParams.data.itemCode : '',
              itemName: remoteParams.data.itemName ? remoteParams.data.itemName : '',
            },
          }
          return remoteParams
        },
        columns: [
          {
            dataIndex: 'itemName',
            title: '商品名称',
            key: 'itemName',
          },
          {
            dataIndex: 'itemCode',
            title: '商品编码',
            key: 'itemCode',
          },
        ],
      },
      on: {
        ok(event, value, rowData, form, name, index, tableRowData) {
          form.setFieldsValue({
            itemId: rowData[0].itemId,
            warehouseLocatorId: '',
            warehouseLocatorName: '',
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              itemId: '',
            })
          }
        },
      },
    },
    { type: 'input', name: 'warehouseLocatorId', label: '货位ID', hidden: true },
    { type: 'input', name: 'lotStatus', label: 'lotStatus', hidden: true,value:'5' },
    {
      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()
          console.log(values,'valuesvaluesvalues');
          remoteParams.data = {
            ...remoteParams.data,
            num: 'locator_stock_list',
            params: {
              cargoOwnerId: values.cargoOwnerId,
              warehouseId: values.warehouseId,
              warehouseLocatorCode:remoteParams.data.warehouseLocatorCode,
              warehouseLocatorName:remoteParams.data.warehouseLocatorName,
            },
          }
          return remoteParams
        },
      },
      value: '',
      remoteUrl: API.commonUrl,
      on: {
        ok(event, value, rowData, form) {
          form.setFieldsValue({
            warehouseLocatorId: rowData[0].warehouseLocatorId,
            itemId: '',
            itemName: '',
          })
        },
        change(event, value, options, form) {
          if (!value) {
            form.setFieldsValue({ warehouseLocatorId: '' })
          }
        },
      },
    },
    {
      span: 8,
      type: 'buttonGroup',
      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: 'warehouseLocatorCode', title: '货位编码', key: 'warehouseLocatorCode' },
  { dataIndex: 'itemCode', title: '商品编号', key: 'itemCode' },
  { dataIndex: 'itemName', title: '商品名称', key: 'itemName' },
  // { dataIndex: 'code5', title: '批号', key: 'code5' },
  // { dataIndex: 'code6', title: '库存数量', key: 'code6' },
  { dataIndex: 'availablequantity', title: '可用库存', key: 'availablequantity' },
  // { dataIndex: 'code7', title: '件数', key: 'code7' },
  // { dataIndex: 'code8', title: '零散数', key: 'code8' },
  { dataIndex: 'rcvQuantity', title: '入库已分配数量', key: 'rcvQuantity' },
  { dataIndex: 'outQuantity', title: '出库已分配数量', key: 'outQuantity' },
  { dataIndex: 'repQuantity', title: '补货已分配数量', key: 'repQuantity' },
  { 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: 'manufacturerName', title: '产地', key: 'manufacturerName' },
  {
    dataIndex: 'action',
    title: '操作',
    key: 'action',
    width: 180,
    fixed: 'right',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  components: { DetailPage, detialinStock, addStock },
  data() {
    return {
      items: items(this),
      columns: null,
      title: '损溢单录入',
      tableRowKey: 'id',
      selectedRowKeys: [],
      selectedRows: [],
      detialRecord: {},
      addStatus: false,
      detialShow: false,
      // ajaxId: '580759567772815360',
      ajaxId: 'item-instock-list',
      tableRemoteUrl: {
        url: SERVICE_DATA_API_BASE + '/search/page',
        method: 'post',
        headers: {
          'Content-Type': 'application/JSON',
        },
      },
      btnOptions: [
        {
          name: '新增',
          icon: '',
          action: 'add',
        },
      ],
    }
  },
  created() {
    this.columns = sorter(columns)
  },
  mounted() {
    this.getAuthBycargoOwner()
    // 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
        }
      })
    },
    changeClick(record) {
      // 编辑
      this.detialShow = true
      this.detialRecord = record
    },
    requestUrl(val) {
      this.detialShow = val
      this.$refs.detial.$refs.table.reload()
    },
    add() {
      this.addStatus = true
    },
    cancelBtn(val) {
      this.addStatus = false
    },
    successBtn() {
      this.addStatus = false
      this.$refs.detial.$refs.table.reload()
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
  },
}
</script>

<style>
.footDes {
  font-size: 16px;
}
</style>
