<template>
  <!-- 业务账户 - 新增登陆账户 -->
  <a-modal class="commonModal add-business-info"
           title="新增账户"
           v-model:visible="visible"
           @cancel="closeAction"
           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="roleids">
            <a-checkbox-group class="commonCheckboxGroup"
                              v-model:value="formState.roleids">
              <a-row>
                <a-col :span="12">
                  <a-checkbox :value="22">业务员</a-checkbox>
                </a-col>
                <a-col :span="12">
                  <a-checkbox :value="23">跟单员</a-checkbox>
                </a-col>
              </a-row>
            </a-checkbox-group>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="账户名称"
                       name="accountname">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.accountname"
                     placeholder="请输入账户名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="登录账号"
                       name="logincode">
            <a-input class="dialogInput"
                     style="width: 200px"
                     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"
                              autocomplete="new-password"
                              v-model:value="formState.password"
                              style="width: 200px"
                              placeholder="请输入登录密码" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="手机号码"
                       name="mobile">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.mobile"
                     placeholder="请输入手机号码" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

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

export default defineComponent({
    name: 'add-business-info',
    components: {},
    emits: ['cancel', 'update'],
    props: {
        selectedRow: {
            default: {},
            type: Object as PropType<ErmcpLoginUserEx>,
        },
    },
    setup(props, context) {
        // 控制关闭弹窗
        const { visible, cancel } = _closeModal(context);
        const loading = ref<boolean>(false);
        const { rules, formState, formRef, initFormData } = handleBusinessForm();
        formState.roleids = [props.selectedRow.roleid];
        function submit() {
            validateAction<BusinessFormState>(formRef, formState).then((param) => {
                const reqParam = {
                    operatetype: 1,
                };
                delete param.userid;
                Object.assign(reqParam, param);
                requestResultLoadingAndInfo(loginAccountOperate, reqParam, loading, ['新增账户成功', '新增账户失败:']).then(() => {
                    cancel(true);
                });
            });
        }
        function closeAction() {
            Object.assign(formState, initFormData());
            cancel();
        }
        return {
            formState,
            rules,
            formRef,
            visible,
            closeAction,
            submit,
            loading,
        };
    },
});
</script>

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