<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="key"
             :data-source="tableList">
      <!-- 额外的展开行 -->
      <template v-if="btnList.length"
                #expandedRowRender="{ record }">
        <BtnList :btnList="btnList"
                 :record="record"
                 class="btn-list-sticky"
                 @click="openComponent" />
      </template>
      <template #createtime="{ record }">
        <a>{{ formatTime(record.createtime) }}</a>
      </template>
      <!-- 现价 -->
      <template #lastprice="{ record }">
        <a>{{ getLastprice(record) }}</a>
      </template>
      <!-- 持仓金额  -->
      <template #holderprice="{ record }">
        <a>{{ getHolderprice(record) }}</a>
      </template>
      <!-- 持仓盈亏 -->
      <template #profitloss="{ record }">
        <a>{{ getProfitloss(record) }}</a>
      </template>
    </a-table>
    <component :is="componentId"
               v-if="componentId"
               :selectedRow="selectedRow"
               :tableList="tableList"
               @cancel="closeComponent"></component>
  </section>
</template>

<script lang="ts">
import { enumOrderComponents } from '@/common/constants/enumOrderComponents';
import { queryTableList, BtnList, defineAsyncComponent, defineComponent, ModalEnum } from '@/common/export/commonTable';
import { getRecordItemTab } from '@/common/setup/order/orderData';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { formatTime } from '@/common/methods';
import { expandIcon } from '@/common/setup/table/clolumn';
import { queryTradePosition } from '@/services/go/ermcp/order';
import { QueryTradePositionRsp } from '@/services/go/ermcp/order/interface';
import { handleSubcriteQuote } from '@/common/setup/table/tableQuote';
import { getQuoteDayInfoByCode } from '@/services/bus/goods';
import { handleDeliveryRelation } from '@/common/setup/deliveryRelation';

export default defineComponent({
    name: enumOrderComponents.commodity_contract_summary,
    components: {
        BtnList,
        [ModalEnum.commodity_contract_summary_settlement]: defineAsyncComponent(() => import('./components/commodity_contract_summary_settlement/index.vue')),
        [ModalEnum.commodity_contract_summary_transfer]: defineAsyncComponent(() => import('./components/commodity_contract_summary_transfer/index.vue')),
    },
    setup() {
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList<QueryTradePositionRsp>();
        const { subscribeAction } = handleSubcriteQuote();
        // 交割商品
        handleDeliveryRelation();
        // 获取列表数据
        const queryTableAction = () => {
            queryTable(queryTradePosition).then((res) => {
                const goodsList = res.map((el) => {
                    return el.goodscode;
                });
                //  行情订阅
                subscribeAction(goodsList);
            });
        };
        // 表格通用逻辑
        const param: ComposeOrderTableParam = {
            queryFn: queryTableAction,
            tableName: 'table_pcweb_spot_trade_bottom_commodity_contract_summary',
            recordList: getRecordItemTab(),
        };
        // 现价
        function getLastprice({ goodscode, decimalplace }: QueryTradePositionRsp) {
            const item = getQuoteDayInfoByCode(goodscode);
            let result = '';
            if (item) {
                const { last, presettle } = item;
                if (last) {
                    result = last.toFixed(decimalplace); // 最新
                } else if (presettle) {
                    result = presettle.toFixed(decimalplace); // 昨结
                }
            }
            return result;
        }
        // 持仓金额 = 数量 * 现价 * 合约单位
        function getHolderprice(record: QueryTradePositionRsp) {
            const price = getLastprice(record);
            if (price !== '--') {
                const { decimalplace, agreeunit, curpositionqty } = record;
                return (+price * curpositionqty * agreeunit).toFixed(decimalplace);
            } else {
                return price;
            }
        }
        /// 持仓盈亏 买方向 = (最新价 - 持仓均价) * 买期末头寸 * 合约单位；卖方向 = (持仓均价 - 最新价) * 卖期末头寸 * 合约单位
        function getProfitloss(record: QueryTradePositionRsp) {
            const price = getLastprice(record);
            if (price !== '--') {
                const { averageprice, decimalplace, agreeunit, curpositionqty } = record;
                return ((+price - averageprice) * curpositionqty * agreeunit).toFixed(decimalplace);
            } else {
                return price;
            }
        }
        return {
            ...handleComposeOrderTable<QueryTradePositionRsp>(param),
            loading,
            tableList,
            formatTime,
            expandIcon,
            getLastprice,
            getHolderprice,
            getProfitloss,
        };
    },
});
</script>

<style lang="less">
</style>;
