/*
 *@description:
 *@author: hudw
 *@date: 2022-03-14 15:14:53
*/
<template>
  <p-card :bordered="false" :title="title" style="flex:1">
    <a-space slot="extra">
      <slot name="extra" />
    </a-space>

    <p-table
      :id="$route.meta.menuId + '_main'"
      :sign="true"
      autoColWidth
      rowSelectFixed
      :activeKey="activeID"
      rowKey="shipbillDetailId"
      :scroll="{ x:true, y: 400 }"
      :selectedRowKeys="selectedRowKeys"
      ref="mainTable"
      bordered
      :customRow="customRow"

      :columns="mx_columns"
      :doneBefore="doneBefore"
      :remoteUrl="mx_remoteUrl"
      :remoteBefore="remoteBefore"
      dragle
      :rowSelectType="2"
      v-on="$listeners"
    >
      <slot  name="operation" slot="operation" slot-scope="text, record" :record="record" :text="text" />
    </p-table>
  </p-card>
</template>
<script>
import { customRow } from '@/blocks/components/scheduling/js/table/tableConfig.js';

import setMainColumns from './tableConfig';
export default {
  name:'mainTable',
  props: {
    doneBefore: { type: Function },
    options: {
      type: Object,
      default: () => {},
    },
    operation: {
      type: null,
    },
    selectedRowKeys: {
      type: Array,
      default: () => [],
    },
    tableExtendedField: {
      type: Array,
    },
    getFieldsValue: {
      type: Function,
    },

    activeID: {
      type: String,
    },
    title: {
      type: null,
    },

    url: {
      type: null,
    },
    columns: {
      type: null,
    },
    setMainColumns: {
      type: Function,
      default:setMainColumns
    }
  },

  data() {
    return {
      customRow,
      confirmLoading: false,
      dataSource: [],
      mx_remoteUrl: {
        method: 'post',
        url: this.url,
      },
      mx_remoteParams: {},
      mx_columns: !this.columns ? this.setMainColumns(this, this.tableExtendedField) : this.columns,
    };
  },
  mounted() {},
  watch: {
    tableExtendedField(value) {
      if (value.length > 0) {
        this.mx_columns = !this.columns ? this.setMainColumns(this, value) : this.columns
      }
    },
    // activeID(v) {
    //   console.log("activeID", v)
    //   this.$refs.mainTable.activeID = v
    // }
  },
  methods: {
    print_item() {
      alert('开发中...');
      this.$emit('print', record);
    },
    copy_item(record) {
      this.$emit('copy', record);
    },
    submit_item(record) {
      this.$emit('submit', [record.id]);
    },
    show_info_item(record) {
      this.$emit('view', record);
    },

    update_item(record) {
      this.$emit('update', [record]);
    },

    reload(args) {
      return this.$refs.mainTable.reload(args);
    },
    remoteBefore(vNode, remoteUrl) {
      let searchBar = this.$parent.$refs.searchBar;
      if (searchBar) {
        const { getFieldsValue } = searchBar;
        console.log(remoteUrl)
       let data= getFieldsValue()
        remoteUrl.data = { ...remoteUrl.data, ...data };


        for (let key in remoteUrl.data) {
          let item=remoteUrl.data[key]
          if (Array.isArray(item) ) {
            let keys = key.split('_');
            if (keys.length > 0) {
              remoteUrl.data[keys[0]]= item[0]
              remoteUrl.data[keys[1]] = item[1]
              delete remoteUrl.data[key]
            }
          }

        }


      }
    },
  },
};
</script>
