<template>
  <!-- 库存审核 入库-->
  <div class="inventory_review_checkin"
       :loading="loading">
    <Filter @search="updateColumn"
            :inOrOut="'in'">
    </Filter>
    <a-table :columns="columns"
             class="srcollYTable"
             :scroll="{ x: '100%', y: 'calc(100vh - 163px)' }"
             :pagination="false"
             :expandedRowKeys="expandedRowKeys"
             :customRow="Rowclick"
             rowKey="key"
             :data-source="tableList">
      <!-- 额外的展开行 -->
      <template #expandedRowRender="{ record }">
        <BtnList :btnList="handlePermissionBtn(firstBtn, record)"
                 :record="record"
                 class="btn-list-sticky"
                 @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>
    <!-- 右键 -->
    <contextMenu :contextMenu="contextMenu"
                 @cancel="closeContext"
                 :list="handlePermissionBtn(firstBtn, selectedRow)">
    </contextMenu>
    <component :is="componentId"
               v-if="componentId"
               :selectedRow="selectedRow"
               @cancel="closeComponent"></component>
  </div>
</template>

<script lang="ts">
import { BtnList, contextMenu, defineAsyncComponent, defineComponent, ModalEnum, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';

import { Filter, CheckinAudit, CheckinCancel, CheckinDetail } from '../../components';

import { queryTableList, 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 { EnumRouterName } from '@/common/constants/enumRouterName';
import { BtnListType } from '@/common/components/btnList/interface';
const Detail = defineAsyncComponent(() => import('../../components/checkinDetail/index.vue'));
const Audit = defineAsyncComponent(() => import('../../components/checkinAudit/index.vue'));
const Cancel = defineAsyncComponent(() => import('../../components/checkinCancel/index.vue'));

export default defineComponent({
    name: 'inventory_review_checkin',
    components: {
        contextMenu,
        Filter,
        BtnList,
        [ModalEnum.detail]: Detail,
        [ModalEnum.inventory_review_checkin_audit]: Audit,
        [ModalEnum.inventory_review_checkin_cancel]: Cancel,
    },
    setup() {
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList({ inouttype: '1,3' });
        // 获取列表数据
        const queryTableAction = () => queryTable();
        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 param: ComposeTableParam = {
            queryFn: queryTableAction,
            menuType: EnumRouterName.inventory_review_checkin,
            tableName: 'table_pcweb_stock_aduit_in',
            tableFilterKey: ['contracttype', 'contractno', 'deliverygoodsname'],
            tableFilterCB: filtrFn,
            isDetail: true,
        };
        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,
        };
    },
});
</script>

<style lang="less">
.inventory_review_checkin {
}
</style
>;

