/*
 *@description:
 *@author: hudw
 *@date: 2022-03-14 15:14:53
*/
<template>
  <p-card :bordered="false" style="margin-bottom: 10px" :bodyStyle="{minHeight:'438px'}">
    <a-space slot="title">
      {{ title }}
      <template v-if="mode && $listeners.modeCheck">
        <p-button :type="mode === 1 ? 'primary' : 'default'" @click="handelModeCheck($event, 1)">运单模式</p-button>
        <p-button v-if="shipperBtn" :type="mode === 5 ? 'primary' : 'default'" @click="handelModeCheck($event, 5)">发货方模式</p-button>
        <p-button v-if="consigneeBtn" :type="mode === 2 ? 'primary' : 'default'" @click="handelModeCheck($event, 2)">收货方模式</p-button>
        <slot name="afterBtn" :handelChecked="handelModeCheck" />
      </template>
    </a-space>
    <a-space slot="extra">
      <slot name="extra" />
      <a-dropdown>
        <p-button>更多<a-icon :type="'down'" /></p-button>
        <a-menu slot="overlay" @click="handleMenuClick">
          <a-menu-item :key="1">
            <export-button
            type="link"
            :getSearchValue="()=>$parent.$refs.searchBar&&$parent.$refs.searchBar.getFieldsValue()"
            :ids="$attrs.exportUrls&&$attrs.exportUrls.ids"
            :pages="mainPages"
            downloadFileName="订单信息" v-bind="$attrs.exportUrls">导出</export-button>
          </a-menu-item>
          <!-- v-action:print -->
          <a-menu-item :key="3" v-action:print>
            <p-button type="link" :disabled="selectedRowKeys.length === 0" @click="print_item">打印 </p-button>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </a-space>
      <!-- :selectedRowKeys="selectedRowKeys" -->

    <t-table
      customScrollbar
       autoColWidth
      :extendedObjectCode="'15'"
      :id="$route.meta.menuId + '-main-table-' + mode"
      :sign="true"
      rowSelectFixed
      :activeKey="activeID"
      :rowKey="rowKey"
      :dataSource="dataSource"
      :scroll="{ x: true}"
      :vScrollX="{enabled:true}"
      :vScrollY="{enabled:true,rows:10}"
      ref="mainTable"
      :rowSelectType="rowSelectType"
      bordered
      autoLoad
      :columns="mx_columns"
      :doneBefore="t_doneBefore"
      :remoteUrl="mx_remoteUrl"
      :remoteBefore="remoteBefore"
      dragle
      v-on="$listeners"
    >
      <template slot="operation" slot-scope="text, record">
        <slot name="operation" :text="text" :record="record"> </slot>
      </template>
    </t-table>
  </p-card>
</template>
<script>
import { setMainColumns } from './tableConfig'//运单模式
import { setSalesColumns } from './tableConfig_sales.js'//销售单模式
import { setAppointmentColumns } from './tableConfig_appointment'//预约单模式
import { setConsignColumns } from './tableConfig_consign' //托运单信息
import { setShipperColumns } from './tableConfig_shipper' //发货方
import { setNextColumns } from './tableConfig_next.js'//收货方
import { setTruckingColumns } from './tableConfig_trucking.js' //派车单信息
import { UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
import exportButton from '@/blocks/components/export-modal/modal.index.vue'

export default {
  name: 'main-table',
  components: { exportButton },
  props: {

    title: {
      type: null,
    },
    allShipbill: {
      type: Boolean,
      default: false,
    },
    mode: {
      type: null,
      default: 1, //1运单模式,2收货方模式,3派车单信息
    },
    shipperBtn: {
      type: Boolean,
      default: false,
    },
    consigneeBtn: {
      type: Boolean,
      default: true,
    },
    rowKey: {
      type: null,
      default: '_row_count',
    },
    url: {
      type: null,
    },
    deliveryTime: {
      type: Boolean,
      default: false,

    },

    doneBefore: { type: Function },
    options: {
      type: Object,
      default: () => {},
    },
    tableColumnsList: {
       type: Array,
    },
    operation: {
      type: null,
    },
    selectedRowKeys: {
      type: Array,
      default: () => [],
    },
    tableExtendedField: {
      type: Array,
    },
    getFieldsValue: {
      type: Function,
    },
    rowSelectType: {
      type: null,
      default:2
    },
    activeID: {
      type: null,
    },
  },

  data() {
    return {
      UPCLOUD_RDC_TMS_BUSINESS_SERVICE,
      confirmLoading: false,
      mainPages: { pageNum: 1, pageSize: 15 },

      dataSource: [],
      mx_remoteUrl: {
        method: 'post',
        url: Array.isArray(this.url) ? this.url[this.mode - 1] : this.url,
      },
      mx_remoteParams: {},
      mx_columns: this.setColumns(this.mode),
    }
  },
  mounted() {
  },
  watch: {
    tableExtendedField(value) {
      if (value.length > 0) {
        this.mx_columns = this.setColumns(this.mode, value)
      }
    },

    // activeID(v) {
    //   console.log("activeID", v)
    //   this.$refs.mainTable.activeID = v
    // }

  },
  methods: {
    // onReset() {
    //   this.mx_columns = this.setColumns(this.mode)

    // },
    setColumns(type, exp = []) {

                                   //1运单模式,         2收货方模式,     3 派车单信息        4 托运单信息         5 发货方        6,预约单模式             7,销售单模式
      let col = [setMainColumns, setMainColumns, setNextColumns, setTruckingColumns, setConsignColumns, setShipperColumns, setAppointmentColumns,setSalesColumns]
      if (this.tableColumnsList?.length && this.tableColumnsList[type]) {
        let cl=this.tableColumnsList[type]
        if( typeof cl ==='function' )
        return cl(this,exp)
      } else if (typeof cl === 'object') {
        return cl

      }

      let _this=this
      return col[type](_this,exp)
    },
    t_doneBefore(res) {
      if (res.success) {
        this.doneBefore && this.doneBefore(res)
        const { pageNum, pageSize } = res.data
        this.mainPages = { pageNum, pageSize }
      }
    },
    clearTableData() {
     return this.$refs.mainTable.tData=[]
    },
    handelModeCheck(eve, type) {
      try {
        this.mx_columns = this.setColumns(+type,this.tableExtendedField)
        this.mx_remoteUrl.url = Array.isArray(this.url) ? this.url[type - 1] : this.url
        this.$emit('modeCheck', eve, type)
      } catch (err) {
         throw new Error(err)
      }
    },
    print_item() {
      this.$emit('print', this.selectedRowKeys)
    },
    copy_item(record) {
      this.$emit('copy', record)
    },
    submit_item(record) {
      this.$emit('submitItem', [record.shipbillId])
    },
    show_info_item(record) {
      this.$emit('view', record)
    },
    delete_item(record) {
      this.$emit('delete', [record.shipbillId])
    },
    update_item(record) {
      this.$emit('update', record)
    },
    reload(args) {
      this.$refs.mainTable.tData = []
      return this.$refs.mainTable.reload(args)
    },
    handleMenuClick() {},
    remoteBefore(vNode, remoteUrl) {
      let searchBar = this.$parent.$refs.searchBar
      if (!searchBar) {
        this.$parent.$parent.$refs.searchBar
      }
      let clearChildrenTableAll = this.$parent.clearChildrenTableAll
      clearChildrenTableAll && clearChildrenTableAll(true)
      if (searchBar) {
        const { getFieldsValue } = searchBar
        remoteUrl.data = { ...remoteUrl.data, ...getFieldsValue() }
      }
    },
  },
}
</script>


