<!--
@name: 已调度
@author: 张泽贤
@time: 2022-04-27
@doc: PROLOG_UPCloudSolutionForTMS__v1.2.1_调度管理_1.2.2
-->
<template>
  <div class="pages">
    <p-spin :spinning="spinning" v-if="!editType">
      <p-card style="margin-bottom: 10px">
        <p-form-model ref="formTarget" :model="form"  @submit="onSearch" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }" class="plg-grid-from">
          <a-row>
            <a-col :span="6">
              <p-form-model-item label="货主">
                <p-input  :value="form.cargoOwnerName"  @blur="event=> form.cargoOwnerName=event.target.value" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="订单号">
                <p-input :value="form.orderNo"  @blur="event=> form.orderNo=event.target.value"/>
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="运单号">
                <p-input  :value="form.shipbillNo"  @blur="event=> form.shipbillNo=event.target.value" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="业务类型">
                <p-select v-model="form.orderBusinessType" :options="options.ORDER_BUSINESS_TYPE" allowClear />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="预计提货时间" >
                <p-range-picker v-model="estimatedPickupTime" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" :placeholder="['开始日期', '结束日期']" style="width: 100%" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="预计送达时间" >
                <p-range-picker v-model="estimatedDeliveryTime" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" :placeholder="['开始日期', '结束日期']" style="width: 100%" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="始发地">
                <p-input :value="form.provenance"  @blur="event=> form.provenance=event.target.value" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="目的地">
                <p-input :value="form.destination"  @blur="event=> form.destination=event.target.value" />
              </p-form-model-item>
            </a-col>
            <a-col :span="6">
              <p-form-model-item label="线路">
                <p-input :value="form.lineName"  @blur="event=> form.lineName=event.target.value"  />
              </p-form-model-item>
            </a-col>
            <a-col :span="18">
              <p-form-model-item :wrapper-col="{ span: 24 }" style="text-align: right">
                <a-space>
                  <p-button type="primary"  html-type="submit"   icon="search">查询</p-button>
                  <p-button @click="onReset">重置</p-button>
                </a-space>
              </p-form-model-item>
            </a-col>
          </a-row>
        </p-form-model>
      </p-card>
      <p-card title="运单信息" style="margin-bottom: 10px">
        <a-space slot="extra">
          <p-button type="danger" @click="onRevoke" :disabled="!selectedRowKeys.length">撤销</p-button>
          <a-dropdown>
            <template #overlay>
              <a-menu>
                <a-menu-item key="2">
                  <export-button
                    type="link"
                    :ids="selectedRowKeys"
                    :pages="mainPages"
                    :getSearchValue="() => listParams"
                    downloadFileName="已调度"
                    :allUrl="UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/dispatch/export_all_dispatch'"
                    :currentUrl="UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/dispatch/export_choose_dispatch'"
                    :selectUrl="UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/dispatch/export_page_dispatch'"
                    >导出</export-button
                  >
                </a-menu-item>
                <a-menu-item type="link" key="3">
                  <p-button type="link" :disabled="!selectedRowKeys.length">打印</p-button>
                </a-menu-item>
              </a-menu>
            </template>
            <a-button>更多</a-button>
          </a-dropdown>
        </a-space>
        <t-table
          ref="masterTable"
          dragle
          rowSelectFixed
          bordered
          customScrollbar
          :extendedObjectCode="'15'"
          row-key="shipbillId"
          :id="$route.meta.menuId + '_vehicleManagement'"
          :scroll="{ x: true }"
          :vScrollY="{ enabled:true,rows:10}"
          :vScrollX="{ enabled:true}"
          :columns="mx_columns"
          :data-source.sync="dataSource"
          :remote-url="listRemoteUrl"
          :remoteData="listParams"
          :rowSelectType="2"
          @selectChange="(selectKeys)=>selectedRowKeys=selectKeys"
          @clickRow="masterClickRow"
          :doneBefore="doneBefore"
        >
          <template slot="operation" slot-scope="text, record">
            <a-space>
              <a @click.stop="toView(record)">查看</a>
              <a @click.stop="toRevoke(record)">撤销</a>
              <a @click.stop="toDel(record)">打印</a>
            </a-space>
          </template>
        </t-table>
      </p-card>
      <a-row :gutter="10">
        <a-col :span="12">
          <p-card title="商品信息" :bodyStyle="{minHeight:'300px'}">
            <t-table
              ref="goodsTable"
              dragle
              bordered
              :extendedObjectCode="'17'"
              row-key="id"
              :id="$route.meta.menuId + '_goodsTable'"
              :scroll="{ x: true}"
             :vScrollX="{enabled:false}"
             :vScrollY="{enabled:true,rows:4}"
               :columns="goods_columns"
              :data-source="goodsDataSource"
              :remote-url="goodsListRemoteUrl"
              :remoteData="goodsListParams"
              :rowSelectType="false"
              @selectChange="(selectKeys)=>goodsSelectedRowKeys=selectKeys"
              :autoLoad="false"
              @clickRow="goodsClickRow"
            >
            </t-table>
          </p-card>
        </a-col>
        <a-col :span="12">
          <p-card title="包裹信息" :bodyStyle="{minHeight:'300px'}">
            <t-table
              ref="packTable"
              dragle
              bordered
              row-key="id"
              :id="$route.meta.menuId + '_packTable'"
              :scroll="{ x: true}"
             :vScrollX="{enabled:false}"
             :vScrollY="{enabled:true,rows:4}"
              :columns="pack_columns"
              :data-source="packDataSource"
              :remote-url="packListRemoteUrl"
              :remoteData="packListParams"
              :rowSelectType="false"
              @selectChange="(selectKeys)=>packSelectedRowKeys=selectKeys"
              :autoLoad="false"
              @clickRow="packClickRow"
            >
            </t-table>
          </p-card>
        </a-col>
      </a-row>
    </p-spin>

    <!-- 查看 -->
    <edit
      v-show="editType === 'view'"
      parcelTable
      :options="options"
      :record="editId ? { shipbillId: editId } : null"
      :formExtendedField="formExtendedField"
      @back="editType = ''"
    ></edit>
  </div>
</template>

<script>
import { initDictionary, resultExtendedField } from '@/blocks/utils'
import edit from '@/blocks/components/orderView/index.orderView.vue'
import exportButton from '@/blocks/components/export-modal/modal.index.vue'
import { UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
import { setGoodsColums, setPackColums } from '@/blocks/components/scheduling/js/table/tableConfig.js'

import { listRemoteUrl, page_detail_item, page_package_item, page_detail_package, page_item_package, undo_Dispatch, viewUrl } from './js/api.js'
import { setMainColumns } from './js/tableConfig.js'

export default {
  components: { edit, exportButton },
  computed: {
    estimatedPickupTime: {
      get() {
        return [this.form.estimatedPickupStartTime, this.form.estimatedPickupEndTime]
      },
      set(value) {
        this.$set(this.form, 'estimatedPickupStartTime', value.length ? value[0] + ' 00:00:00' : '')
        this.$set(this.form, 'estimatedPickupEndTime', value.length ? value[1] + ' 23:59:59' : '')
      },
    },
    estimatedDeliveryTime: {
      get() {
        return [this.form.estimatedDeliveryStartTime, this.form.estimatedDeliveryEndTime]
      },
      set(value) {
        this.$set(this.form, 'estimatedDeliveryStartTime', value.length ? value[0] + ' 00:00:00' : '')
        this.$set(this.form, 'estimatedDeliveryEndTime', value.length ? value[1] + ' 23:59:59' : '')
      },
    },
  },
  data() {
    return {
      formExtendedField: {
        form: [],
        formTable:[]
      },
      UPCLOUD_RDC_TMS_BUSINESS_SERVICE,
      spinning: false,
      editType: '', // 空-列表页 view-查看 address-地址解析
      editId: '',
      form: {
        cargoOwnerName: '',
        orderNo: '',
        shipbillNo: '',
        orderBusinessType: '',
        estimatedPickupStartTime: '',
        estimatedPickupEndTime: '',
        estimatedDeliveryStartTime: '',
        estimatedDeliveryEndTime: '',
        destination: '',
        provenance: '',
        lineName: '',
      },
      options: {
        ORDER_BUSINESS_TYPE: [],
      },

      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl,
      listParams: {},
      mx_columns: setMainColumns(this, this.tableExtendedField),
      mextendedField_list: [],
      tableExtendedField: [],
      mainPages: { pageNum: 1, pageSize: 15 },

      // 商品表格
      goodsDataSource: [],
      goodsSelectedRowKeys: [],
      goodsListRemoteUrl: page_detail_item,
      goodsListParams: {},
      page_detail_item,
      goods_columns: setGoodsColums(this, this.goods_tableExtendedField),
      goods_tableExtendedField: [],

      // 包裹表格
      packDataSource: [],
      packSelectedRowKeys: [],
      packListRemoteUrl: page_detail_package,
      packListParams: {},
      page_detail_package,
      pack_columns: setPackColums(this),
    }
  },
  mounted() {
    initDictionary([{ objectcode: 'ORDER_BUSINESS_TYPE' }]).then((res) => {
      this.options.ORDER_BUSINESS_TYPE = res.ORDER_BUSINESS_TYPE.map((item) => {
        item.value = +item.value
        return item
      })
    })
    resultExtendedField({ extendedObjectCode: '15' }).then((res) => {
      if (res) {
        this.mextendedField_list = res.list
        this.tableExtendedField = res.table
        this.formExtendedField.form = res.form
        this.mx_columns = setMainColumns(this, this.tableExtendedField);

      }
    })
    resultExtendedField({ extendedObjectCode: '17' }).then((res) => {
      if (res) {
        this.goods_tableExtendedField = res.table
        this.formExtendedField.formTable = res.table
        this.goods_columns = setGoodsColums(this, this.goods_tableExtendedField);
      }
    })
  },
  methods: {
    viewUrl,
    // 查询
    onSearch(event) {
      if (event) {
        event.preventDefault()
         event.submitter.focus();
      }

      this.dataSource = []
      this.selectedRowKeys = []
      this.goodsDataSource = []
      this.goodsSelectedRowKeys = []
      this.packDataSource = []
      this.packSelectedRowKeys = []
      this.listParams = { ...this.form }
    },
    // 重置
    onReset() {
      this.form = {
        cargoOwnerName: '',
        orderNo: '',
        shipbillNo: '',

        orderBusinessType: '',
        estimatedPickupStartTime: '',
        estimatedPickupEndTime: '',
        estimatedDeliveryStartTime: '',
        estimatedDeliveryEndTime: '',
        destination: '',
        provenance: '',
        lineName: '',
      }
    },
    doneBefore(res) {
      const { pageNum, pageSize } = res.data
      this.mainPages = { pageNum, pageSize }
    },
    masterClickRow(event, activeID, rowRecord) {
      this.goodsListRemoteUrl = page_detail_item
      this.packListRemoteUrl = page_detail_package
      this.goodsListParams = { shipbillId: rowRecord.shipbillId }
      this.packListParams = { shipbillId: rowRecord.shipbillId }
    },
    goodsClickRow(event, activeID, rowRecord) {
      this.packListRemoteUrl = page_package_item
      this.packListParams = { shipbillId: rowRecord.shipbillId, itemId: rowRecord.itemId }
    },
    packClickRow(event, activeID, rowRecord) {
      this.goodsListRemoteUrl = page_item_package
      this.goodsListParams = { shipbillId: rowRecord.shipbillId, packageId: rowRecord.packageId }
    },
    toView(editData = { shipbillId: '' }) {
      this.editId = editData.shipbillId
      this.editType = 'view'
    },

    // 撤销
    onRevoke() {
      this.undo_Dispatch(this.selectedRowKeys)
    },
    toRevoke(record) {
      this.undo_Dispatch([record.shipbillId])
    },
    undo_Dispatch(list) {
      let self = this
      this.$confirm({
        title: '是否撤销',
        onOk() {
          return new Promise((resolve, reject) => {
            self.spinning = true
            undo_Dispatch(list)
              .then((res) => {
                self.spinning = false
                if (res.success) {
                  self.$message.success(res.message)
                  self.onSearch()
                  resolve()
                } else {
                  self.$exception(res)
                  resolve()
                }
              })
              .finally(() => {
                self.spinning = false
                reject()
              })
          })
        },
      })
    },
  },
}
</script>