<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 #applystatus="{ text }">
          <a>{{ getApplyStatusName(text) }}</a>
        </template>
        <!-- 现货合同类型 -->
        <template #contracttype="{ text }">
          <a>{{ getContractTypeName(text) }}</a>
        </template>
        <!-- 点价类型 -->
        <template #pricetype="{ text }">
          <a>{{ getPriceTypeName(text) }}</a>
        </template>
        <!-- 出入库类型 -->
        <template #inouttype="{ text }">
          <a>{{ InOutTypeName(text) }}</a>
        </template>
        <!-- 对手方 -->
        <template #buynicknameOrsellusername="{ record }">
          <a>{{ handleName(record) }}</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 { QueryAreaStockApply } from '@/services/go/ermcp/inventory-review';
import { Ermcp3AreaStockApply, handleName } from '../setup';
import { getContractTypeName, getPriceTypeName, getApplyStatusName } from '@/common/constants/enumsName';
import { operateApplyTypeName } from '@/views/manage/finance-review/setup';
import { InOutTypeName } from '@/views/manage/inventory-review/setup';
import { Column, ColumnType } from '@/common/setup/table';
import { BtnListType } from '@/common/components/btnList/interface';
import { getTableButton } from '@/common/setup/table/button';
import { useRoute } from 'vue-router';

export default defineComponent({
  name: 'inventory_review_checkin',
  components: {
    MtpTableScroll,
    contextMenu,
    Filter,
    MtpTableButton,
    detail: defineAsyncComponent(() => import('../../components/checkinDetail/index.vue')),
    check: defineAsyncComponent(() => import('../../components/checkinAudit/index.vue')),
    cancel: defineAsyncComponent(() => import('../../components/checkinCancel/index.vue')),
  },
  setup() {
    const { name: routeName } = useRoute();
    // 表格权限按钮
    const buttons = getTableButton();
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<Ermcp3AreaStockApply>();

    // 获取列表数据
    const queryTableAction = () => {
      switch (routeName) {
        // 待审核
        case 'stock_checkpending':
          queryTable(QueryAreaStockApply, { applystatus: '1' });
          break;
        // 已审核
        case 'stock_performance':
          queryTable(QueryAreaStockApply, { applystatus: '2,3' });
          break;
      }
    };

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

    // 获取表头名称
    const getTableName = () => {
      switch (routeName) {
        // 入库审核待审核
        case 'stock_checkpending':
          return 'table_pcweb_stock_aduit_in';
        // 入库审核已审核
        case 'stock_performance':
          return 'table_pcweb_stock_aduit_yin';
      }
    };

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

    function handlePermissionBtn(btnList: BtnListType[], item: Ermcp3AreaStockApply) {
      if (item) {
        const { applystatus } = item;
        if (applystatus === 1) {
          return btnList;
        } else {
          return btnList.filter((e) => e.code === 'detail');
        }
      } else {
        return [];
      }
    }

    return {
      ...handleComposeTable<Ermcp3AreaStockApply>(param),
      handlePermissionBtn,
      loading,
      tableList,
      getApplyStatusName,
      getContractTypeName,
      operateApplyTypeName,
      getPriceTypeName,
      InOutTypeName,
      handleName,
      buttons,
    };
  },
});
</script>
