/*
 *@description：提货确认
 *@author: hudw
 *@date: 2022-06-16 11:35:37
*/
<template>
  <div class="pages">
    <updateFormModal
      ref="updateFormModal"
      :onValuesChange="onValuesChange"
      rowKey="ids"
      :visible.sync="visible"
      :view="2"
      :count="8"
      :remoteBefore="viewRemoteBefore"
      @ok="handle_ok"
      :title="card_title"
      :currentData="currentData"
      :formItems="updateFormItems"
    />
    <orderView
      :formExtendedField="formExtendedField"
      v-show="detailsState === 1"
      :parcelTable="true"
      :st="1"
      :goods="goods"
      :parcel="parcel"
      :record="mainCurrent"
      :mainId="mainId"
      :options="options"
      :formName="'main'"
      @back="onBack"
    />
    <div class="pages" v-show="detailsState === 0">
      <searchBar ref="searchBar" :options="options" :count="checkMode" @submit="searchSubmit"></searchBar>
      <mainTable
        :exportUrls="exportUrls"
        ref="mainTable"
        :rowKey="'loadingBillId'"
        title="派车单信息"
        :mode="checkMode"
        :url="mainUrl"
        :options="options"
        :activeID="mainId"
        :tableExtendedField="tableExtendedField"
        @selectChange="(selectKey, selectData)=>mainsSelectedRowKeys=selectKey"
        :operation="{ width: 260 }"
        @clickRow="handleRowClick"
        @view="handleView"
        @print="handPrint"

      >
        <template #extra>
          <p-button type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="handelFeedback('启运登记', mainsSelectedRowKeys, 0)">启运登记</p-button>
          <p-button type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="handelFeedback('客服反馈', mainsSelectedRowKeys, 0)">客服反馈</p-button>
          <p-button type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="handelFeedback('异常登记', mainsSelectedRowKeys, 0)">异常登记</p-button>
        </template>
        <template slot="operation" slot-scope="props">
          <a href="javascript:;" @click.stop="handelFeedback('启运登记', [props.record.loadingBillId], 0)">启运登记</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click.stop="handelFeedback('客服反馈', [props.record.loadingBillId], 0)">客服反馈</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click.stop="handelFeedback('异常登记', [props.record.loadingBillId], 0)">异常登记</a>
          <a-divider type="vertical" />
          <a href="javascript:;">打印</a>
        </template>
      </mainTable>
      <p-card :bordered="false">
        <a-tabs style="background-color: #fff" v-model="tabsKey">
          <a-space v-if="tabsKey == 1" slot="tabBarExtraContent" slot-scope="props">
            <p-button type="primary" :disabled="shipbillTableSelectedRowKeys.length === 0" @click.stop="handelFeedback('启运登记', shipbillTableSelectedRowKeys, 1)">启运登记</p-button>
            <p-button type="primary" :disabled="shipbillTableSelectedRowKeys.length === 0" @click.stop="handelFeedback('客服反馈', shipbillTableSelectedRowKeys, 1)">客服反馈</p-button>
            <p-button type="primary" :disabled="shipbillTableSelectedRowKeys.length === 0" @click.stop="handelFeedback('异常登记', shipbillTableSelectedRowKeys, 1)">异常登记</p-button>
            <p-button type="primary" :disabled="shipbillTableSelectedRowKeys.length === 0" @click.stop="handleRemove(shipbillTableSelectedRowKeys)">剔除运单</p-button>
          </a-space>
          <a-tab-pane :key="1" tab="配载运单">
            <shipbillTable
              ref="shipbillTable"
              @selectChange="shipbillSelectChange"
              @update:selectedRowKeys="(keys) => (shipbillTableSelectedRowKeys = keys)"
              :rowSelectType="2"
              :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 { feedbackConfirm, abnormalRegister, startShipRegister, removeItem, mainTableInfoUrl, goodsTableUrl, packageTableUrl, shipbillTableUrl, exportUrls, printUrl } from './api.js'
import { setUpdateForm, setStartShipRegisterItems } from './config.js'

export default {
  mixins: [useMixin, dictionary],
  data() {
    const goods = {
      url: goodsTableUrl,
    }
    const parcel = {
      url: packageTableUrl,
    }
    return {
      shipbillTableSelectedRowKeys: [],
      exportUrls,
      printUrl,
      shipbillTableUrl,
      goodsTableUrl,
      packageTableUrl,
      goods,
      parcel,
      mainUrl: mainTableInfoUrl,
      checkMode: 3,
      tabsKey: 1,
      detailsState: 0 /**0=不显示, 1=新建，2=修改 ，3=查看 ，4=复制  */,
      card_title: '',
      visible: false,
      updateFormItems: null,
      currentData: null,
    }
  },
  mounted() {},
  watch: {
    tabsKey(value) {
      if (this.mainCurrent) {
        this.handleRowClick(null, this.mainId, this.mainCurrent)
      }
    },
  },
  methods: {
    handleRemove(shipbillIds) {
      return removeItem(this, { shipbillIds, loadingBillId: this.mainCurrent?.loadingBillId })
    },
    handelFeedback(title, data, st) {
      if (Array.isArray(data)) {
        data = { ids: data.join(',') }
      }
      if (title === '启运登记') {
        this.updateFormItems = setStartShipRegisterItems(this, st)
      } else if (title === '客服反馈' || title === '异常登记') {
        this.updateFormItems = setUpdateForm(this, title, st)
      } else {
        return
      }
      this.card_title = title
      this.currentData = data
      this.$nextTick(() => {
        this.visible = true
      })
    },
    handle_ok(submit, close) {
      submit().then((values) => {
        if (values === null) return close()

        if (this.card_title === '启运登记') {
          return startShipRegister(this, values, close)
        } else if (this.card_title === '客服反馈') {
          return feedbackConfirm(this, values, close)
        } else if (this.card_title === '异常登记') {
          return abnormalRegister(this, values, close)
        }
      })
    },

    handleRowClick(event, key, data) {
      this.clearChildrenTableAll(true)
      this.mainId = key

      if (key) {
        this.mainCurrent = data
        let loadingBillId = data.loadingBillId
        this.$nextTick(() => {
          const { goodsTable, parcelTable, shipbillTable } = this.$refs
          let ref = [null, shipbillTable, goodsTable, parcelTable]
          ref = ref[this.tabsKey]
          ref && ref.reload({ data: { loadingBillId } })
        })
      }
    },

    shipbillSelectChange(selectKey, selectData) {
      this.shipbillTableSelectedRowKeys = selectData.map((item) => {
        return item.shipbillId
      })
    },
    onValuesChange({ form }, values) {},
  },
}
</script>