<template>
  <!-- 重置账户密码 -->
  <a-modal class="add-custom"
           title="重置登录密码"
           v-model:visible="visible"
           @cancel="cancel"
           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="logincode">
            <a-input class="dialogInput"
                     style="width: 200px"
                     readonly
                     v-model:value="formState.logincode"
                     placeholder="请输入登录账号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="新密码"
                       name="password">
            <a-input-password class="dialogInput"
                              style="width: 200px"
                              v-model:value="formState.password"
                              placeholder="请输入8到20个字符的新密码" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="确认新密码"
                       name="comfirePassword">
            <a-input-password class="dialogInput"
                              style="width: 200px"
                              v-model:value="formState.comfirePassword"
                              placeholder="请再次确认新密码" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import { defineComponent, ref, PropType } from 'vue';
import { ErmcpLoginUser } from '@/services/go/ermcp/account/interface';
import { ResetPasswordFormState } from '../interface';
import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { loginAccountOperate } from '@/services/proto/accountinfo';
import { LoginaccountOperateReq } from '@/services/proto/accountinfo/interface';
import { validateAction } from '@/common/setup/form';
import { handlePasswordForm } from '../setup';
import { _closeModal } from '@/common/setup/modal/modal';

export default defineComponent({
    name: 'account_info_business_btn_reset',
    emits: ['cancel', 'update'],
    props: {
        selectedRow: {
            default: {},
            type: Object as PropType<ErmcpLoginUser>,
        },
    },
    setup(props, context) {
        // 控制关闭弹窗
        const { visible, cancel } = _closeModal(context);
        const { rules, formState, formRef } = handlePasswordForm();
        const loading = ref<boolean>(false);

        formState.logincode = props.selectedRow.logincode;

        function submit() {
            validateAction<ResetPasswordFormState>(formRef, formState).then((res) => {
                let reqParam: LoginaccountOperateReq = {
                    userid: props.selectedRow.userid,
                    loginid: props.selectedRow.loginid,
                    operatetype: 9, // 5: 锁定 6：解锁
                    logintaaccounts: [],
                    logincode: props.selectedRow.logincode,
                    password: res.password,
                };
                requestResultLoadingAndInfo(loginAccountOperate, reqParam, loading, ['重置登录账户密码成功', '重置登录账户密码失败:']).then(() => {
                    cancel(true);
                });
            });
        }

        return {
            formState,
            rules,
            formRef,
            visible,
            cancel,
            submit,
            loading,
        };
    },
});
</script>

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