/*
 *@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="childerCurrent"
      :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="(selectedRowKeys, selectedRows)=>{ mainsSelectedRowKeys=selectedRowKeys}"

        :operation="{ width: 100 }"
        @clickRow="handleRowClick"
        @print="handPrint"
      >
        <template #extra>
          <p-button type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="handelFeedback('托运交接', mainsSelectedRowKeys)">托运交接</p-button>
        </template>
        <template slot="operation" slot-scope="props">
          <a href="javascript:;" @click.stop="handelFeedback('托运交接', [props.record.loadingBillId])">托运交接</a>
        </template>
      </mainTable>
      <p-card :bordered="false">
        <a-tabs style="background-color: #fff" v-model="tabsKey">
          <template slot="tabBarExtraContent" slot-scope="props">
            <p-button type="primary" :disabled="shipbillTableSelectedRowKeys.length === 0" @click.stop="handelFeedback('正本回单确认', shipbillTableSelectedRowKeys)">正本回单确认</p-button>
          </template>
          <a-tab-pane :key="1" tab="配载运单">
            <shipbillTable
              ref="shipbillTable"
              @selectChange="shipbillSelectChange"
              @view="handleView"
              :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 { mainTableInfoUrl, goodsTableUrl, packageTableUrl, shipbillTableUrl, completeArrangeGoods, exportUrls, printUrl, confirmSubmit, handoverSubmit } from './api.js'
import { setUpdateForm, setShipbillForm } 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: 4,
      tabsKey: 1,
      detailsState: 0 /**0=不显示, 1=新建，2=修改 ，3=查看 ，4=复制  */,
      card_title: '',
      visible: false,
      updateFormItems: null,
      currentData: null,
      childerCurrent:null
    }
  },
  mounted() {
    // this.handelFeedback('ttt',['12312312','3123124'])
  },
  watch: {
    tabsKey(value) {
      if (this.mainCurrent) {
        this.handleRowClick(null, this.mainId, this.mainCurrent)
      }
    },
  },
  methods: {
    handleView(data) {
      this.detailsState = 1;
      this.$nextTick(() => {
        this.childerCurrent = data;
      });
    },
    handelFeedback(title, data) {
      if (Array.isArray(data)) {
        data = { ids: data.join(',') }
      }
      if (title === '正本回单确认') {
        this.updateFormItems = setShipbillForm(this, title)
      } else {
        this.updateFormItems = setUpdateForm(this, title)
      }

      this.card_title = title
      this.currentData = data
      this.$nextTick(() => {
        this.visible = true
      })
    },
    handle_ok(submit, close) {
      submit().then((values) => {
        if (values === null) return close()
        values.ids = values.ids.split(',')
        values.pictureUrl = values.picture
        delete values.picture
        if (this.card_title === '正本回单确认') {
          return confirmSubmit(this, values, close)
        } else {
          return handoverSubmit(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=selectKey
      this.shipbillTableSelectedRowKeys = selectData.map((item) => {
        return item.shipbillId
      })
    },
    onValuesChange({ form }, values) {},
  },
}
</script>