<template>
    <div class="main-container">
        <div class="detail-head">
            <div class="left" @click="back()">
                <span class="iconfont iconxiangzuojiantou !text-xs"></span>
                <span class="ml-[1px]">{{t('returnToPreviousPage')}}</span>
            </div>
            <span class="adorn">|</span>
            <span class="right">{{ pageName }}</span>
        </div>
        <el-card class="box-card !border-none" shadow="never">
            <el-form :model="formData" label-width="90px" ref="formRef" :rules="formRules" class="page-form">
                <el-form-item :label="t('memberNo')" >
                    <el-input v-model="formData.member_no" clearable :placeholder="t('memberNoPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('pid')" >
                    <el-select class="input-width" v-model="formData.pid" clearable :placeholder="t('pidPlaceholder')">
                       <el-option label="请选择" value=""></el-option>
                        <el-option
                            v-for="(item, index) in pidList"
                            :key="index"
                            :label="item['mobile']"
                            :value="item['member_id']"
                        />
                    </el-select>
                </el-form-item>
                
                <el-form-item :label="t('username')" prop="username">
                    <el-input v-model="formData.username" clearable :placeholder="t('usernamePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('mobile')" prop="mobile">
                    <el-input v-model="formData.mobile" clearable :placeholder="t('mobilePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('password')" prop="password">
                    <el-input v-model="formData.password" clearable :placeholder="t('passwordPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('nickname')" prop="nickname">
                    <el-input v-model="formData.nickname" clearable :placeholder="t('nicknamePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('headimg')">
                    <upload-image v-model="formData.headimg" />
                </el-form-item>
                
                <el-form-item :label="t('memberLevel')" >
                    <el-input v-model="formData.member_level" clearable :placeholder="t('memberLevelPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('memberLabel')" >
                    <el-input v-model="formData.member_label" clearable :placeholder="t('memberLabelPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('wxOpenid')" >
                    <el-input v-model="formData.wx_openid" clearable :placeholder="t('wxOpenidPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('weappOpenid')" >
                    <el-input v-model="formData.weapp_openid" clearable :placeholder="t('weappOpenidPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('wxUnionid')" >
                    <el-input v-model="formData.wx_unionid" clearable :placeholder="t('wxUnionidPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('aliOpenid')" >
                    <el-input v-model="formData.ali_openid" clearable :placeholder="t('aliOpenidPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('douyinOpenid')" >
                    <el-input v-model="formData.douyin_openid" clearable :placeholder="t('douyinOpenidPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('registerChannel')" >
                    <el-input v-model="formData.register_channel" clearable :placeholder="t('registerChannelPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('registerType')" >
                    <el-input v-model="formData.register_type" clearable :placeholder="t('registerTypePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('loginIp')" >
                    <el-input v-model="formData.login_ip" clearable :placeholder="t('loginIpPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('loginType')" >
                    <el-input v-model="formData.login_type" clearable :placeholder="t('loginTypePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('loginChannel')" >
                    <el-input v-model="formData.login_channel" clearable :placeholder="t('loginChannelPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('loginCount')" >
                    <el-input v-model="formData.login_count" clearable :placeholder="t('loginCountPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('loginTime')" >
                    <el-input v-model="formData.login_time" clearable :placeholder="t('loginTimePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('lastVisitTime')" >
                    <el-input v-model="formData.last_visit_time" clearable :placeholder="t('lastVisitTimePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('lastConsumTime')" >
                    <el-input v-model="formData.last_consum_time" clearable :placeholder="t('lastConsumTimePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('sex')" prop="sex">
                    <el-select class="input-width"  v-model="formData.sex" clearable :placeholder="t('sexPlaceholder')">
                    <el-option label="请选择" value=""></el-option>
                        <el-option
                            v-for="(item, index) in sexList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                
                <el-form-item :label="t('status')" prop="status">
                    <el-select class="input-width"  v-model="formData.status" clearable :placeholder="t('statusPlaceholder')">
                    <el-option label="请选择" value=""></el-option>
                        <el-option
                            v-for="(item, index) in statusList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                
                <el-form-item :label="t('birthday')"  class="input-width">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.birthday"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        :placeholder="t('birthdayPlaceholder')">
                    </el-date-picker>
                </el-form-item>
                <el-form-item :label="t('point')" >
                    <el-input v-model="formData.point" clearable :placeholder="t('pointPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('pointGet')" >
                    <el-input v-model="formData.point_get" clearable :placeholder="t('pointGetPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('balance')" >
                    <el-input v-model="formData.balance" clearable :placeholder="t('balancePlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('balanceGet')" >
                    <el-input v-model="formData.balance_get" clearable :placeholder="t('balanceGetPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('money')" >
                    <el-input v-model="formData.money" clearable :placeholder="t('moneyPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('moneyGet')" >
                    <el-input v-model="formData.money_get" clearable :placeholder="t('moneyGetPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('moneyCashOuting')" >
                    <el-input v-model="formData.money_cash_outing" clearable :placeholder="t('moneyCashOutingPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('growth')" >
                    <el-input v-model="formData.growth" clearable :placeholder="t('growthPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('growthGet')" >
                    <el-input v-model="formData.growth_get" clearable :placeholder="t('growthGetPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('commission')" >
                    <el-input v-model="formData.commission" clearable :placeholder="t('commissionPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('commissionGet')" >
                    <el-input v-model="formData.commission_get" clearable :placeholder="t('commissionGetPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('commissionCashOuting')" >
                    <el-input v-model="formData.commission_cash_outing" clearable :placeholder="t('commissionCashOutingPlaceholder')" class="input-width" />
                </el-form-item>
                
                <el-form-item :label="t('isMember')" prop="is_member">
                    <el-select class="input-width"  v-model="formData.is_member" clearable :placeholder="t('isMemberPlaceholder')">
                    <el-option label="请选择" value=""></el-option>
                        <el-option
                            v-for="(item, index) in is_memberList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                
                <el-form-item :label="t('memberTime')"  class="input-width">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.member_time"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        :placeholder="t('memberTimePlaceholder')">
                    </el-date-picker>
                </el-form-item>
                <el-form-item :label="t('address')" >
                    <el-input v-model="formData.address" clearable :placeholder="t('addressPlaceholder')" class="input-width" />
                </el-form-item>
                

            </el-form>
        </el-card>
         <div class="fixed-footer-wrap">
            <div class="fixed-footer">
                <el-button type="primary" @click="onSave(formRef)">{{ t('save') }}</el-button>
                <el-button @click="back()">{{ t('cancel') }}</el-button>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import type { FormInstance } from 'element-plus'
import { getUsersInfo,addUsers,editUsers, getWithUsersList } from '@/addon/zhjw/api/users';
import { useRoute } from 'vue-router'

const route = useRoute()
const id:number = parseInt(route.query.id);
const loading = ref(false)
const pageName = route.meta.title



/**
 * 表单数据
 */
const initialFormData = {
    member_id: 0,
    member_no: '',
    pid: '',
    username: '',
    mobile: '',
    password: '',
    nickname: '',
    headimg: '',
    member_level: 0,
    member_label: '',
    wx_openid: '',
    weapp_openid: '',
    wx_unionid: '',
    ali_openid: '',
    douyin_openid: '',
    register_channel: '',
    register_type: '',
    login_ip: '',
    login_type: '',
    login_channel: '',
    login_count: 0,
    login_time: 0,
    last_visit_time: 0,
    last_consum_time: 0,
    sex: 0,
    status: 0,
    birthday: '',
    point: 0,
    point_get: 0,
    balance: '',
    balance_get: '',
    money: '',
    money_get: '',
    money_cash_outing: '',
    growth: 0,
    growth_get: 0,
    commission: '',
    commission_get: '',
    commission_cash_outing: '',
    is_member: 0,
    member_time: 0,
    address: '',

}
const formData: Record<string, any> = reactive({ ...initialFormData })

const setFormData = async (id:number = 0) => {
    Object.assign(formData, initialFormData)
    const data = await (await getUsersInfo(id)).data
    Object.keys(formData).forEach((key: string) => {
        if (data[key] != undefined) formData[key] = data[key]
    })
}
if(id) setFormData(id);

const formRef = ref<FormInstance>()
// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    let sexList = ref([])
    const sexDictList = async () => {
    sexList.value = await (await useDictionary('users_sex')).data.dictionary
    }
    sexDictList();
    watch(() => sexList.value, () => { formData.sex = sexList.value[0].value })
    let statusList = ref([])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    statusDictList();
    watch(() => statusList.value, () => { formData.status = statusList.value[0].value })
    let is_memberList = ref([])
    const is_memberDictList = async () => {
    is_memberList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    is_memberDictList();
    watch(() => is_memberList.value, () => { formData.is_member = is_memberList.value[0].value })
    let is_delList = ref([])
    const is_delDictList = async () => {
    is_delList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    is_delDictList();
    watch(() => is_delList.value, () => { formData.is_del = is_delList.value[0].value })


    
    const pidList = ref([] as any[])
    const setPidList = async () => {
    pidList.value = await (await getWithUsersList({})).data
    }
    setPidList()
// 表单验证规则
const formRules = computed(() => {
    return {
    member_no: [
        { required: true, message: t('memberNoPlaceholder'), trigger: 'blur' },
        
    ]
,
    pid: [
        { required: true, message: t('pidPlaceholder'), trigger: 'blur' },
        
    ]
,
    username: [
        { required: true, message: t('usernamePlaceholder'), trigger: 'blur' },
        
    ]
,
    mobile: [
        { required: true, message: t('mobilePlaceholder'), trigger: 'blur' },
        
    ]
,
    password: [
        { required: true, message: t('passwordPlaceholder'), trigger: 'blur' },
        
    ]
,
    nickname: [
        { required: true, message: t('nicknamePlaceholder'), trigger: 'blur' },
        
    ]
,
    headimg: [
        { required: true, message: t('headimgPlaceholder'), trigger: 'blur' },
        
    ]
,
    member_level: [
        { required: true, message: t('memberLevelPlaceholder'), trigger: 'blur' },
        
    ]
,
    member_label: [
        { required: true, message: t('memberLabelPlaceholder'), trigger: 'blur' },
        
    ]
,
    wx_openid: [
        { required: true, message: t('wxOpenidPlaceholder'), trigger: 'blur' },
        
    ]
,
    weapp_openid: [
        { required: true, message: t('weappOpenidPlaceholder'), trigger: 'blur' },
        
    ]
,
    wx_unionid: [
        { required: true, message: t('wxUnionidPlaceholder'), trigger: 'blur' },
        
    ]
,
    ali_openid: [
        { required: true, message: t('aliOpenidPlaceholder'), trigger: 'blur' },
        
    ]
,
    douyin_openid: [
        { required: true, message: t('douyinOpenidPlaceholder'), trigger: 'blur' },
        
    ]
,
    register_channel: [
        { required: true, message: t('registerChannelPlaceholder'), trigger: 'blur' },
        
    ]
,
    register_type: [
        { required: true, message: t('registerTypePlaceholder'), trigger: 'blur' },
        
    ]
,
    login_ip: [
        { required: true, message: t('loginIpPlaceholder'), trigger: 'blur' },
        
    ]
,
    login_type: [
        { required: true, message: t('loginTypePlaceholder'), trigger: 'blur' },
        
    ]
,
    login_channel: [
        { required: true, message: t('loginChannelPlaceholder'), trigger: 'blur' },
        
    ]
,
    login_count: [
        { required: true, message: t('loginCountPlaceholder'), trigger: 'blur' },
        
    ]
,
    login_time: [
        { required: true, message: t('loginTimePlaceholder'), trigger: 'blur' },
        
    ]
,
    last_visit_time: [
        { required: true, message: t('lastVisitTimePlaceholder'), trigger: 'blur' },
        
    ]
,
    last_consum_time: [
        { required: true, message: t('lastConsumTimePlaceholder'), trigger: 'blur' },
        
    ]
,
    sex: [
        { required: true, message: t('sexPlaceholder'), trigger: 'blur' },
        
    ]
,
    status: [
        { required: true, message: t('statusPlaceholder'), trigger: 'blur' },
        
    ]
,
    birthday: [
        { required: true, message: t('birthdayPlaceholder'), trigger: 'blur' },
        
    ]
,
    point: [
        { required: true, message: t('pointPlaceholder'), trigger: 'blur' },
        
    ]
,
    point_get: [
        { required: true, message: t('pointGetPlaceholder'), trigger: 'blur' },
        
    ]
,
    balance: [
        { required: true, message: t('balancePlaceholder'), trigger: 'blur' },
        
    ]
,
    balance_get: [
        { required: true, message: t('balanceGetPlaceholder'), trigger: 'blur' },
        
    ]
,
    money: [
        { required: true, message: t('moneyPlaceholder'), trigger: 'blur' },
        
    ]
,
    money_get: [
        { required: true, message: t('moneyGetPlaceholder'), trigger: 'blur' },
        
    ]
,
    money_cash_outing: [
        { required: true, message: t('moneyCashOutingPlaceholder'), trigger: 'blur' },
        
    ]
,
    growth: [
        { required: true, message: t('growthPlaceholder'), trigger: 'blur' },
        
    ]
,
    growth_get: [
        { required: true, message: t('growthGetPlaceholder'), trigger: 'blur' },
        
    ]
,
    commission: [
        { required: true, message: t('commissionPlaceholder'), trigger: 'blur' },
        
    ]
,
    commission_get: [
        { required: true, message: t('commissionGetPlaceholder'), trigger: 'blur' },
        
    ]
,
    commission_cash_outing: [
        { required: true, message: t('commissionCashOutingPlaceholder'), trigger: 'blur' },
        
    ]
,
    is_member: [
        { required: true, message: t('isMemberPlaceholder'), trigger: 'blur' },
        
    ]
,
    member_time: [
        { required: true, message: t('memberTimePlaceholder'), trigger: 'blur' },
        
    ]
,
    address: [
        { required: true, message: t('addressPlaceholder'), trigger: 'blur' },
        
    ]
,
    }
})

const onSave = async (formEl: FormInstance | undefined) => {
    if (loading.value || !formEl) return
    await formEl.validate(async (valid) => {
       if (valid) {
           loading.value = true
           let data = formData

           const save = id ? editUsers : addUsers
           save(data).then(res => {
               loading.value = false
               history.back()
           }).catch(err => {
               loading.value = false
           })

       }
    })
}

// 验证手机号格式
const mobileVerify = (rule: any, value: any, callback: any) => {
    if (value && !/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error(t('generateMobile')))
    } else {
        callback()
    }
}

// 验证身份证号
const idCardVerify = (rule: any, value: any, callback: any) => {
    if (value && !/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value)) {
        callback(new Error(t('generateIdCard')))
    } else {
        callback()
    }
}

// 验证邮箱号
const emailVerify = (rule: any, value: any, callback: any) => {
    if (value && !/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(value)) {
        callback(new Error(t('generateEmail')))
    } else {
        callback()
    }
}


// 验证请输入整数
const numberVerify = (rule: any, value: any, callback: any) => {
    if (!Number.isInteger(value)) {
        callback(new Error(t('generateNumber')))
    } else {
        callback()
    }
}
const back = () => {
    history.back()
}
</script>

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