<template>
  <!-- 商品订单 - 议价单 -->
  <section>
    <a-table :columns="columns" class="srcollYTable expandLeftTable" :scroll="{ x: '100%', y: '190px' }" :pagination="false" :loading="loading" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :expandIcon="expandIcon" :expandIconAsCell="false" :rowKey="(record,index)=>index" :data-source="tableList">
      <!-- 额外的展开行 -->
      <template v-if="btnList.length" #expandedRowRender="{ record }">
        <BtnList :btnList="handleBtnList(record, btnList)" :record="record" class="btn-list-sticky" @click="openComponent" />
      </template>
      <template #applytime="{ text }">
        <span>{{ formatTime(text) }}</span>
      </template>
      <template #transferamount="{ record , text}">
        <div>
          <!-- 审核拒绝 -->
          <a-tooltip placement="bottom" v-if="record.applystatus === 4" overlayClassName="toolTipTableColumn">
            <template #title>
              <span>{{ getScfContractStatusName(text) }}</span>
            </template>
            <span :class="record.applystatus === 4 ? 'red' : 'white'">{{ record.transferamount }}</span>
            <ExclamationCircleOutlined class="ml10 red" v-if="record.applystatus === 4" />
          </a-tooltip>
          <span v-else>{{ record.transferamount }}</span>
        </div>
      </template>
      <template #buyorsell="{ text }">
        <span>{{ getBuyOrSellName(text) }}</span>
      </template>
      <template #applystatus="{ text }">
        <span>{{ getScfContractStatusName(text) }}</span>
      </template>
    </a-table>
    <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" :tableList="tableList" @cancel="closeComponent"></component>
  </section>
</template>

<script lang="ts">
import { BtnListType } from '@/common/components/btnList/interface';
import { ApplyType } from '@/common/constants/enumCommon';
import { getBuyOrSellName, getScfContractStatusName } from '@/common/constants/enumsName';
import { BtnList, defineAsyncComponent, defineComponent, queryTableList } from '@/common/export/commonTable';
import { formatTime } from '@/common/methods';
import { handleDeliveryRelation } from '@/common/setup/deliveryRelation';
import { getRecordItemTab } from '@/common/setup/order/orderData';
import { expandIcon } from '@/common/setup/table/clolumn';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { getAccoutIdList } from '@/services/bus/account';
import { QueryTradePositionRsp } from '@/services/go/ermcp/order/interface';
import { queryTjmdTransferApply } from '@/services/go/Tjmd';
import { QueryTjmdTransferApplyReq, QueryTjmdTransferApplyRsp } from '@/services/go/Tjmd/interface';
import Bus from '@/utils/eventBus/index';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { ref } from 'vue';
import { columns } from './setup';

export default defineComponent({
  name: 'commodity_contract_bargain',
  components: {
    ExclamationCircleOutlined,
    BtnList,
    commodity_contract_bargain_submit_order: defineAsyncComponent(() => import('./components/commodity_contract_bargain_submit_order/index.vue')),
    commodity_contract_bargain_cancel_order: defineAsyncComponent(() => import('./components/commodity_contract_bargain_cancel_order/index.vue')),
    commodity_contract_bargain_refuse_order: defineAsyncComponent(() => import('./components/commodity_contract_bargain_refuse_order/index.vue')),
  },
  setup() {
    // 类型 1-我的申请 2-对方申请
    const margainType = ref<ApplyType>(ApplyType.my);
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryTjmdTransferApplyRsp>();
    // 交割商品
    handleDeliveryRelation();
    // 获取列表数据
    const queryTableAction = () => {
      const param: QueryTjmdTransferApplyReq = {
        applytype: margainType.value,
        accountids: getAccoutIdList(),
      };
      queryTable(queryTjmdTransferApply, param);
    };
    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: () => queryTableAction(),
      recordList: getRecordItemTab(),
    };
    // 切换 我的申请和对方申请
    Bus.$onOnly('bargain', (value: ApplyType) => {
      margainType.value = value;
      queryTableAction();
    });
    function handleBtnList(record: QueryTjmdTransferApplyRsp, btnList: BtnListType[]) {
      if (record.applystatus === 1) {
        if (margainType.value === ApplyType.my) {
          // 我的申请
          return btnList.filter((e) => e.code === 'commodity_contract_bargain_cancel_order');
        } else {
          // 对方的申请
          return btnList.filter((e) => e.code !== 'commodity_contract_bargain_cancel_order');
        }
      } else {
        return [];
      }
    }
    return {
      ...handleComposeOrderTable<QueryTradePositionRsp>(param),
      columns,
      loading,
      tableList,
      formatTime,
      expandIcon,
      handleBtnList,
      getBuyOrSellName,
      getScfContractStatusName,
    };
  },
});
</script>
