<!-- 会员中心-我的发票-编辑 -->
<template>
    <app-drawer :title="selectedRow?.autoid ? '修改发票' : '新增发票'" :width="460" v-model:show="show" :loading="loading"
        :refresh="refresh">
        <el-form ref="formRef" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item label="发票类型" prop="ReceiptType">
                <el-radio-group v-model="formData.ReceiptType">
                    <el-radio :label="1">个人</el-radio>
                    <el-radio :label="2">企业</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="发票抬头" prop="UserName">
                <el-input placeholder="请输入" v-model="formData.UserName" />
            </el-form-item>
            <template v-if="formData.ReceiptType === 2">
                <el-form-item label="税号" prop="TaxpayerID">
                    <el-input placeholder="请输入" v-model="formData.TaxpayerID" />
                </el-form-item>
                <el-form-item label="开户银行" prop="ReceiptBank">
                    <el-input placeholder="请输入" v-model="formData.ReceiptBank" />
                </el-form-item>
                <el-form-item label="银行账号" prop="ReceiptAccount">
                    <el-input placeholder="请输入" v-model="formData.ReceiptAccount" />
                </el-form-item>
                <el-form-item label="企业地址" prop="Address">
                    <el-input placeholder="请输入" v-model="formData.Address" />
                </el-form-item>
                <el-form-item label="企业电话" prop="ContactInfo">
                    <el-input placeholder="请输入" v-model="formData.ContactInfo" />
                </el-form-item>
            </template>
        </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 { useInvoiceForm } from '@/business/user'
import AppDrawer from '@pc/components/base/drawer/index.vue'

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

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

const formRules: FormRules = {
    ReceiptType: [{ required: true, message: '请选择发票类型' }],
    UserName: [{ required: true, message: '请输入发票抬头', trigger: 'blur' }],
    TaxpayerID: [{ required: true, message: '请输入税号', trigger: 'blur' }],
    ReceiptBank: [{ required: true, message: '请输入开户银行', trigger: 'blur' }],
    ReceiptAccount: [{ required: true, message: '请输入银行账号', trigger: 'blur' }],
    Address: [{ required: true, message: '请输入企业地址', trigger: 'blur' }],
    ContactInfo: [{ required: true, message: '请输入企业电话', trigger: 'blur' }],
}

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>