<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        :destroy-on-close="true"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem
                        :label="t('auth.admin.username')"
                        v-model="baTable.form.items!.username"
                        type="string"
                        prop="username"
                        :placeholder="t('auth.admin.Administrator login')"
                    />
                    <FormItem
                        :label="t('auth.admin.nickname')"
                        v-model="baTable.form.items!.nickname"
                        type="string"
                        prop="nickname"
                        :placeholder="t('Please input field', { field: t('auth.admin.nickname') })"
                    />
                    <FormItem
                        :label="t('auth.admin.group')"
                        v-model="singleGroupValue"
                        prop="group_arr"
                        type="remoteSelect"
                        :key="'group-' + baTable.form.items!.id"
                        :input-attr="{
                            multiple: false,
                            disabled: adminInfo.id == baTable.form.items!.id,
                            params: { isTree: true, absoluteAuth: adminInfo.id == baTable.form.items!.id ? 0 : 1 },
                            field: 'name',
                            remoteUrl: '/admin/auth.Group/index',
                            placeholder: t('Click select'),
                        }"
                    />
                    <FormItem
                        :label="t('auth.admin.commission_rate')"
                        v-model="baTable.form.items!.commission_rate"
                        type="number"
                        prop="commission_rate"
                        :input-attr="{ step: 0.01, precision: 2, min: 0, max: 100, disabled: shouldDisableCommissionRate() }"
                        :placeholder="t('Please input field', { field: t('auth.admin.commission_rate') })"
                    />
                    <el-alert class="commission-rate-alert" :title="t('auth.admin.commission_rate_desc_title')" type="info" :closable="false" show-icon>
                        <template #default>
                            <ul class="commission-rate-desc-list">
                                <li>{{ t('auth.admin.commission_rate_desc_1') }}</li>
                                <li>{{ t('auth.admin.commission_rate_desc_2') }}</li>
                                <li>{{ t('auth.admin.commission_rate_desc_3') }}</li>
                            </ul>
                        </template>
                    </el-alert>
                    <FormItem
                        v-if="baTable.form.operate == 'Edit'"
                        :label="t('auth.admin.invite_code')"
                        v-model="baTable.form.items!.invite_code"
                        type="string"
                        :input-attr="{ readonly: true, disabled: true }"
                    />
                    <FormItem :label="t('auth.admin.avatar')" type="image" v-model="baTable.form.items!.avatar" />
                    <FormItem
                        :label="t('auth.admin.email')"
                        prop="email"
                        v-model="baTable.form.items!.email"
                        type="string"
                        :placeholder="t('Please input field', { field: t('auth.admin.email') })"
                    />
                    <FormItem
                        :label="t('auth.admin.mobile')"
                        prop="mobile"
                        v-model="baTable.form.items!.mobile"
                        type="string"
                        :placeholder="t('Please input field', { field: t('auth.admin.mobile') })"
                    />
                    <FormItem
                        :label="t('auth.admin.Password')"
                        prop="password"
                        v-model="baTable.form.items!.password"
                        type="password"
                        :input-attr="{ autocomplete: 'new-password' }"
                        :placeholder="
                            baTable.form.operate == 'Add'
                                ? t('Please input field', { field: t('auth.admin.Password') })
                                : t('auth.admin.Please leave blank if not modified')
                        "
                    />
                    <el-form-item prop="motto" :label="t('auth.admin.Personal signature')">
                        <el-input
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            v-model="baTable.form.items!.motto"
                            type="textarea"
                            :placeholder="t('Please input field', { field: t('auth.admin.Personal signature') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('State')"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :input-attr="{
                            border: true,
                            content: { disable: t('Disable'), enable: t('Enable') },
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { computed, reactive, inject, watch, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { regularPassword, buildValidatorData } from '/@/utils/validate'
import type { FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const adminInfo = useAdminInfo()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

/** 解析管理员分红比例：与后端 isValidCommissionRate 一致地拒绝非法字符，避免 Number('30,00') 等为 NaN 导致误报 */
function parseAdminCommissionRateInput(raw: unknown): { kind: 'empty' } | { kind: 'invalid' } | { kind: 'ok'; value: number } {
    if (raw === null || raw === undefined || raw === '') {
        return { kind: 'empty' }
    }
    if (typeof raw === 'number') {
        if (!Number.isFinite(raw)) {
            return { kind: 'invalid' }
        }
        return { kind: 'ok', value: raw }
    }
    const s = String(raw).trim()
    if (s === '') {
        return { kind: 'empty' }
    }
    const normalized = s.replace(',', '.')
    const n = parseFloat(normalized)
    if (!Number.isFinite(n)) {
        return { kind: 'invalid' }
    }
    return { kind: 'ok', value: n }
}

const shouldDisableCommissionRate = () => {
    return adminInfo.id == baTable.form.items!.id
}
const singleGroupValue = computed({
    get: () => {
        const group = baTable.form.items?.group_arr
        if (Array.isArray(group)) {
            return group.length > 0 ? group[0] : ''
        }
        return group ?? ''
    },
    set: (value) => {
        if (!baTable.form.items) {
            return
        }
        baTable.form.items.group_arr = value === '' || value === null || value === undefined ? [] : [value]
    },
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    username: [buildValidatorData({ name: 'required', title: t('auth.admin.username') }), buildValidatorData({ name: 'account' })],
    nickname: [buildValidatorData({ name: 'required', title: t('auth.admin.nickname') })],
    group_arr: [
        {
            required: true,
            validator: (_rule: any, val: unknown, callback: Function) => {
                if (Array.isArray(val)) {
                    if (val.length !== 1) {
                        return callback(new Error(t('auth.admin.Please select exactly one group')))
                    }
                    return callback()
                }
                if (val === null || val === undefined || val === '') {
                    return callback(new Error(t('Please select field', { field: t('auth.admin.group') })))
                }
                return callback()
            },
            trigger: 'change',
        },
    ],
    email: [buildValidatorData({ name: 'email', message: t('Please enter the correct field', { field: t('auth.admin.email') }) })],
    mobile: [buildValidatorData({ name: 'mobile', message: t('Please enter the correct field', { field: t('auth.admin.mobile') }) })],
    password: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (baTable.form.operate == 'Add') {
                    if (!val) {
                        return callback(new Error(t('Please input field', { field: t('auth.admin.Password') })))
                    }
                } else {
                    if (!val) {
                        return callback()
                    }
                }
                if (!regularPassword(val)) {
                    return callback(new Error(t('validate.Please enter the correct password')))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    commission_rate: [
        {
            validator: (_rule: unknown, val: unknown, callback: (e?: Error) => void) => {
                const parsed = parseAdminCommissionRateInput(val)
                if (parsed.kind === 'empty') {
                    return callback()
                }
                if (parsed.kind === 'invalid') {
                    return callback(new Error(t('Please enter the correct field', { field: t('auth.admin.commission_rate') })))
                }
                const n = parsed.value
                const rounded = Math.round(n * 100) / 100
                if (rounded < -0.000001 || rounded > 100.000001) {
                    return callback(new Error(t('Please enter the correct field', { field: t('auth.admin.commission_rate') })))
                }
                return callback()
            },
            trigger: ['blur', 'change'],
        },
    ],
})

watch(
    () => baTable.form.operate,
    (newVal) => {
        rules.password![0].required = newVal == 'Add'
    }
)
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.commission-rate-alert {
    margin-bottom: 12px;
}
.commission-rate-desc-list {
    margin: 6px 0 0;
    padding-left: 18px;
    line-height: 1.6;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
