<template>
  <div>
    <detailPage
      v-show="!detialShow"
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :items="items"
      :ajaxId="ajaxId"
      :tableAutoLoad="false"
      @cancelDel="cancelDel"
      @checkOk="checkOk"
      @changeClick="changeClick"
      @selectChange="selectChange"
      ref="detial"
    />
    <p-modal
      title="审核意见"
      :visible="visible"
      :draggable="true"
      @ok="handleOk"
      @cancel="
        visible = false
        textareaValue = ''
      "
    >
      <p-textarea placeholder="请输入审核意见" :value="textareaValue" :limit="200" :rows="6" @change="onChange" />
    </p-modal>
  </div>
</template>

<script>
import detailPage from '../components/commonTable'
import { moveAudit, moveCancel } from '../api/api'
import API from '../api/default'
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from '@/views/kzyWMS/SearchModal'
import { sorter } from '../../utils/sort'
import { upcloud_base_wh_operate } from '@/config/serviceSettings'
import { getCargoOwner } from '../../utils/index'
const items = function (that) {
  let grid = {
    labelCol: { span: 8 },
    span: 8,
  }
  return [
    ...genWarehouseAuthSearchModal(1, true, false, null, null, grid),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid),
    {
      type: 'select',
      name: 'moveOutBldg',
      label: '移出建筑',
      required: true,
      defaultValueIndex: 0,
      destroyOptions: true,
      value: '',
      autoLoad: true,
      remoteUrl: API.wmsWhBldgPage,
      replaceFields: {
        label: 'bldgName',
        value: 'bldgId',
      },
      remoteBefore(res, remoteParams) {
        // console.log(arguments, 'warehouseIdwarehouseId')
        that.$nextTick(() => {
          let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId
          } else {
            remoteParams.params = { warehouseId: values.warehouseId }
          }
        })
      },
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, 'rowDatarowData')
        },
      },
    },
    {
      type: 'select',
      name: 'moveInBldg',
      label: '移入建筑',
      required: true,
      defaultValueIndex: 0,
      destroyOptions: true,
      value: '',
      autoLoad: true,
      remoteUrl: API.wmsWhBldgPage,
      replaceFields: {
        label: 'bldgName',
        value: 'bldgId',
      },
      remoteBefore(res, remoteParams) {
        // console.log(arguments, 'warehouseIdwarehouseId')
        that.$nextTick(() => {
          let values = that.$refs.detial.$refs.forms.form.getFieldsValue()
          if (remoteParams.params) {
            remoteParams.params.warehouseId = values.warehouseId
          } else {
            remoteParams.params = { warehouseId: values.warehouseId }
          }
        })
      },
      on: {
        ok(event, value, rowData, form) {
          console.log(rowData, 'rowDatarowData')
        },
      },
    },
    {
      type: 'input',
      name: 'autoBillNum',
      label: '自动单号',
    },
    // {
    //   type: 'input',
    //   name: 'warehouseName2',
    //   label: '手动单号'
    // },
    {
      type: 'row',
      label: '单据日期',
      name: 'ff',
      list: [
        {
          type: 'date',
          name: 'start',
          style: { paddingRight: '5px' },
          format: 'YYYY-MM-DD',
          placeholder: '请输入开始日期',
          value: '',
        },
        {
          style: { paddingLeft: '5px' },
          type: 'date',
          name: 'end',
          placeholder: '请输入结束日期',
          format: 'YYYY-MM-DD',
          value: '',
        },
      ],
    },
    {
      type: 'select',
      name: 'moveBillType',
      label: '来源类型',
      value: '',
      defaultValueIndex: 0,
      options: [
        // { label: '全部', value: '' },
        { label: 'WMS', value: 'WMS' },
        { label: 'GSP', value: 'GSP' },
        { label: 'ERP', value: 'ERP' },
        { label: 'SC', value: 'SC' },
        { label: 'CHECK', value: 'CHECK' },
      ],
    },
  ]
}
const columns = [
  { dataIndex: 'autoBillNum', title: '自动单号', width: 150 },
  { dataIndex: 'cargoOwnerName', title: '货主', width: 180 },
  { dataIndex: 'moveOutBldgName', title: '移出建筑', width: 100 },
  { dataIndex: 'moveInBldgName', title: '移入建筑', width: 100 },
  { dataIndex: 'moveBillType', title: '来源类型', width: 80 },
  { dataIndex: 'moveDate', title: '单据日期', width: 150 },
  { dataIndex: 'itemCode', title: '商品编码', width: 100 },
  { dataIndex: 'itemName', title: '商品名称', width: 120 },
  { dataIndex: 'outLocatorCode', title: '移出货位编码', width: 100 },
  { dataIndex: 'outContainerCode', title: '移出托盘号', type: 'edit' },
  { dataIndex: 'outWarehouseAreaCategory', title: '移出库别编码', width: 100 },
  { dataIndex: 'outGoodsCaseCode', title: '移出货格号', width: 100 },
  { dataIndex: 'inLocatorCode', title: '移入货位编码', width: 100 },
  { dataIndex: 'inContainerCode', title: '移入托盘号', type: 'edit' },
  { dataIndex: 'inWarehouseAreaCategory', title: '移入库别编码', width: 100 },
  { dataIndex: 'pieceQuantity', title: '移动数量', width: 100 },
  { dataIndex: 'uom', title: '单位', width: 80 },
  { dataIndex: 'caseConversionRate', title: '包装数量', width: 120 },
  { dataIndex: 'manufacturerLotNum', title: '批号', width: 150 },
  { dataIndex: 'reason', title: '移库原因', width: 150 },
  // { dataIndex: 'code16', title: '目标容器', key: 'code16', type: 'edit' },
  // { dataIndex: 'code17', title: '目标货格', key: 'code17', type: 'edit' },
  { dataIndex: 'batchProductionDateShow', title: '生产日期', width: 100 },
  { dataIndex: 'batchExpiredDateShow', title: '有效期至', width: 100 },
  { dataIndex: 'shelfLifeDays', title: '保质天数', width: 80 },
  { dataIndex: 'warehouseName', title: '仓库名称', width: 150 },
  { dataIndex: 'warehouseWorkerId', title: '库工编码', width: 120 },
  { dataIndex: 'warehouseWorkerName', title: '库工名称', width: 150 },
  { dataIndex: 'comments', title: '备注', width: 150 },
]

export default {
  components: { detailPage },
  data() {
    return {
      items: items(this),
      columns: null,
      title: '移库制单确认',
      tableRowKey: 'moveLineId',
      selectedRowKeys: [],
      selectedRows: [],
      detialRecord: {},
      addStatus: false,
      textareaValue: '', // 审核意见
      visible: 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: '1',
          statusList: 'W00',
        },
      },
      btnOptions: [
        {
          name: '反审核',
          icon: '',
          action: 'cancelDel',
        },
        {
          name: '审核',
          icon: '',
          action: 'checkOk',
        },
      ],
    }
  },
  created() {
    console.log()
    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,
      start: this.getDay(-7),
      end: this.getDay(0),
    })
  },
  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
    },
    changeClick(record) {
      // 编辑
      this.detialShow = true
      this.detialRecord = record
      console.log(record)
    },
    requestUrl(val) {
      this.detialShow = val
    },
    cancelBtn(val) {
      this.addStatus = false
    },
    onChange(e) {
      // 审核意见
      this.textareaValue = e
    },
    handleOk() {
      console.log(this.textareaValue, 'this.textareaValuethis.textareaValue')
      if (this.textareaValue.length < 1) {
        return this.$message.warn('请输入审核意见')
      }
      let params = {
        lineBillId: this.selectedRowKeys,
        auditComments: this.textareaValue,
      }
      moveAudit(params).then((res) => {
        if (res.success) {
          this.$message.success('审核成功')
          this.visible = false
          this.textareaValue = ''
          this.$refs.detial.$refs.table.reload()
        } else {
          this.$exception(res)
        }
      })
    },
    checkOk() {
      // 审核
      if (this.selectedRowKeys < 1) {
        return this.$message.warn('请选择商品！')
      }
    //   点击【审核】按钮，若选择审核的移库记录中【移出库别】=【立库】，则依据勾选的记录行对应移出托盘号调用接口【出库任务推送】（作业类型=20移库出库）产生出库指令。
      this.visible = true
    },

    cancelDel() {
      // 作废
      if (this.selectedRowKeys < 1) {
        return this.$message.warn('请选择商品！')
      }
      let params = {
        lineBillId: this.selectedRowKeys,
      }
      moveCancel(params).then((res) => {
        if (res.success) {
          this.$message.success('作废成功')
          this.$refs.detial.$refs.table.reload()
        } else {
          this.$exception(res)
        }
      })
    },
    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
    },
  },
}
</script>

<style></style>
