<template>
  <!-- 采购合同: 特点价合同-待交收合同-->
  <mtp-table-scroll>
    <template #header>
      <filterCustomTable @search="updateColumn"></filterCustomTable>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :loading="loading" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record,index)=>index" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="handleBtnList(record)" :record="record" @click="openComponent" />
        </template>
        <template #pricedqty="{ text, record }">
          <span>{{ handleEnumdic(text, record) }}</span>
        </template>
        <template #accountname="{ record}">
          {{record.nickname}}
        </template>
        <template #unsureqty="{ text, record }">
          <span>{{ handleEnumdic(text, record) }}</span>
        </template>
        <template #qty="{ text, record }">
          <span>{{ handleEnumdic(text, record) }}</span>
        </template>
        <template #unpricedqty="{ text, record }">
          <span>{{ handleEnumdic(text, record) }}</span>
        </template>
        <template #unpayamount="{ record }">
          <span>{{ record.unpayamount.toFixed(2) }}</span>
        </template>
        <template #daikaiamount="{ record }">
          <span>{{ record.daikaiamount.toFixed(2) }}</span>
        </template>
        <template #deliverystartdate,deliveryenddate="{ record }">
          <span>{{ formatValue(formatTime(record.deliverystartdate, 'd') + '--' + formatTime(record.deliveryenddate, 'd')) }}</span>
        </template>
        <template #startdate,enddate="{ record }">
          <span>{{ formatValue(formatTime(record.startdate, 'd') + '--' + formatTime(record.enddate, 'd')) }}</span>
        </template>
        <!-- 业务类型 -->
        <template #biztype="{ text }">
          <a>{{ getBizTypeName(text) }}</a>
        </template>
        <!-- 点价类型 -->
        <template #goodsid="{ text }">
          <span>{{ text > 0 ? '期货' : '现货' }}</span>
        </template>
        <!-- 定价类型 -->
        <template #pricetype="{ text }">
          <a>{{ getPriceTypeName(text) }}</a>
        </template>
        <!-- 价格 -->
        <template #price="{ text }">
          <span>{{ handleNoneValue(text) }}</span>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <!-- 右键 -->
  <contextMenu :contextMenu="contextMenu" @cancel="closeContext" :list="handleBtnList(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 { queryTableList, MtpTableButton, contextMenu, defineAsyncComponent, defineComponent, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import { Ermcp3SellBuyContract } from '../index';
import { filterCustomTable } from '../../components';
import { QueryPurchase } from '@/services/go/ermcp/purchase';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { handleEnumdic } from '../setup';
import { formatTime, formatValue } from '@/common/methods';
import { getTableButton } from '@/common/setup/table/button';
import { useRoute } from 'vue-router';
import { getBizTypeName, getPriceTypeName } from '@/common/constants/enumsName';
import { handleNoneValue } from '@/common/setup/table/tableQuote';
import { useTradeAccount } from '@/hooks/account'
import moment from 'moment';

export default defineComponent({
  name: EnumRouterName.purchase_pending,
  components: {
    MtpTableScroll,
    contextMenu,
    filterCustomTable,
    MtpTableButton,
    detail: defineAsyncComponent(() => import('@/views/information/spot-contract/components/detail/index.vue')), // 详情
    purchase_pending_trade: defineAsyncComponent(() => import('@/views/market/futures/compoments/futures-trade/index.vue')), // 套保交易
    purchase_pending_someprice: defineAsyncComponent(() => import('../../components/someprice/index.vue')), //点价登记
    purchase_performance_settlement: defineAsyncComponent(() => import('../../components/settlement/index.vue')), //交收登记
    purchase_performance_funds: defineAsyncComponent(() => import('../../components/funds/index.vue')), // 款项登记
    purchase_performance_invoice: defineAsyncComponent(() => import('../../components/invoice/index.vue')), // 发票登记
    purchase_performance_storage: defineAsyncComponent(() => import('../../components/storage/index.vue')), // 入库登记
    purchase_performance_ending: defineAsyncComponent(() => import('@/views/information/spot-contract/components/finish/index.vue')), // 完结合同
  },
  setup() {
    const { name: routeName } = useRoute();
    // 权限按钮
    const buttons = getTableButton();
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3SellBuyContract>(true, 2);

    // 获取列表数据
    const queryTableAction = async () => {
      switch (routeName) {
        // 待点价合同
        case 'purchase_pending':
          await queryTable(QueryPurchase, { querytype: 2 });
          break;
        // 待交收合同
        case 'purchase_performance':
          await queryTable(QueryPurchase, { querytype: 3 });
          break;
      }
      // 日期降序排序
      tableList.value = tableList.value.sort((a, b) => moment(b.createtime).valueOf() - moment(a.createtime).valueOf());
    };

    // 获取表头名称
    const getTableName = () => {
      switch (routeName) {
        // 待点价合同
        case 'purchase_pending':
          return 'table_pcweb_purchase_pointprice';
        // 待交收合同
        case 'purchase_performance':
          return 'table_pcweb_purchase_settle';
        default:
          return 'table_pcweb_purchase_all';
      }
    };

    const { tradeAccountList } = useTradeAccount();
    // 处理按钮显示
    function handleBtnList(item: Ermcp3SellBuyContract) {
      if (item && tradeAccountList.length) {
        if (item.goodsid) {
          return buttons;
        }
      }
      return buttons.filter((e) => e.code !== 'purchase_pending_trade');
    }

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

    return {
      ...handleComposeTable<Ermcp3SellBuyContract>(param),
      tableList,
      loading,
      handleEnumdic,
      formatTime,
      formatValue,
      buttons,
      handleBtnList,
      getBizTypeName,
      getPriceTypeName,
      handleNoneValue,
    };
  },
});
</script>
