<template>
  <div>
    <detail-page
      v-show="!detialShow"
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableAutoLoad="false"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :items="items"
      :ajaxId="ajaxId"
      @print="print"
      @changeClick="changeClick"
      @add="add"
      @selectChange="selectChange"
      ref="detial"
    >
    </detail-page>
  </div>
</template>

<script>
import { toPrintServer, tempType } from "@/views/kzyWMS/utils/printServer.js";
import DetailPage from '../components/commonTable'
import { getMovePrintData } from '../api/api'
import API from '../api/default'
import { sorter } from '../../utils/sort'

import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from '../../SearchModal'
import { upcloud_base_wh_operate } 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, null, { span: 8 }),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, {
      span: 8,
    }),

    {
      type: 'select',
      name: 'moveBillType',
      label: '来源类型',
      value: '',
      span: 8,
      options: [
        { label: '全部', value: '' },
        { label: 'WMS', value: 'WMS' },
        { label: 'GSP', value: 'GSP' },
        { label: 'ERP', value: 'ERP' },
        { label: 'SC', value: 'SC' },
      ],
    },
    { type: 'input', name: 'warehouseWorkerId', label: '操作员Id', hidden: true },
    {
      type: 'search',
      name: 'warehouseWorkerName',
      label: '操作员',
      span: 8,
      destroyOnClose: true,
      targetValueKey: 'locname',
      targetClick: true,
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'locname',
            label: '操作员',
            span: 8,
            value: '',
          },
          {
            type: 'input',
            name: 'jobnumber',
            label: '工号',
            span: 8,
            value: '',
          },
        ],
      },
      tableConfig: {
        rowKey: 'id',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'locname', title: '操作员', key: 'locname' },
          { dataIndex: 'jobnumber', title: '工号', key: 'jobnumber' },
          { dataIndex: 'id', title: '员工ID', key: 'id' },
        ],
      },
      value: '',
      remoteUrl: API.employeePage,
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, 'rowDatarowData')
          form.setFieldsValue({
            warehouseWorkerId: rowData[0].id,
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              creator: '',
              warehouseWorkerId: '',
              warehouseWorkerName: '',
            })
          }
        },
      },
    },
    {
      type: 'row',
      label: '日期',
      span: 8,
      list: [
        {
          type: 'date',
          name: 'start',
          placeholder: '请输入开始日期',
          value: that.startTime,
          format: 'YYYY-MM-DD',
          style: 'padding-right:5px;',
        },
        {
          type: 'date',
          name: 'end',
          placeholder: '请输入结束日期',
          format: 'YYYY-MM-DD',
          value: that.endTime,
        },
      ],
    },
  ]
}
const columns = [
  { dataIndex: 'itemCode', title: '商品编号', key: 'itemCode', width: 150 },
  { dataIndex: 'itemName', title: '商品名称', key: 'itemName', width: 100 },
  { dataIndex: 'moveDate', title: '移位时间', key: 'moveDate', type: 'txt', width: 160 },
  { dataIndex: 'manufacturerLotNum', title: '批号', key: 'manufacturerLotNum', width: 180 },
  { dataIndex: 'uom', title: '包装单位', key: 'uom', width: 100 },
  { dataIndex: 'conversionRate', title: '包装数', key: 'conversionRate', width: 100 },
  // { dataIndex: 'code13', title: '商品大类', key: 'code13' },
  // { dataIndex: 'code14', title: '是否器械', key: 'code14' },
  { dataIndex: 'outLocatorId', title: '移出货位ID', key: 'outLocatorId', width: 180 },
  { dataIndex: 'outWarehouseAreaCategory', title: '移出库别编码', key: 'outWarehouseAreaCategory', width: 100 },
  // { dataIndex: 'code16', title: '注册证号', key: 'code16' },
  { dataIndex: 'inLocatorId', title: '移入货位ID', key: 'inLocatorId', width: 180 },
  { dataIndex: 'inWarehouseAreaCategory', title: '移入库别编码', key: 'inWarehouseAreaCategory', width: 100 },
  { dataIndex: 'batchProductionDate', title: '生产日期', key: 'batchProductionDate', width: 100 },
  { dataIndex: 'caseQuantity', title: '件数', key: 'caseQuantity', width: 100 },
  { dataIndex: 'oddPieceQuantity', title: '零散数', key: 'oddPieceQuantity', width: 100 },
  { dataIndex: 'warehouseWorkerName', title: '操作员', key: 'warehouseWorkerName', width: 100 },
  { dataIndex: 'manufacturerName', title: '生产厂家', key: 'manufacturerName', width: 100 },
]

export default {
  components: { DetailPage },
  data() {
    return {
      items: items(this),
      columns: null,
      title: '移位查询',
      tableRowKey: 'autoBillNum',
      selectedRowKeys: [],
      selectedRows: [],
      startTime: '',
      endTime: '',
      detialRecord: {},
      addStatus: false,
      detialShow: false,
      ajaxId: '', // 移库类型 1 移库 2移位
      tableRemoteUrl: {
        url: upcloud_base_wh_operate + '/move/v1/list',
        method: 'get',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {
          subBillType: '2',
          statusList: 'W00',
        },
      },
      btnOptions: [
          {
          name: '打印',
          icon: '',
          action: 'print',
        },
      ],
    }
  },
  created() {
    this.columns = sorter(columns)
  },
  mounted() {
    this.getAuthBycargoOwner()
    console.log(this.$refs, 'this.$refs.this.$refs.this.$refs.')
    this.$refs.detial.$refs.forms.form.setFieldsValue({
      // 最近一周时间设置
      start: this.getDay(-7),
      end: this.getDay(0),
      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
        }
      })
    },
    getDay(day) {
      let today = new Date()
      var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday_milliseconds) //注意，这行是关键代码
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length == 1) {
        m = '0' + month
      }
      return m
    },
    getDay(day) {
      let today = new Date()
      var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday_milliseconds) //注意，这行是关键代码
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length == 1) {
        m = '0' + month
      }
      return m
    },
    changeClick(record) {
      // 编辑
      this.detialShow = true
      this.detialRecord = record
      console.log(record)
    },
    requestUrl(val) {
      this.detialShow = val
    },
    add() {
      this.addStatus = true
    },
    cancelBtn(val) {
      this.addStatus = false
    },
    successBtn() {
      this.addStatus = false
      this.$refs.detial.$refs.table.reload()
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      console.log(selectedRowKeys, selectedRows, 'selectedRowKeys, selectedRows')
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },


    //打印
    print() {
      console.log(this.$store.getters.userInfo)
      console.log(this.$refs.detial.$refs.forms.form.getFieldsValue())
      if (this.selectedRows.length < 1) {
        this.$message.error('请至少勾选一条子表数据')
        return
      }

      let formdata = this.$refs.detial.$refs.forms.form.getFieldsValue()
      let par = {
            warehouseName: formdata.warehouseName,
            cargoOwnerName: formdata.cargoOwnerName,
            dtls: this.selectedRows
      }
          

          getMovePrintData(par).then((res) => {
            if (!res.success) {
              this.$exception(res);
              return;
            }
            // const printParms = {
            //     consignorId: this.selectedMainItem.mtlOwnerId,
            // }
            const warehouseId = this.$store.getters.warehouse.warehouseId;
            //调用打印方法()
            toPrintServer(res.data.data, warehouseId, tempType.YW_KP).then((res) => {
              if (!res.success) {
                this.$exception(res);
                return;
              }
              this.$message.success(res.message);
            });
          });



    },
  },
}
</script>

<style></style>
