<template>
  <!-- 采购合同: 新增合同-已完结合同-->
  <mtp-table-scroll>
    <template #header>
      <filterCustomTable @search="updateColumn">
        <mtp-table-button :buttons="firstBtn" @click="openComponent" />
      </filterCustomTable>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :pagination="false" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record,index)=>index" :data-source="tableList" :scroll="scroll">
        <!-- 额外的展开行 -->
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="handleBtnList(record)" :record="record" @click="openComponent" />
        </template>
        <template #biztype="{ text }">
          <a>{{ getBizTypeName(text) }}</a>
        </template>
        <template #pricetype="{ text }">
          <span>{{ getPriceTypeName(text) }}</span>
        </template>
        <template #contracctstatus="{ text }">
          <a>{{ getContractStatusName(text) }}</a>
        </template>
        <template #contracttype="{ text }">
          <a>{{ getContractTypeName(text) }}</a>
        </template>
        <!-- 对手方 -->
        <template #negative="{ record }">
          <a>{{ record.contracttype === 1 ? record.sellusername : record.buyusername }}</a>
        </template>
        <!-- 点假期 -->
        <template #startdate="{ record }">
          <span>{{ formatValue(formatTime(record.startdate, 'd') + '--' + formatTime(record.enddate, 'd')) }}</span>
        </template>
        <!-- 交收期 -->
        <template #deliverystartdate="{ record }">
          <span>{{ formatValue(formatTime(record.deliverystartdate, 'd') + '--' + formatTime(record.deliveryenddate, 'd')) }}</span>
        </template>
        <!-- 业务员 -->
        <template #saleuserid="{ record }">
          <span>{{ findManagerName(record.saleuserid) }}</span>
        </template>
        <!-- 跟单员 -->
        <template #meruserid="{ record }">
          <span>{{ findManagerName(record.meruserid) }}</span>
        </template>
        <!-- 合同量 -->
        <template #qty="{ text, record }">
          <span>{{ handleEnumdic(text, record) }}</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" :contractType="1" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import { computed } from 'vue'
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { queryTableList, MtpTableButton, contextMenu, defineAsyncComponent, defineComponent, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import { filterCustomTable } from '../../components';
import { formatTime, formatValue } from '@/common/methods';
import { queryBusinessManager, findManagerName, tableFilterCB, handleEnumdic } from '../setup';
import { getPriceTypeName, getBizTypeName, getContractStatusName, getContractTypeName } from '@/common/constants/enumsName';
import { Ermcp3ContractRsp } from '@/services/go/ermcp/spot-contract/interface';
import { QuerySpotContract } from '@/services/go/ermcp/spot-contract';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { getTableButton } from '@/common/setup/table/button';
import { useRoute } from 'vue-router';
import moment from 'moment';

export default defineComponent({
  name: EnumRouterName.spot_contract_unsubmitted,
  components: {
    MtpTableScroll,
    filterCustomTable,
    contextMenu,
    detail: defineAsyncComponent(() => import('../../components/detail/index.vue')),
    add: defineAsyncComponent(() => import('../../components/add/index.vue')),
    resubmit: defineAsyncComponent(() => import('../../components/modify/index.vue')),
    cancel: defineAsyncComponent(() => import('../../components/cancel/index.vue')),
    delete: defineAsyncComponent(() => import('../../components/delete/index.vue')),
    MtpTableButton,
  },
  setup() {
    const { name: routeName } = useRoute();
    // 权限按钮
    const firstBtn = getTableButton(['add']);
    const secondBtn = getTableButton(['add'], true);
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3ContractRsp>();

    // 获取列表数据
    const queryTableAction = async () => {
      switch (routeName) {
        // 新增合同
        case 'purchase_contract':
          await queryTable(QuerySpotContract, { contracttype: 1, querytype: '1,2' });
          break;
        // 已完结合同
        case 'purchase_finish':
          await queryTable(QuerySpotContract, { contracttype: 1, querytype: '4' });
          break;
      }
      // 日期降序排序
      tableList.value = tableList.value.sort((a, b) => moment(b.createtime).valueOf() - moment(a.createtime).valueOf());
      // 获取 业务账户
      queryBusinessManager();
    };

    // 处理根据状态显示对应按钮
    const handleBtnList = (item: Ermcp3ContractRsp) => {
      if (item) {
        switch (item.contracctstatus) {
          case 1: // 待审核
          case 2: // 执行中
          case 3: // 正常完结
          case 5: // 异常完结
            return secondBtn.filter((e) => e.code !== 'delete');
          case 4: // 审核拒绝
          case 6: // 已撤回
            return secondBtn.filter((e) => e.code !== 'cancel');
          default:
            return secondBtn;
        }
      }
      return []
    };

    // 表格通用逻辑
    const param: ComposeTableParam = {
      queryFn: queryTableAction,
      tableName: 'table_pcweb_delivery',
      tableFilterCB,
    };

    const compose = handleComposeTable<Ermcp3ContractRsp>(param);
    const columns = computed(() => {
      return compose.columns.value.map((item) => {
        if (item.dataIndex === 'negative') {
          item.title = '销售方';
        }
        return item;
      })
    })

    return {
      ...compose,
      columns,
      loading,
      tableList,
      getBizTypeName,
      getPriceTypeName,
      getContractStatusName,
      getContractTypeName,
      formatTime,
      formatValue,
      findManagerName,
      handleEnumdic,
      firstBtn,
      secondBtn,
      handleBtnList,
    };
  },
});
</script>