<template>
  <!-- 修改协议 -->
  <a-modal class="commonModal modify-custom"
           title="修改协议"
           v-if="visible"
           v-model:visible="visible"
           centered
           :maskClosable="false"
           @cancel="cancel"
           width="890px">
    <template #footer>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="submit(2)">修改</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="协议编号">
            <span class="white">{{formState.agreementno}}</span>
          </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="协议类型">
            <span class="white">{{getAgreementTypeName(formState.agreementtype)}}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="签署频率"
                       name="agreementname">
            <span class="white">{{getRate()}}</span>
          </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, PropType, ref, watchEffect } from 'vue';
import { handleForm, initFormState } from '../add/setup';
import { FormState } from '../add/interface';
import { mergeTwoObj } from '@/utils/objHandle';
import { validateAction } from '@/common/setup/form';
import { QhjAgreementConfig } from '@/services/go/ermcp/qhj/interface';
import { handleArgreementType } from '../../setup';
import {QHJAgreementConfigOperateReq} from "@/services/proto/manager/interface";
import {getUserId} from "@/services/bus/account";
import {requestResultLoadingAndInfo} from "@/common/methods/request/resultInfo";
import {agreementConfigOperate} from "@/services/proto/manager";
import {getAgreementTypeName} from "@/common/constants/enumsName";

export default defineComponent({
    name: 'modify-custom',
    components: {},
    props: {
        selectedRow: {
            default: initFormState,
            type: Object as PropType<QhjAgreementConfig>,
        },
    },
    setup(props: { selectedRow: QhjAgreementConfig }, context: { emit: (arg0: string) => void }) {
        const { visible, cancel } = closeModal('platinum_agreement_modify');

        const loading = ref<boolean>(false);

        const { formRef, formState, rules } = handleForm();
        // 协议类型
        const { getRate } = handleArgreementType(formState);
        watchEffect(() => {
            if (visible.value) {
                mergeTwoObj(formState, props.selectedRow);
            }
        });
        function submit() {
            validateAction<FormState>(formRef, formState).then((param) => {
                const reqParam: QHJAgreementConfigOperateReq = {
                    operatetype: 2, // uint32 操作类型-1:新增 2:修改
                    agreementid: props.selectedRow.agreementid,
                    agreementname: param.agreementname, // string 协议名称
                    agreementcontent: param.agreementcontent, // string 协议内容
                    operatesrc: 1, // uint32 操作来源 - 1:管理端 2:终端
                    operateid: getUserId(), // uint64 操作人
                };
                requestResultLoadingAndInfo(agreementConfigOperate, reqParam, loading, ['修改协议成功', '修改协议失败:']).then(() => {
                    cancel();
                    context.emit('refresh');
                });
            });
        }
        return {
            getRate,
            formState,
            rules,
            formRef,
            visible,
            cancel,
            submit,
            loading,
            getAgreementTypeName,
        };
    },
});
</script>

<style lang="less">
.modify-custom {
    .upload {
        display: inline-flex;
        .ant-btn.uploadBtn {
            width: 60px;
            height: 30px;
            background: @m-blue0;
            border: 0;
            padding: 0;
            text-align: center;
            font-size: 14px;
            color: @m-white0;
            .rounded-corners(3px);
            &:hover {
                background: rgba(@m-blue0, 0);
                color: rgba(@m-white0, 0.8);
            }
        }
        .look {
            color: @m-blue0;
            font-size: 14px;
            margin-left: 10px;
            cursor: pointer;
        }
    }
}
</style
>;
