<template>
  <!-- 平仓-->
  <Drawer :title="'平仓'" :placement="'right'" class="bottom486" :visible="visible" @cancel="cancel">
    <div class="listed c_c_s_s">
      <a-form class="inlineForm dialogForm" ref="formRef" :model="formState" :rules="rules">
        <div class="formBar">
          <div class="formtop">
            <div class="firstTitle">
              <span>合约：{{ selectedRow.goodscode }}/{{ selectedRow.goodsname }}</span>
            </div>
            <div class="secondLine">
              <div class="left">持仓单号/方向</div>
              <div class="middle">数量/价格/金额</div>
              <div class="right">到期日/盈亏</div>
            </div>
            <div class="lineBar">
              <div class="line1">
                <div class="name">
                  {{ selectedRow.tradeid }}
                  <template v-if="selectedRow.holderdays">
                    <span class="red">{{ selectedRow.holderdays }}</span>天后可平
                  </template>
                </div>
                <div class="date">{{ formatTime(selectedRow.expiredate, 'd') }}</div>
              </div>
              <div class="line2">
                <div class="left">{{ selectedRow.buyorsell === BuyOrSell.buy ? '买入' : '卖出' }}</div>
                <div class="middle">
                  <div>{{ selectedRow.holderqty }}</div>
                  <div>{{ selectedRow.holderprice }}</div>
                  <div>{{ selectedRow.holderamount }}</div>
                </div>
                <div :class="['right', +useProfit(selectedRow, goodscode) > 0 ? 'up-quote-color' : 'down-quote-color']">{{ useProfit(selectedRow, goodscode) }}</div>
              </div>
            </div>
          </div>
        </div>
        <div class="fixedBtns">
          <a-row :gutter="24">
            <a-col :span="24" class="mt12">
              <a-form-item label="估算价" class="inputIconBox mb10 not-copy">
                <!-- 估算价取标的商品行情的最新价 -->
                <span class="white">{{ getQuoteDayInfoByCodeFindPrice(goodscode) }}</span>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="估算金额" class="mb10 not-copy">
                <span class="white">{{ getMoney() }}</span>
              </a-form-item>
            </a-col>
          </a-row>
          <a-form-item class="btnCenter mt10">
            <a-button class="listedBtn" :loading="loading" :disabled="loading" @click="submit">提交</a-button>
            <a-button class="ml10 cancelBtn" @click="cancel">取消</a-button>
          </a-form-item>
        </div>
      </a-form>
    </div>
  </Drawer>
</template>

<script lang="ts">
import Drawer from '@/common/components/drawer/index.vue';
import UploadImg from '@/common/components/uploadImg/index.vue';
import { BuyOrSell } from '@/common/constants/enumCommon';
import { ModalEnum } from '@/common/constants/modalNameEnum';
import { formatTime } from '@/common/methods';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { validateAction } from '@/common/setup/form';
import { _closeModal } from '@/common/setup/modal/modal';
import { getSelectedAccountId } from '@/services/bus/account';
import { getQuoteDayInfoByCodeFindPrice } from '@/services/bus/goods';
import { geLoginID_number } from '@/services/bus/login';
import { QueryTradeHolderDetailRsp } from '@/services/go/order/interface';
import { QueryQuoteGoodsListRsp } from '@/services/go/Tjmd/interface';
import { holderClose } from '@/services/proto/warehousetrade';
import { HolderCloseReq } from '@/services/proto/warehousetrade/interface';
import { MinusOutlined, PlusOutlined } from '@ant-design/icons-vue';
import Long from 'long';
import { defineComponent, PropType, ref } from 'vue';
import { findGoodsCode } from '../../setup';
import { useProfit } from '../setup';
import { FormState } from './interface';
import { handleForm, usePrice } from './setup';

export default defineComponent({
  name: ModalEnum.commodity_contract_summary_settlement,
  components: { Drawer, UploadImg, PlusOutlined, MinusOutlined },
  emits: ['cancel', 'update'],
  props: {
    selectedRow: {
      type: Object as PropType<QueryTradeHolderDetailRsp>,
      default: {},
    },
    swapList: {
      type: Array as PropType<QueryQuoteGoodsListRsp[]>,
      default: [],
    },
  },
  setup(props, context) {
    const goodscode = findGoodsCode(props.selectedRow.goodsid, props.selectedRow.goodscode, props.swapList);

    const { visible, cancel } = _closeModal(context);
    const { rules, formState, formRef } = handleForm();
    const loading = ref<boolean>(false);
    // 估算金额=估算价*平仓数量*合约单位。
    function getMoney() {
      let result = '--';
      const lastPrice = getQuoteDayInfoByCodeFindPrice(goodscode);
      if (lastPrice && lastPrice !== '--') {
        const { holderqty, agreeunit, decimalplace } = props.selectedRow;
        result = (+lastPrice * holderqty * agreeunit).toFixed(decimalplace);
      }
      return result;
    }

    const toFixed0 = (value: number) => +value.toFixed(0);

    function submit() {
      validateAction<FormState>(formRef, formState).then((res) => {
        const param: HolderCloseReq = {
          TradeID: Long.fromString(props.selectedRow.tradeid),
          LoginID: geLoginID_number()!, // uint64 登陆账号
          AccountID: getSelectedAccountId(), // uint64 交易账号
          GoodsID: props.selectedRow.goodsid,
          MarketID: props.selectedRow.marketid, // uint32 市场ID
          BuyOrSell: props.selectedRow.buyorsell,
          OperatorID: geLoginID_number()!, // uint64 操作员账号ID
        };
        requestResultLoadingAndInfo(holderClose, param, loading, ['平仓成功', '平仓失败:']).then(() => {
          cancel(true);
        });
      });
    }
    return {
      visible,
      cancel,
      submit,
      loading,
      toFixed0,
      rules,
      formState,
      formRef,
      ...usePrice(formState),
      BuyOrSell,
      formatTime,
      useProfit,
      goodscode,
      getQuoteDayInfoByCodeFindPrice,
      getMoney,
    };
  },
});
</script>

<style lang="less" scoped>
.c_c_s_s {
    background: @m-black40;
    width: 100%;
    height: 100%;
    position: relative;
    .formBar {
        padding: 0;
        height: calc(100% - 170px);
        background: @m-black41;
        .formtop {
            width: 100%;
            padding: 0 20px;
            .flex;
            flex-direction: column;
            .firstTitle {
                width: calc(100% + 40px);
                height: 40px;
                line-height: 40px;
                margin-left: -20px;
                padding: 0 20px;
                font-size: 14px;
                color: @m-white6;
                border-bottom: 1px solid @m-black42;
            }
            .secondLine {
                width: 100%;
                height: 40px;
                line-height: 40px;
                display: inline-flex;
                > div {
                    flex: 1;
                    font-size: 14px;
                    color: @m-grey1;
                }
                .left {
                    text-align: left;
                }
                .middle {
                    text-align: center;
                }
                .right {
                    text-align: right;
                }
            }
            .lineBar {
                width: 100%;
                min-height: 100px;
                padding-left: 15px;
                padding-right: 10px;
                background: @m-blue19;
                margin-bottom: 10px;
                .rounded-corners(5px);
                .line1 {
                    display: inline-flex;
                    user-select: none;
                    width: 100%;
                    height: 40px;
                    line-height: 40px;
                    font-size: 12px;
                    color: @m-white6;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                    overflow: hidden;
                    border-bottom: 1px solid @m-blue20;
                    > div {
                        align-self: center;
                        align-items: center;
                    }
                    .name {
                        margin-left: 10px;
                    }
                    .date {
                        flex: 1;
                        text-align: right;
                        font-size: 14px;
                        color: @m-grey1;
                    }
                }
                .line2 {
                    width: 100%;
                    user-select: none;
                    padding: 12px 0 14px 0;
                    display: inline-flex;
                    .left {
                        width: 25%;
                        color: @m-white6;
                        font-size: 14px;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        overflow: hidden;
                        line-height: 34px;
                        // }
                    }
                    .middle {
                        width: 50%;
                        display: inline-flex;
                        justify-content: space-between;
                        padding: 0 10px;
                        font-size: 16px;
                        color: @m-white6;
                        line-height: 34px;
                        > div {
                            white-space: nowrap;
                        }
                    }
                    .right {
                        width: 25%;
                        font-size: 16px;
                        text-align: right;
                        line-height: 34px;
                        white-space: nowrap;
                    }
                }
            }
        }
    }
    .fixedBtns {
        padding-top: 0;
        left: 0;
        right: 0;
        padding-left: 20px;
        padding-right: 20px;
    }
    .formbottom {
        width: 100%;
        .flex;
        flex-direction: column;
        background: @m-black40;
        padding: 0 18px 0 20px;
        .line1 {
            width: 100%;
            padding: 0 12px 0 16px;
            height: 36px;
            line-height: 36px;
            display: inline-flex;
            justify-content: space-between;
            color: @m-grey1;
            font-size: 14px;
        }
        .line2 {
            width: 100%;
            height: 35px;
            line-height: 34px;
            padding-left: 15px;
            padding-right: 10px;
            background: @m-black43;
            .rounded-corners(5px);
            font-size: 14px;
            color: @m-grey1;
            display: inline-flex;
            justify-content: space-between;
            > div {
                align-self: baseline;
                align-items: baseline;
            }
            .right {
                display: inline-flex;
                color: @m-yellow6;
                div:last-child {
                    font-size: 16px;
                    margin-left: 2px;
                }
            }
        }
        .line3 {
            margin-top: 16px;
            width: 100%;
            height: 15px;
            line-height: 15px;
            font-size: 14px;
            text-align: right;
            color: @m-white6;
        }
    }
    .inputIconBox {
        .ant-form-item-children {
            .anticon-plus {
                right: 156px;
            }
        }
    }
}
</style>;