<template>
  <div>
    <div>
      <p-card title="主要信息">
        <p-grid-form
          ref="formTarget"
          :items="gridItems"
          @submit="
            ($event, form) => {
              handleSubmit($event, form, 'formTarget')
            }
          "
        />
        <p-table-edit-pro
          ref="logTable"
          style="margin-top: 10px"
          :columns="tableColumnsConfig"
          rowKey="lineId"
          :rowSelectType="2"
          :dataSource="dataSource"
          :rowSelectFixed="true"
          :loading="tabLoding"
          :scroll="{ x: '100%' }"
          @selectChange="detailChange"
          dragle
          auto-width
          bordered
        >
        </p-table-edit-pro>
        <div slot="extra">
          <p-button type="primary" @click="extract"> 提取 </p-button
          ><p-button style="margin-left: 10px" type="primary" @click="save"> 保存 </p-button>
        </div>
      </p-card>
    </div>
    <div>
      <p-modal
        title="提取"
        :visible="visible"
        :enlargeable="false"
        :draggable="true"
        width="65%"
        :maskClosable="false"
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <div>
          <p-table
            :columns="extractColumns"
            rowKey="billId"
            :expandRowByClick="false"
            :rowSelectFixed="true"
            :rowSelectType="1"
            :dataSource.sync="extractData"
            :hasRowSelectCheck="true"
            @selectChange="selectChange"
            dragle
            auto-width
            bordered
          >
          </p-table>
        </div>
      </p-modal>
    </div>
  </div>
</template>
<script>
import { setFormItems, tableColumns, extractColumns } from './js/config'
import { getList, detailList, checkAdd } from './api/index'
import { diffOption } from '@/views/kzyWMS/utils/index.js'
import { sorter } from '../../../../utils/sort'
import { columns } from '../../../../QualityControl/inBoundSupervised/config/columns'
import { getCargoOwner } from '../../../../utils'
import { toPrintServer, tempType } from '../../../../utils/printServer'

export default {
  data() {
    return {
      dataSource: [],
      tabLoding: false,
      gridItems: setFormItems(this),
      rowSelectType: 2,
      tableColumnsConfig: null,
      visible: false,
      extractColumns: extractColumns,
      extractData: [],
      extractRow: null,
      detailRow: null,
    }
  },
  created() {
    let column = tableColumns(this)
    this.tableColumnsConfig = sorter(column)
    // this.getList()
    let warehouse = this.$store.state.user.warehouse
    this.$nextTick(() => {
      this.$refs.formTarget.form.setFieldsValue(warehouse)
    })
    this.getCargoOwnerFirst()
    // diffOption(tableColumnsConfig).then((res) => {
    //   this.tableColumnsConfig = sorter(res)
    // })
  },
  methods: {
    handleOk(e) {
      this.confirmLoading = true
      if (!this.extractRow) {
        this.$message.warning('请选择数据！')
        return
      }
      detailList({
        billId: this.extractRow[0].billId,
        billNo: this.extractRow[0].billNo,
      }).then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        this.visible = false
        this.confirmLoading = false
        res.data.list.forEach((t) => {
          console.log(t,'ttttttttt');
          t.oddPieceQuantity = 0
          t.caseQuantity = 0
          t.warehouseAreaCategory = t.inAreaCategory
        })
        this.dataSource = res.data.list
      })
    },
    //弹框关闭
    handleCancel(e) {
      this.visible = false
    },

    //查询事件
    handleSubmit(event, form) {
      event.preventDefault()
    },

    selectChange(selectedRowKeys, selectedRows) {
      this.extractRow = selectedRows
    },
    // 提取
    extract() {
      let warehouseId = this.$refs.formTarget.form.getFieldsValue().warehouseId
      if (!warehouseId) {
        this.$message.warning('请先选择仓库')
        return
      }
      getList({ warehouseId: warehouseId }).then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        res.data.list.outGoodsCaseCode = ''
        this.extractData = res.data.list
        this.visible = true
      })
    },

    detailChange(selectedRowKeys, selectedRows) {
      this.detailRow = selectedRows
    },
    save() {
      if (!this.detailRow) {
        this.$message.warning('请选择需要保存的信息')
        return false
      }
      var that = this
      this.$refs.logTable.validateFields(function (error, values, selectData) {
        if (error) return
        that.$confirm({
          content: that.$lang('是否确定保存?'),
          onOk: () => {
            return that.handleAdd(that.detailRow)
          },
        })
      })
    },
    handleAdd(params) {
      return checkAdd(params).then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        this.$message.success('保存成功！')
        this.$refs.logTable.reload()
        this.dataSource = []
        
        const printParms = {
          consignorId: params[0].cargoOwnerId,
        }
        const warehouseId = params[0].warehouseId
        var temp = tempType.YK_KP
        //调用打印方法()
        toPrintServer(res.data.data, warehouseId, temp, printParms).then((res) => {
          if (!res.success) {
            this.$exception(res)
            return
          }
          this.$message.success(res.message)
        })
      })
    },


    //获取货主信息
    getCargoOwnerFirst() {
      getCargoOwner().then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        var that = this
        this.$nextTick(() => {
          if (res.data.list.length > 0) {
          that.$refs.formTarget.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerCode: res.data.list[0].companyCode,
              cargoOwnerName: res.data.list[0].companyName,
            })
          }
        })
        // }
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-card-body {
  padding: 12px 18px 18px 18px;
}
</style>