<template>
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <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
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('mall.userAsset.id')"
                        type="number"
                        v-model="baTable.form.items!.id"
                        prop="id"
                        :input-attr="{ disabled: true }"
                    />
                    <FormItem
                        :label="t('mall.userAsset.playx_user_id')"
                        type="string"
                        v-model="baTable.form.items!.playx_user_id"
                        prop="playx_user_id"
                        :input-attr="{ disabled: true }"
                    />
                    <FormItem
                        :label="t('mall.userAsset.username')"
                        type="string"
                        v-model="baTable.form.items!.username"
                        prop="username"
                        :placeholder="t('Please input field', { field: t('mall.userAsset.username') })"
                    />
                    <FormItem
                        :label="t('mall.userAsset.phone')"
                        type="string"
                        v-model="baTable.form.items!.phone"
                        prop="phone"
                        :placeholder="t('Please input field', { field: t('mall.userAsset.phone') })"
                    />
                    <FormItem
                        :label="t('mall.userAsset.locked_points')"
                        type="number"
                        v-model="baTable.form.items!.locked_points"
                        prop="locked_points"
                        :input-attr="{ disabled: true }"
                    />
                    <el-form-item :label="t('mall.userAsset.available_points')">
                        <el-input v-model="baTable.form.items!.available_points" readonly>
                            <template #append>
                                <el-button @click="changeAvailablePoints">{{ t('mall.userAsset.adjust_available_points') }}</el-button>
                            </template>
                        </el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('mall.userAsset.today_limit')"
                        type="number"
                        v-model="baTable.form.items!.today_limit"
                        prop="today_limit"
                        :input-attr="{ step: 1, min: 0 }"
                        :placeholder="t('Please input field', { field: t('mall.userAsset.today_limit') })"
                    />
                    <FormItem
                        :label="t('mall.userAsset.today_claimed')"
                        type="number"
                        v-model="baTable.form.items!.today_claimed"
                        prop="today_claimed"
                        :input-attr="{ disabled: true }"
                    />
                </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">
                    {{ t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

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

const config = useConfig()
const router = useRouter()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass
const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    username: [buildValidatorData({ name: 'required', title: t('mall.userAsset.username') })],
    phone: [buildValidatorData({ name: 'required', title: t('mall.userAsset.phone') })],
    locked_points: [buildValidatorData({ name: 'required', title: t('mall.userAsset.locked_points') })],
    available_points: [buildValidatorData({ name: 'required', title: t('mall.userAsset.available_points') })],
    today_limit: [buildValidatorData({ name: 'required', title: t('mall.userAsset.today_limit') })],
    today_claimed: [buildValidatorData({ name: 'required', title: t('mall.userAsset.today_claimed') })],
})

const changeAvailablePoints = () => {
    const userAssetId = baTable.form.items!.id
    baTable.toggleForm()
    router.push({
        name: 'mall/pointsLog',
        query: {
            user_asset_id: userAssetId,
        },
    })
}
</script>

<style scoped lang="scss"></style>
