<template>
  <!-- 管理-业务审核-点价 -->
  <mtp-table-scroll>
    <template #header>
      <Filter @search="updateColumn"></Filter>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record,index)=>index" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="handlePermissionBtn(buttons, record)" :record="record" @click="openComponent" />
        </template>
        <template #pricetype="{ text }">
          <a>{{ getPriceTypeName(text) }}</a>
        </template>
        <template #applystatus="{ text }">
          <a>{{ getApplyStatusName(text) }}</a>
        </template>
        <template #contracttype="{ text }">
          <a>{{ getContractTypeName(text) }}</a>
        </template>
        <template #contractstatus="{ text }">
          <a>{{ getContractStatusName(text) }}</a>
        </template>
        <template #buyusernameOrsellusername="{ record }">
          <a>{{ getAnalogueName(record.contracttype, record.buyusername, record.sellusername) }}</a>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <!-- 右键 -->
  <contextMenu :contextMenu="contextMenu" @cancel="closeContext" :list="handlePermissionBtn(buttons, selectedRow)"> </contextMenu>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { MtpTableButton, contextMenu, defineAsyncComponent, defineComponent, handleComposeTable, ComposeTableParam, queryTableList } from '@/common/export/commonTable';
import { Filter } from '../../components';
import { getPriceTypeName, getContractStatusName, getContractTypeName, getApplyStatusName } from '@/common/constants/enumsName';
import { getAnalogueName } from '@/views/information/spot-contract/setup';
import { Column, ColumnType } from '@/common/setup/table';
import { BtnListType } from '@/common/components/btnList/interface';
import { getTableButton } from '@/common/setup/table/button';
import { QueryBusinessDj } from '@/services/go/ermcp/business-review';
import { QryBusinessDjRsp } from '@/services/go/ermcp/business-review/interface';
import { useRoute } from 'vue-router';
import moment from 'moment';

export default defineComponent({
  name: 'business-review-dj',
  components: {
    MtpTableScroll,
    contextMenu,
    Filter,
    MtpTableButton,
    detail: defineAsyncComponent(() => import('../../components/somepriceDetail/index.vue')),
    check: defineAsyncComponent(() => import('../../components/somepriceAudit/index.vue')),
    cancel: defineAsyncComponent(() => import('../../components/somepriceCancel/index.vue')),
  },
  setup() {
    const { name: routeName } = useRoute();
    // 权限按钮
    const buttons = getTableButton();

    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QryBusinessDjRsp>();

    // console.log(typeof tableList,11)
    // console.log(tableList.value.applytime,11)
    // 获取列表数据
    const queryTableAction = () => {
      switch (routeName) {
        case 'someprice_checkpending':
          queryTable(QueryBusinessDj, { applystatus: '1' }).then((res) => {
            tableList.value.sort((a, b) => {
              return moment(b.audittime).valueOf() - moment(a.audittime).valueOf();
            });
          });
          break;
        case 'someprice_performance':
          queryTable(QueryBusinessDj, { applystatus: '2,3' }).then((res) => {
            tableList.value.sort((a, b) => {
              return moment(b.audittime).valueOf() - moment(a.audittime).valueOf();
            });
          });
      }
    };

    // 获取表头名称
    const getTableName = () => {
      switch (routeName) {
        // 待审核
        case 'someprice_checkpending':
          return 'table_pcweb_business_aduit_dj';
        // 已审核
        case 'someprice_performance':
          return 'table_pcweb_business_aduit_ydj';
      }
    };

    const filtrFn = (e: Column, item: ColumnType, filtered: any) => {
      if (e.columnfield === 'buyusernameOrsellusername') {
        item.onFilter = (value: string, record: QryBusinessDjRsp) => {
          const { contracttype, buyusername, sellusername } = record;
          if (contracttype === 1) {
            // 采购
            return sellusername.includes(value);
          } else {
            return buyusername.includes(value);
          }
        };
        item.filteredValue = filtered.buyusernameOrsellusername || null;
      }
    };

    // 表格通用逻辑
    const param: ComposeTableParam = {
      queryFn: queryTableAction,
      tableName: getTableName(),
      tableFilterCB: filtrFn,
    };

    function handlePermissionBtn(btnList: BtnListType[], item: QryBusinessDjRsp) {
      if (item) {
        const { applystatus } = item;
        if (applystatus === 1) {
          return btnList;
        } else {
          return btnList.filter((e) => e.code === 'detail');
        }
      } else {
        return [];
      }
    }
    return {
      ...handleComposeTable<QryBusinessDjRsp>(param),
      handlePermissionBtn,
      loading,
      tableList,
      getPriceTypeName,
      getApplyStatusName,
      getContractTypeName,
      getContractStatusName,
      getAnalogueName,
      buttons,
    };
  },
});
</script>
