<template>
  <!-- 现货合同审核-->
  <a-modal class="add-custom custom-detail"
           title="现货合同审核"
           v-model:visible="visible"
           centered
           :maskClosable="maskClosableFlag"
           @cancel="cancel"
           width="890px">
    <template #footer>
      <a-button key="submit"
                class="cancelBtn"
                @click="refuse">审核拒绝</a-button>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="submit">审核通过</a-button>
    </template>
    <a-form class="inlineForm"
            ref="formRef"
            :model="formState"
            :rules="rules">
      <fieldset class="formFieldSet">
        <legend>基本信息</legend>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="合同编号">
              <span class="white">{{ formatValue(selectedRow.contractno) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同类型">
              <span class="white">{{ getContractTypeName(selectedRow.contracttype) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="业务类型">
              <span class="white">{{ getBizTypeName(selectedRow.biztype) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购方">
              <span class="white">{{ formatValue(selectedRow.buyusername) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="销售方">
              <span class="white">{{ formatValue(selectedRow.sellusername) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同附件">
              <span class="white"
                    @click="previewImg(selectedRow.attachment)">{{ formatValue(getImgName(selectedRow.attachment)) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="状态">
              <span class="green">{{ formatValue(getContractStatusName(selectedRow.contracctstatus)) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
      </fieldset>
      <fieldset class="formFieldSet">
        <legend>现货信息</legend>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="现货品种">
              <span class="white">{{ formatValue(selectedRow.deliverygoodsname) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="品类">
              <span class="white">{{ formatValue(selectedRow.wrstandardname) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="品牌">
              <span class="white">{{ formatValue(selectedRow.brandname) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="标仓系数">
              <span class="white">{{ formatValue(selectedRow.convertfactor) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="商品规格">
              <span class="white">{{ formatValue(selectedRow.spotgoodsdesc) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
      </fieldset>
      <fieldset class="formFieldSet">
        <legend>价格信息</legend>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="定价类型">
              <span class="white">{{ getPriceTypeName(formatValue(selectedRow.pricetype)) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数量">
              <span class="white">{{ formatValue(selectedRow.qty) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="点价合约">
              <span class="white">{{ formatValue(selectedRow.goodscode) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="升贴水">
              <span class="white">{{ formatValue(selectedRow.pricemove) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="点价期">
              <span class="white">{{ formatValue(formatTime(selectedRow.startdate, "d") + '--' + formatTime(selectedRow.enddate, "d")) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="交收期">
              <span class="white">{{ formatValue(formatTime(selectedRow.deliverystartdate, "d") + '--' + formatTime(selectedRow.deliveryenddate, "d"))}}</span>
            </a-form-item>
          </a-col>
        </a-row>
      </fieldset>
      <fieldset class="formFieldSet">
        <legend>其他信息</legend>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="保证金">
              <span class="white">{{ formatValue(selectedRow.contractmargin) }}</span>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="业务员"
                         name="SaleUserID">
              <a-select class="inlineFormSelect"
                        v-model:value="formState.SaleUserID"
                        style="width: 200px"
                        placeholder="请选择业务员">
                <a-select-option v-for="item in businesserList"
                                 :key="item.loginid"
                                 :value="item.loginid">
                  {{item.accountname}}-{{item.logincode}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="跟单员"
                         name="MerUserID">
              <a-select class="inlineFormSelect"
                        v-model:value="formState.MerUserID"
                        style="width: 200px"
                        placeholder="请选择跟单员">
                <a-select-option v-for="item in merchandiserList"
                                 :key="item.loginid"
                                 :value="item.loginid">
                  {{item.accountname}}-{{item.logincode}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="交易用户"
                         name="TradeUserID">
              <a-select class="inlineFormSelect"
                        style="width: 200px"
                        v-model:value="formState.TradeUserID"
                        placeholder="请选择期货账户">
                <a-select-option v-for="item in traderList"
                                 :key="item.roleid"
                                 :value="item.roleid">
                  {{item.rolename}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="备注">
              <span class="white">{{ formatValue(selectedRow.remark) }}</span>
            </a-form-item>
          </a-col>
        </a-row>
      </fieldset>
      <fieldset class="formFieldSet">
        <legend>审核信息</legend>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="审核意见"
                         name="Remark">
              <a-input class="dialogInput"
                       v-model:value="formState.Remark"
                       style="width: 620px"
                       placeholder="请输入审核意见" />
            </a-form-item>
          </a-col>
        </a-row>
      </fieldset>
    </a-form>
    <a-modal :visible="previewVisible"
             :footer="null"
             @cancel="cancelImg">
      <img alt="预览附件"
           style="width: 100%"
           :src="previewImage" />
    </a-modal>
  </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, toRaw, watchEffect } from 'vue';
import { closeModal } from '@/common/setup/modal/index';
import { getBizTypeName, getContractStatusName, getContractTypeName, getPriceTypeName } from '@/common/constants/enumsName';
import { Ermcp3ContractRsp } from '@/services/go/ermcp/spot-contract/interface';
import { formatTime, formatValue } from '@/common/methods';
import { orderContractControl, orderContractOperateControl } from '@/views/information/spot-contract/components/setup';
import { Modal } from 'ant-design-vue';
import { handleForm } from './setup';
import { GldErmcpSpotContractOperateReq, SpotContractOperateReq } from '@/services/proto/spotcontract/interface';
import { validateAction } from '@/common/setup/form';
import { FormState } from './interface';
import { ErmcpLoginUser } from '@/services/go/ermcp/account/interface';
import { handlerManagerList } from '@/common/setup/user';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { hedgePlanReq } from '@/services/proto/hedgeplan';
import { orderContract, spotContractStatus } from '@/services/proto/spotcontract';
import { getUserId } from '@/services/bus/account';
import { handlePreviewImg } from '@/common/setup/upload';

export default defineComponent({
    name: 'spot-contract-detail',
    components: {},
    props: {
        selectedRow: {
            type: Object as PropType<Ermcp3ContractRsp>,
            default: {},
        },
    },
    setup(props, context) {
        const loading = ref<boolean>(false);
        const { visible, cancel } = closeModal('spot_contract_btn_check');
        // 获取 业务账户
        const { queryTable: queryBusinessManager, getBusinesserOrMerchandiser } = handlerManagerList(loading, 1);
        const { tableList: traderList, queryTable: queryTradeManager } = handlerManagerList(loading, 2, true);
        // 业务员
        const businesserList = ref<ErmcpLoginUser[]>([]);
        // 跟单员
        const merchandiserList = ref<ErmcpLoginUser[]>([]);
        // 表单
        const { rules, formState, formRef } = handleForm();
        // 预览附件
        const { previewVisible, previewImage, cancelImg, previewImg, getImgName } = handlePreviewImg();
        function submit() {
            validateAction<FormState>(formRef, formState).then((param) => {
                Modal.confirm({
                    title: '是否确认审核通过',
                    okText: '审核通过',
                    cancelText: '取消',
                    onOk() {
                        // 为什么不删 因为以后可能又改回来
                        // const reqParam: SpotContractOperateReq = {
                        //     SpotContractID: props.selectedRow.spotcontractid,
                        //     OperateType: 3,
                        //     Remark: param.Remark,
                        //     Info: {
                        //         BizType: props.selectedRow.biztype,
                        //         MerUserID: param.MerUserID as number, //    跟单员ID
                        //         SaleUserID: param.SaleUserID as number, //   业务员ID
                        //         TradeUserID: param.TradeUserID as number, //  交易员ID
                        //         Remark: param.Remark,
                        //         UserID: getUserId(),
                        //     },
                        // };

                        const reqParam: GldErmcpSpotContractOperateReq = {
                            SpotContractID: props.selectedRow.spotcontractid,
                            OperateType: 4,
                            Remark: param.Remark,
                            Info: {
                                BizType: props.selectedRow.biztype,
                                MerUserID: param.MerUserID as number, //    跟单员ID
                                SaleUserID: param.SaleUserID as number, //   业务员ID
                                TradeUserID: param.TradeUserID as number, //  交易员ID
                                Remark: param.Remark,
                                UserID: getUserId(),
                                CurrencyID: props.selectedRow.currencyid, // 币种id
                            },
                        };
                        // Fixme 6/4 代码修改
                        // 审核通过 SpotContractOperateReq operateType 操作类型-1:保存草稿(作废)2:提交申请(作废)3:审核通过4:审核拒绝(作废)5:撤回(作废)6:正常完结7:异常终止
                        // Fixme 6/9 代码修改
                        // 审核通过 GldErmcpSpotContractOperateReq 操作类型-1:保存草稿2:提交申请3:审核通过4:审核拒绝5:撤回6:正常完结7:异常终止
                        requestResultLoadingAndInfo(orderContract, reqParam, loading, ['审核通过成功', '审核失败:']).then(() => {
                            context.emit('refresh');
                            cancel();
                        });
                    },
                    onCancel() {},
                });
            });
        }
        function refuse() {
            Modal.confirm({
                title: '是否确认审核拒绝',
                okText: '审核拒绝',
                cancelText: '取消',
                onOk() {
                    const param = toRaw(formState);
                    const reqParam: GldErmcpSpotContractOperateReq = {
                        SpotContractID: props.selectedRow.spotcontractid,
                        OperateType: 5,
                        Remark: param.Remark,
                    };
                    // Fixme 6/4 代码修改
                    // 审核拒绝  操作类型-1:保存草稿2:提交申请3:删除4:审核通过5:审核拒绝6:撤销
                    requestResultLoadingAndInfo(orderContract, reqParam, loading, ['审核拒绝成功', '审核失败:']).then(() => {
                        context.emit('refresh');
                        cancel();
                    });
                },
                onCancel() {},
            });
        }
        watchEffect(() => {
            if (visible.value) {
                queryBusinessManager().then(() => {
                    businesserList.value = getBusinesserOrMerchandiser('22');
                    merchandiserList.value = getBusinesserOrMerchandiser('23');
                });
                queryTradeManager();
                const { saleuserid, tradeuserid, meruserid, remark } = props.selectedRow;

                saleuserid && (formState.SaleUserID = saleuserid);
                tradeuserid && (formState.TradeUserID = tradeuserid);
                meruserid && (formState.MerUserID = meruserid);
                formState.Remark = remark;
            }
        });
        return {
            visible,
            cancel,
            submit,
            maskClosableFlag: false,
            getContractTypeName,
            getBizTypeName,
            formatValue,
            getContractStatusName,
            formatTime,
            getPriceTypeName,
            refuse,
            traderList,
            merchandiserList,
            businesserList,
            loading,
            formState,
            rules,
            formRef,
            previewVisible,
            previewImage,
            cancelImg,
            previewImg,
            getImgName,
        };
    },
});
</script>

<style lang="less">
.add-custom custom-detail {
    .upload {
        .look {
            margin-left: 0;
        }
    }
}
</style>;
