<template>
  <!-- 新增协议-->
  <a-modal class="add-custom"
           title="新增协议"
           v-model:visible="visible"
           @cancel="colseAction"
           centered
           :maskClosable="false"
           width="890px">
    <template #footer>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="submit()">新增
      </a-button>
    </template>
    <a-form class="inlineForm"
            ref="formRef"
            :model="formState"
            :rules="rules">
      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item label="协议编号"
                       name="agreementno">
            <a-input class="dialogInput"
                     v-model:value="formState.agreementno"
                     style="width: 200px"
                     placeholder="请输入协议编号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="协议名称"
                       name="agreementname">
            <a-input class="dialogInput"
                     v-model:value="formState.agreementname"
                     style="width: 200px"
                     placeholder="请输入协议名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="协议类型"
                       name="agreementtype">
            <a-select class="typeSelect"
                      style="width: 200px"
                      v-model:value="formState.agreementtype"
                      placeholder="请选择协议类型">
              <a-select-option v-for="item in agreementTypeList"
                               :value="item.value"
                               :key="item.value">
                {{item.lable}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="签署频率">
            <a-input class="dialogInput"
                     :value="getRate()"
                     readonly
                     style="width: 200px"
                     placeholder="" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="协议内容"
                       name="agreementcontent">
            <a-textarea class="dialogInput"
                        v-model:value="formState.agreementcontent"
                        style="width: 635px"
                        placeholder="请输入协议内容"
                        :autosize="{ minRows: 8, maxRows: 24 }" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import { closeModal } from '@/common/setup/modal/index';
import { defineComponent, ref } from 'vue';
import { handleForm, initFormState } from './setup';
import { FormState } from './interface';
import { validateAction } from '@/common/setup/form';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { agreementConfigOperate } from '@/services/proto/manager';
import { QHJAgreementConfigOperateReq } from '@/services/proto/manager/interface';
import { getUserId } from '@/services/bus/account';
import { handleArgreementType } from '@/views/platinum/platinum_agreement/setup';
import { _closeModal } from '@/common/setup/modal/modal';

export default defineComponent({
    emits: ['cancel', 'update'],
    name: 'add-custom',
    components: {},
    setup(props, context) {
        // 控制弹窗
        const { visible, cancel } = _closeModal(context);
        // 表单
        const { formRef, formState, rules } = handleForm();
        // 协议类型
        const { getAgreementTypeList, getRate } = handleArgreementType(formState);

        const loading = ref<boolean>(false);
        function colseAction() {
            Object.assign(formState, initFormState());
            cancel(true);
        }
        function submit(OperateType: 1 | 2) {
            validateAction<FormState>(formRef, formState).then((param) => {
                const reqParam: QHJAgreementConfigOperateReq = {
                    operatetype: 1, // uint32 操作类型-1:新增 2:修改
                    agreementno: param.agreementno, // string 协议编号
                    agreementname: param.agreementname, // string 协议名称
                    agreementtype: Number(param.agreementtype), // uint32 协议类型 - 1:注册 2:登录 3:交易
                    agreementcontent: param.agreementcontent, // string 协议内容
                    operatesrc: 1, // uint32 操作来源 - 1:管理端 2:终端
                    operateid: getUserId(), // uint64 操作人
                };
                requestResultLoadingAndInfo(agreementConfigOperate, reqParam, loading, ['新增协议成功', '新增协议失败:']).then(() => {
                    colseAction();
                });
            });
        }

        return {
            agreementTypeList: getAgreementTypeList(),
            getRate,
            formState,
            rules,
            formRef,
            visible,
            colseAction,
            submit,
            loading,
        };
    },
});
</script>

<style lang="less">
.add-custom {
}
</style
>;
