/*
 *@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"
        :formExtendedField="formExtendedField"
        :record="currentMainDataView"
        :options="options"
        @back="onBack"
      >
      </orderView>

      <orderEdit
        v-show="detailsState > 1"
        :st="detailsState"
        :record="currentMainData"
        :mainId="mainId"
        :formExtendedField="formExtendedField"
        :options="options"
        :formName="formName"
        @back="onBack"
      >
      </orderEdit>

      <div class="pages" v-show="detailsState === 0">
        <searchBar ref="searchBar" v-action:query :bordered="false" :options="options" @submit="submit" />
        <p-card :title="$route.meta.title" :bordered="false">
          <a-space slot="extra">
            <p-button @click="add_item" type="primary" icon="plus" v-action:add>新增</p-button>
            <import-Button :importUrl="mainImportUrl" v-action:import :tmpUrl="mainDownUrl" @onSubmit="$refs.mainTable.reload();$refs.childrenTable.dataSource=[]" :fileName="'订单导入模板'" />
            <!-- v-action:accept -->
            <p-button type="primary" @click="toggleReject(selectedRowKeys, 1)" :disabled="selectedRowKeys.length === 0" v-action:order>接单</p-button>
            <!-- v-action:reject -->
            <p-button type="danger" @click="toggleReject(selectedRowKeys, 0)" :disabled="selectedRowKeys.length === 0"  v-action:refuse>拒绝</p-button>
            <p-button @click="dataSubmit(selectedRowKeys)" :disabled="selectedRowKeys.length === 0" v-action:submit type="primary">提交</p-button>
            <p-button @click="delete_items(selectedRowKeys)" :disabled="selectedRowKeys.length === 0" v-action:cancellation type="danger">作废</p-button>
            <a-dropdown>
              <p-button
                >更多
                <a-icon :type="'down'" />
              </p-button>
              <a-menu slot="overlay" @click="handleMenuClick">
                <a-menu-item :key="1">
                  <p-button type="link" @click="mapPase('shipbill', selectedRowKeys)" :disabled="selectedRowKeys.length === 0" v-action:mapParse>地址解析 </p-button>
                </a-menu-item>
                <a-menu-item :key="2" v-action:download>
                  <export-button
                    type="link"
                    :ids="selectedRowKeys"
                    :pages="mainPages"
                    :getSearchValue="() => $refs.searchBar.getFieldsValue()"
                    downloadFileName="订单信息"
                    :allUrl="UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_export_all'"
                    :currentUrl="UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_export_page'"
                    :selectUrl="UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_export_choose'"
                    >导出</export-button
                  >
                </a-menu-item>
                <a-menu-item :key="3">
                  <p-button type="link" :disabled="selectedRowKeys.length === 0" v-action:print>打印</p-button>
                </a-menu-item>
                <a-menu-item type="link" key="4" v-action:location>
                  <p-button type="link" @click="onLocation" :disabled="selectedRowKeys.length === 0">定位</p-button>
                </a-menu-item>
                <slot />
              </a-menu>
            </a-dropdown>
          </a-space>
          <mainTable
            ref="mainTable"
            :options="options"
            :tableExtendedField="tableExtendedField"
            :activeID="mainId"
            @update="handleUpdate"
            @view="handleView"
            @copy="handleCopy"
            @clickRow="handleRowClick"
            @delete="(arr) => delete_items(arr)"
            :doneBefore="doneBefore"
            @submitItem="(arr) => dataSubmit(arr)"
            @selectChange="selectChange"
          />
        </p-card>
        <p-card title="商品信息" :bordered="false" style="margin-top: 10px">
          <childrenTable
            ref="childrenTable"
            :options="options"
            :tableExtendedField="childrenTableExtendedField"
            @selectChange="(selectedKeys)=>{children_selectedRowKeys=selectedKeys}"
            :mainRowKey="mainId"
            :activeID="childrenId"
          />
        </p-card>
      </div>
    </div>
    <addressAnalysis :showType="addressAnalysisShow" :type="addressType" rowKey="shipbillId" :remoteUrl="addressResolutionUrl" :searchParams="addressSearchData" @back="addressBack" />
  </div>
</template>
<script>
import searchBar from './model/searchBar/index.searchBar.vue'
import mainTable from './model/mainTable/index.mainTable.vue' /**主表 */
import childrenTable from './model/childrenTable/index.childrenTable.vue' /**明细表 */
import orderEdit from './model/orderEdit/index.orderEdit.vue' /**内容编辑 */
import orderView from '@/blocks/components/orderView/index.orderView.vue' /**内容查看 */
import {getOrganization } from '@/blocks/components/formConfig/departmenTree.js'

import { mainTableInfoUrl, deleteMainItem, dataSubmit, mainImportUrl, addressResolutionUrl, toggleReject, mainDownUrl,Location } from './api.js'
import { initDictionary, resultExtendedField } from '@/blocks/utils'
import importButton from './model/importButton/index.importButton.vue'
import exportButton from '@/blocks/components/export-modal/modal.index.vue'
import addressAnalysis from '@/views/TMS/order/orderManage/model/addressAnalysis/addressAnalysis.index.vue' // 地址解析
let options = null
import { UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
import orderPath from './model/orderPath/index.orderPath.vue'

export default {
  components: {orderPath, exportButton, orderView, orderEdit, importButton, searchBar, mainTable, childrenTable, addressAnalysis },
  data() {
    return {
      UPCLOUD_RDC_TMS_BUSINESS_SERVICE,
      mainPages: { pageNum: 1, pageSize: 15 },
      addressResolutionUrl,
      mainTableInfoUrl,
      mainImportUrl,
      mainDownUrl,
      options: options || {},
      loading: false,
      formName: 'main',
      selectedRowKeys: [],
      children_selectedRowKeys: [],
      confirmLoading: false,
      detailsState: 0,
      /**0=不显示,1=查看,2=新建,3=修改,4=复制 */
      currentMainData: null,
      currentMainDataView: null,
      currentChildrenData: null,
      tableExtendedField: [],
      childrenTableExtendedField: [],
      childrenFormExtendedField: [],
      goodsExtendedField: [],
      formExtendedField: {
        form: [],
        formTable:[]
      },
      mainId: '',
      childrenId: '',
      selectedRowsData: [],
      addressAnalysisShow: 0,
      addressType: 'shipbill',
      addressSearchData: {},
      addressData: {}, // 新增修改时带过去的经纬度、地址信息
    }
  },
  mounted() {
    let _this = this
    getOrganization(this)
        //运单扩展
    resultExtendedField({ extendedObjectCode: '15' }).then((data) => {
      if (data) {
        _this.tableExtendedField = data.table
        _this.formExtendedField.form = data.form
      }
    })
    //运单商口明细扩展
    resultExtendedField({ extendedObjectCode: '17' }).then((data) => {
      if (data) {
        data.form.forEach((item, i) => {
          data.table[i].type = item.type
          if (item.options) {
            data.table[i].options=item.options
          }
        })
        _this.formExtendedField.formTable = data.table
      }
    })
    //商品扩展
    resultExtendedField({ extendedObjectCode: '2' }).then((data) => {
      if (data) {
        _this.childrenTableExtendedField = data.table
      }
    })
    initDictionary([
      { objectcode: 'PICK_UP_SIGN_TIME' },
      { objectcode: 'DELIVERY_SIGN_TIME' },
      { objectcode: 'SIGN_TIME_UNIT' },
      { objectcode: 'ORDER_BUSINESS_TYPE' },
      { objectcode: 'ORDER_TYPE' },
      { objectcode: 'PACK_TYPE' },
      { objectcode: 'CARGO_THERMOSPHERE' },
      { objectcode: 'DELIVERY_METHOD_TYPE' },
      { objectcode: 'TEMPERATURE_REQUIREMENT' },
      { objectcode: 'SHIPPER_TYPE' },
      { objectcode: 'CONSIGNEE_TYPE' },
      { objectcode: 'LOCATION_FLAG' },
      { objectcode: 'TRANSPORT_TYPE' },
    ]).then((res) => {
      for (let key in res) {
        res[key].forEach((item) => {
          item.value = +item.value
        })
      }
      _this.options = { ..._this.options, ...res }
    })
  },
  watch: {
    currentChildrenData(data) {
      if (data) {
        this.childrenId = data.id
      }
    },
  },
  computed: {
    formRecord() {
      let obj = {
        main: this.currentMainData,
        children: this.currentChildrenData,
      }
      return obj[this.formName]
    },
  },
  methods: {
    exportRemoteBefore() {},
    toggleReject(selectedRowKeys, state) {
      toggleReject(this, selectedRowKeys, state)
      this.$refs.childrenTable.dataSource = []
      return this.$refs.mainTable.reload()
    },
    doneBefore(res) {
      if (res.data) {
        const { pageNum, pageSize } = res.data
        this.mainPages = { pageNum, pageSize }
      }
    },
    dataSubmit(selectedRowKeys) {
      dataSubmit(this, selectedRowKeys)
      this.$refs.childrenTable.dataSource = []
    },

    delete_items(selectedRowKeys) {
      deleteMainItem(this, selectedRowKeys)
      this.$refs.childrenTable.dataSource = []
    },
    handleMenuClick() {},
    submit() {
      this.mainId = ''
      this.$refs.childrenTable.dataSource = []
      return this.$refs.mainTable.reload()
    },
    add_item(data) {
      this.detailsState = 2
      this.$nextTick(() => {
        this.currentMainData = null
        this.formName = 'main'
      })
    },

    import_item() {},

    handleView(data) {
      this.detailsState = 1
      this.$nextTick(() => {
        this.currentMainDataView = data
        this.formName = 'main'
      })
    },
    childrenHandleView(data) {
      this.detailsState = 1
      this.$nextTick(() => {
        this.currentMainDataView = data
        this.formName = 'children'
      })
    },
    handleUpdate(data) {
      this.detailsState = 3
      this.currentMainData = null
      this.$nextTick(() => {
        this.currentMainData = data
        this.formName = 'main'
      })
    },

    handleCopy(data) {
      this.detailsState = 4
      this.currentMainData = null
      this.$nextTick(() => {
        this.currentMainData = data
        this.formName = 'main'
      })
    },
    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=发货方
    },

    handleRowClick(event, key, data) {
      this.mainId = key
      this.childrenId = ''
      if (key) {
        this.currentMainData = data
      } else {
        this.currentMainData = null
      }
    },
    selectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys=selectedRowKeys
      this.selectedRowsData = selectedRows
    },

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

    export_item() {},
    onBack(formName, id, isReload) {
      this.detailsState = 0
      this.currentMainData = null
      this.currentMainDataView = null
      this.$refs.childrenTable.dataSource = []
      if (isReload) {
        this.mainId = ''
        this.$refs.childrenTable.dataSource = []
        this.$refs['mainTable'].reload().then(() => {
          if (id) {
            this.mainId = id
          }
        })
      }
    },

    onLocation() {
      let list = this.selectedRowsData.map(item => {
        return {
          shipbillId: item.shipbillId,
          shipperType: item.shipperType,
          shipperId: item.shipperId,
          consigneeType: item.consigneeType,
          consigneeId: item.consigneeId,
        }
      })
      Location(this, list);
      this.$refs.childrenTable.dataSource = [];
    },
  },
}
</script>
