<!-- 会员中心-我的地址-编辑 -->
<template>
    <app-drawer :title="selectedRow?.autoid ? '修改地址' : '新增地址'" :width="880" v-model:show="show" :loading="loading"
        :refresh="refresh">
        <el-form ref="formRef" class="el-form--horizontal" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item label="收货人" prop="ReceiverName">
                <el-input placeholder="请输入" v-model="formData.ReceiverName" />
            </el-form-item>
            <el-form-item label="证件类型" prop="CardTypeID">
                <el-select v-model="formData.CardTypeID">
                    <el-option :label="item.label" :value="item.value" v-for="(item, index) in certificateTypeList"
                        :key="index" />
                </el-select>
            </el-form-item>
            <el-form-item label="联系电话" prop="PhoneNum">
                <el-input placeholder="请输入" v-model="formData.PhoneNum" />
            </el-form-item>
            <el-form-item label="证件号码" prop="CardNum">
                <el-input placeholder="请输入" v-model="formData.CardNum" />
            </el-form-item>
            <el-form-item class="el-form-item--row" label="收货地址" prop="area">
                <app-region class="el-form-item--col" v-model:province="formData.ProvinceID"
                    v-model:city="formData.CityID" v-model:district="formData.DistrictID" />
            </el-form-item>
            <el-form-item class="el-form-item--row" prop="Address">
                <el-input placeholder="请输入" v-model="formData.Address" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)" plain>取消</el-button>
            <el-button type="primary" @click="onSubmit">提交</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useAddressForm } from '@/business/user'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppRegion from '@pc/components/base/region/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.UserReceiveInfoRsp>,
        default: () => ({})
    }
})

const { loading, formData, certificateTypeList, addOrUpdate } = useAddressForm(props.selectedRow)
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    ReceiverName: [{ required: true, message: '请输入收货人', trigger: 'blur' }],
    CardTypeID: [{ required: true, message: '请选择证件类型' }],
    PhoneNum: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
    CardNum: [{ required: true, message: '请输入证件号码', trigger: 'blur' }],
    Address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
    area: [
        {
            required: true,
            validator: (rule, value, callback) => {
                if (formData.ProvinceID && formData.CityID && formData.DistrictID) {
                    callback()
                } else {
                    callback(new Error('请选择收货地区'))
                }
            }
        }
    ],
}

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            addOrUpdate().then(() => {
                ElMessage.success('提交成功')
                onCancel(true)
            }).catch((err) => {
                ElMessage.error('提交失败：' + err)
            })
        }
    })
}
</script>