/*
 *@description:运单支出业务
 *@author: hudw
 *@date: 2022-04-13 13:51:13
*/
<template>
  <div class="pages">
    <div class="pages" v-show="!addressAnalysisShow">
      <orderView
        v-show="detailsState === 1"
        :st="1"
        :url="mainViewUrl"
        :formExtendedField="formExtendedField"
        :goodsExtendedField="childrenFormExtendedField"
        :record="currentMainDataView"
        :mainId="detailsState>0&&mainId"
        :options="options"
        :formName="formName"
        subTitle="支出"
        @back="onBack"
      >
      </orderView>

      <orderEdit
        subTitle="支出"
        :saveDataSubmit="mainSaveDataSubmit"
        :saveData="mainSaveData"
        v-show="detailsState > 1"
        :st="detailsState"
        :record="currentMainData"
        :mainId="detailsState>0&&mainId"
        :formExtendedField="formExtendedField"
        :goodsExtendedField="childrenFormExtendedField"
        :options="options"
        :formName="formName"
        @back="onBack"
      >
      </orderEdit>
      <div class="pages" v-show="detailsState === 0">
        <searchBar ref="searchBar" :options="options" :count="1" @submit="searchSubmit"></searchBar>
        <p-card title="运单信息" :bordered="false" :bodyStyle="{minHeight:'300px'}">
          <a-space slot="extra">
            <p-button @click="handleAdd" type="primary" v-action:add>新增</p-button>
            <import-Button :importUrl="importUrl" :tmpUrl="downUrl" fileName="运单去出模板" v-action:import />
            <p-button @click="adjustAccounts_items(selectedRowKeys)" :disabled="selectedRowKeys.length === 0" type="primary">核算</p-button>
            <p-button @click="batch_sub(selectedRowKeys,true)" :disabled="selectedRowKeys.length === 0" type="primary">核算提交</p-button>
          </a-space>
          <mainTable
            ref="mainTable"
            :options="options"
            :tableExtendedField="tableExtendedField"
            :activeID="mainId"
            @update="handleUpdate"
            @view="handleView"
            @clickRow="handleRowClick"
            @adjustAccounts="adjustAccounts_items"
            :doneBefore="doneBefore"
            @selectChange="selectChange"
            @submit="(select)=>batch_sub(select,false)"
          >

        </mainTable>
        </p-card>
        <p-card :bordered="false" style="margin-top: 10px" :bodyStyle="{minHeight:'300px'}">
          <a-tabs style="background-color: #fff" v-model="tabsKey">
            <a-tab-pane :key="1" tab="商品信息" >
              <goodsTable ref="childrenTable_1" :options="options" :isShow="tabsKey===1" :tableExtendedField="childrenTableExtendedField" :mainRowKey="mainId" :activeID="childrenId" />
            </a-tab-pane>
            <a-tab-pane :key="2" tab="包裹信息">
              <parcelTable ref="childrenTable_2" :options="options" :isShow="tabsKey===2"  :mainRowKey="mainId" />
            </a-tab-pane>
            <a-tab-pane :key="3" tab="调整信息">
              <adjustTable ref="childrenTable_3" :options="options" :isShow="tabsKey===3" :mainRowKey="mainId" />
            </a-tab-pane>
            <a-tab-pane :key="4" tab="审批信息">
              <approveTable ref="childrenTable_4" :options="options"  :isShow="tabsKey===4" :mainRowKey="mainId" />
            </a-tab-pane>
            <a-tab-pane :key="5" tab="操作日志">
              <logTable ref="childrenTable_5" :options="options" :isShow="tabsKey===5" :mainRowKey="mainId" /></a-tab-pane>
          </a-tabs>
        </p-card>
      </div>
    </div>
  </div>
</template>
<script>

import mainTable from './model/mainTable/index.mainTable.vue' /**主表 */
import goodsTable from './model/childrenTable/goodsTable.vue' /**明细表 */
import parcelTable from './model/childrenTable/parcelTable.vue' /**明细表 */
import adjustTable from './model/childrenTable/adjustTable.vue' /**明细表 */
import approveTable from './model/childrenTable/approveTable.vue' /**明细表 */
import logTable from './model/childrenTable/logTable.vue' /**明细表 */
import {  resultExtendedField } from '@/blocks/utils'

import { mainTableInfoUrl,mainViewUrl, adjustAccounts, dataSubmit,mainSaveDataSubmit,mainSaveData,downUrl,importUrl} from './api'

import { UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
import useMixin from '../../$common/mixins/useMixin'
import dictionary from '../../$common/mixins/dictionary' //@字典
export default {
  mixins: [useMixin, dictionary],
  components: { mainTable, goodsTable, parcelTable, adjustTable, approveTable, logTable },
  data() {
    return {
      downUrl,importUrl,
      mainViewUrl,
      UPCLOUD_RDC_TMS_BUSINESS_SERVICE,
      mainPages: { pageNum: 1, pageSize: 15 },
      mainTableInfoUrl,
      loading: false,
      formName: 'main',
      selectedRowKeys: [],
      children_selectedRowKeys: [],
      confirmLoading: false,
      detailsState: 0,/**0=不显示,1=查看,2=新建,3=修改,4=复制 */
      tabsKey: 1,
      currentMainData: null,
      currentMainDataView: null,
      currentChildrenData: null,
      childrenFormExtendedField: [],
      mainId: '',
      childrenId: '',
      selectedRowsData: [],
      addressAnalysisShow: 0,
      addressType: 'shipbill',
      addressSearchData: {},
      addressData: {}, // 新增修改时带过去的经纬度、地址信息
    }
  },
  mounted() {
    let _this = this
        //运单扩展字段
    resultExtendedField({ extendedObjectCode: '18' }).then((data) => {
      if (data) {
        _this.tableExtendedField = data.table
        _this.formExtendedField = data.form
      }
    })
        //商品扩展字段
    resultExtendedField({ extendedObjectCode: '19' }).then((data) => {
      if (data) {
        _this.childrenTableExtendedField = data.table
        _this.childrenFormExtendedField = data.form
        console.log(_this.childrenFormExtendedField)
      }
    })




  },
  watch: {
    currentChildrenData(data) {
      if (data) {
        this.childrenId = data.id
      }
    },
    tabsKey(value) {
      this.childrenTableReload()
    }
  },
  computed: {


  },
  methods: {
    mainSaveDataSubmit,
    mainSaveData,
    childrenTableReload() {
      this.$nextTick(() => {
        let table = this.$refs['childrenTable_' + this.tabsKey]
        if (table) {
          table.dataSource = []
          this.mainId && table.reload()
        }
      })
    },



    doneBefore(res) {
      if (res.data) {
        const { pageNum, pageSize } = res.data
        this.mainPages = { pageNum, pageSize }
      }
    },

    adjustAccounts_items(selectedRowKeys, id = null) {
      adjustAccounts(this, selectedRowKeys, id)
      this.childrenTableClear()
    },
    handleMenuClick() {},
    submit() {
      this.mainId = ''
      this.childrenTableClear()
      return this.$refs.mainTable.reload()
    },
    batch_sub(selectedRowKeys,bool) {
      dataSubmit(this, selectedRowKeys,bool)
      this.childrenTableClear()
    },


    import_item() {},

    handleView(data) {
      this.detailsState = 1
      this.$set(this, 'currentMainDataView', data)
      this.formName = 'main'

    },
    childrenHandleView(data) {
      this.detailsState = 1
      this.$set(this, 'currentMainDataView', data)
      this.formName = 'children'

    },
    handleAdd() {

      this.detailsState = 2
      this.formName = 'main'
    },
    handleUpdate(data) {

      this.detailsState = 3
      this.$set(this, 'currentMainData', data)
      this.formName = 'main'
    },

    childrenHandleUpdate(data) {
      this.detailsState = 3
      this.$set(this, 'currentChildrenData', data)
      this.formName = 'children';
    },


    addressBack() {
      let formName = ''
      this.addressAnalysisShow = 0
      if (this.addressType == 'supplier') {
        formName = 'main'
      } else if (this.addressType == 'shipper') {
        formName = 'children'
      } else {
        return
      }
      this.$nextTick(() => {
        this.$refs[formName + 'Table'].reload().then(() => {})
      })
      //supplier=供应商,shipper=发货方
    },
    // onMapReturnData(data) {
    //   debugger
    //   let form = this.$refs.formDetails.form()
    //   form.setFieldsValue(data)
    //   this.addressAnalysisShow = 0
    // },

    handleRowClick(event, key, data) {
      this.mainId = key
      this.childrenId = ''
      this.currentMainData = data

    },
    selectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys=selectedRowKeys
      this.selectedRowsData = selectedRows
    },

    mapPase(type, selectedRowKeys) {
      this.addressSearchData = { shipbillIds: this.selectedRowKeys }
      this.addressType = type
      this.addressAnalysisShow = 2
    },


  },
}
</script>
