/*
 *@description：待理货
 *@author: hudw
 *@date: 2022-06-16 11:35:37
*/
<template>
  <div class="pages">
    <orderView

      v-show="detailsState === 1"
      :parcelTable="true"
      :st="checkMode"
      :goods="goods"
      :formExtendedField="formExtendedField"
      :parcel="parcel"
      :record="mainCurrent"
      :mainId="mainId"
      :options="options"
      :formName="'main'"
      @back="onBack"
    />
    <div class="pages" v-show="detailsState === 0">
      <searchBar ref="searchBar" :adQueryUrl="advancedQueryUrl" :options="options" :count="searchType" @submit="searchSubmit"></searchBar>
      <mainTable
        ref="mainTable"
        title="运单信息"
        :mode="checkMode"
        :url="mainUrl"
        :exportUrls="exportUrls"
        :options="options"
        :activeID="mainId"
        :tableExtendedField="tableExtendedField"
        :operation="{ width: 160 }"
        @clickRow="handleRowClick"
        @modeCheck="modeClick"
        @view="handleView"
        @selectChange="selectChange"
        @print="handPrint"
      >
        <template slot="afterBtn" slot-scope="prop">
          <p-button :type="checkMode === 6 ? 'primary' : 'default'" @click="prop.handelChecked($event, 6)">预约单模式</p-button>
        </template>
        <template #extra>
          <p-button type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="start(mainsSelectedRowKeys)">开始理货</p-button>
          <p-button type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="complete(mainsSelectedRowKeys)">理货完成</p-button>
        </template>

        <template slot="operation" slot-scope="props">
          <template v-if="checkMode === 6">
          <!-- 待理货页面 预约模式 tallyStatus=10 开始理货亮，tallyStatus=20 理货完成亮 -->
            <a href="javascript:;" :disabled="!(props.record.tallyStatus == 10)" @click.stop="start([props.record])">开始理货</a>
            <a-divider type="vertical" />
            <a href="javascript:;" :disabled="!(props.record.tallyStatus == 20)" @click.stop="complete([props.record])">理货完成</a>
          </template>
          <template v-else>
            <a href="javascript:;" :disabled="!(checkMode===2|| props.record.status == 50)" @click.stop="start([props.record])">开始理货</a>
            <a-divider type="vertical" />
            <a href="javascript:;" :disabled="!(checkMode===2||props.record.status == 60)" @click.stop="complete([props.record])">理货完成</a>
          </template>
        </template>
      </mainTable>
      <p-card :bordered="false">
        <a-tabs style="background-color: #fff" v-model="tabsKey">
          <a-tab-pane :key="1" tab="配载运单" v-if="checkMode !== 1">
            <shipbillTable ref="shipbillTable" :url="shipbillTableUrl" :options="options" :tableExtendedField="tableExtendedField" />
          </a-tab-pane>
          <a-tab-pane :key="2" tab="配载商品">
            <goodsTable ref="goodsTable" :url="goodsTableUrl" :options="options" :tableExtendedField="goodsExtendedField" />
          </a-tab-pane>
          <a-tab-pane :key="3" tab="配载包裹">
            <parcelTable ref="parcelTable" :url="packageTableUrl" :options="options" :tableExtendedField="tableExtendedField" />
          </a-tab-pane>
        </a-tabs>
      </p-card>
    </div>
  </div>
</template>
<script>
import useMixin from '../$common/mixins/useMixin.js'
import dictionary from '../$common/mixins/dictionary.js'
import {
  advancedQueryUrl,
  mainTableAppointmentUrl,
  mainTableInfoUrl,
  shipbillTableUrl,
  mainTableNextInfoUrl,
  goodsTableUrl,
  packageTableUrl,
  completeArrangeGoods,
  startArrangeGoods,
  exportUrls,
  printUrl,
} from './api.js'

export default {
  mixins: [useMixin, dictionary],
  data() {
    const goods = {
      url: goodsTableUrl,
    }
    const parcel = {
      url: packageTableUrl,
    }
    return {
      advancedQueryUrl,
      printUrl,
      exportUrls,
      goodsTableUrl,
      packageTableUrl,
      goods,
      parcel,
      shipbillTableUrl,
      searchType: 1,
      mainUrl: [mainTableInfoUrl, mainTableNextInfoUrl, '', '', '', mainTableAppointmentUrl],
      checkMode: 1,
      tabsKey: 2,
      detailsState: 0 /**0=不显示, 1=新建，2=修改 ，3=查看 ，4=复制  */,
    }
  },
  mounted() {},
  watch: {
    tabsKey(value) {
      if (this.mainCurrent) {
        this.handleRowClick(null, this.mainId, this.mainCurrent)
      }
    },
  },
  methods: {
    selectChange(selectKey, selectData) {
      this.mainsSelectedRowKeys = selectData
    },
    start(arr) {
      let record = arr.map((item) => {
        let _id = item.shipbillId
        if (item.shipbillIds) {
          _id = item.shipbillIds
        }
        return _id
      })
      record = record.join(',')
      startArrangeGoods(this, record.split(','))
    },
    complete(arr) {
      let record = arr.map((item) => {
        let _id = item.shipbillId
        if (item.shipbillIds) {
          _id = item.shipbillIds
        }
        return _id
      })
      record = record.join(',')
      completeArrangeGoods(this, record.split(','))
    },

    modeClick(event, type) {
      this.searchType = 1
      if (type == 6) {
        this.searchType = type
      }
      this.checkMode = type
      this.clearChildrenTableAll(true)

      if (type === 1) {
        //运单模式
        this.tabsKey = 2
      } else if (type === 2 || type === 6) {
        //收货方模式\预约单模式
        this.tabsKey = 1
      }
      this.$refs.mainTable.clearTableData()
      type !== 6 &&
        this.$nextTick(() => {
          this.$refs.mainTable.reload()
        })
    },
    handleRowClick(event, key, data) {
      this.clearChildrenTableAll(true)
      this.mainId = key
      if (key) {
        this.mainCurrent = data
        const { goodsTable, parcelTable, shipbillTable } = this.$refs
        let ref = [null, shipbillTable, goodsTable, parcelTable]
        ref = ref[this.tabsKey]
        let shipbillId = data.shipbillId
        if (data.shipbillIds) {
          shipbillId = data.shipbillIds
        }
        ref && ref.reload({ data: { shipbillId } })
      }
    },
  },
}
</script>
