<!-- 订单管理-我的履约-修改联络信息 -->
<template>
    <app-drawer title="联络信息" :width="512" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="80px" :model="formData" :rules="formRules">
            <el-form-item label="履约计划">
                <span>{{ handleNoneValue(selectedRow.curstepid) }}</span>
            </el-form-item>
            <el-form-item label="联络信息">
                <el-input type="textarea" :rows="3" v-model="formData.ContactInfo" />
            </el-form-item>
            <template v-if="code === 'order_buy_edit'">
                <el-form-item label="收货地址">
                    <div class="el-form-item--col">
                        <el-input type="textarea" :rows="2" v-model="formData.ReceiveInfo" />
                        <el-dropdown trigger="click" v-if="addressList.length">
                            <el-icon :size="20" style="cursor: pointer;">
                                <CirclePlusFilled />
                            </el-icon>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item v-for="(item, index) in addressList" :key="index"
                                        @click="addressChange(item)">
                                        {{ [item.provincename, item.cityname, item.districtname, item.address].join(' ') }}
                                    </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </div>
                </el-form-item>
                <el-form-item label="发票信息">
                    <div class="el-form-item--col">
                        <el-input type="textarea" :rows="2" v-model="formData.ReceiptInfo" />
                        <el-dropdown trigger="click" v-if="invoiceList.length">
                            <el-icon :size="20" style="cursor: pointer;">
                                <CirclePlusFilled />
                            </el-icon>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item v-for="(item, index) in invoiceList" :key="index"
                                        @click="invoiceChange(item)">
                                        {{ [getReceiptTypeName(item.receipttype), item.username].join(' ') }}
                                    </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </div>
                </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, onMounted } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { handleNoneValue } from '@/filters'
import { getReceiptTypeName } from '@/constants/receipt'
import { useRequest } from '@/hooks/request'
import { queryUserReceiveInfo, queryWrUserReceiptInfo } from '@/services/api/user'
import { usePerformanceModify } from '@/business/order'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const props = defineProps({
    code: String,
    selectedRow: {
        type: Object as PropType<Ermcp.MyPerformancRsp>,
        required: true
    }
})

const { loading, formData, formSubmit } = usePerformanceModify(props.selectedRow)
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()
const formRules: FormRules = {}

const { dataList: addressList } = useRequest(queryUserReceiveInfo)
const { dataList: invoiceList } = useRequest(queryWrUserReceiptInfo)

// 选择地址
const addressChange = (item: Ermcp.UserReceiveInfoRsp) => {
    formData.ReceiveInfo = [item.provincename, item.cityname, item.districtname, item.address].join(' ')
}

// 选择发票
const invoiceChange = (item: Ermcp.WrUserReceiptInfoRsp) => {
    formData.ReceiptInfo = ''
    Object.entries(item).forEach(([key, value]) => {
        if (value !== '') {
            switch (key) {
                case 'receipttype': {
                    formData.ReceiptInfo += '发票类型:' + getReceiptTypeName(Number(value)) + '\n'
                    break
                }
                case 'username': {
                    formData.ReceiptInfo += '户名:' + value + '\n'
                    break
                }
                case 'address': {
                    formData.ReceiptInfo += '地址:' + value + '\n'
                    break
                }
                case 'contactinfo': {
                    formData.ReceiptInfo += '联系方式:' + value + '\n'
                    break
                }
                case 'idnum': {
                    formData.ReceiptInfo += '身份证号码:' + value + '\n'
                    break
                }
                case 'receiptaccount': {
                    formData.ReceiptInfo += '发票帐号:' + value + '\n'
                    break
                }
                case 'receiptbank': {
                    formData.ReceiptInfo += '发票开户行:' + value + '\n'
                    break
                }
                case 'taxpayerid': {
                    formData.ReceiptInfo += '纳税人识别号:' + value + '\n'
                    break
                }
            }
        }
    })
}

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

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

onMounted(() => {
    switch (props.code) {
        case 'order_buy_edit':
            if (props.selectedRow.buyerinfo) {
                const res = JSON.parse(props.selectedRow.buyerinfo)
                formData.ContactInfo = res.ContactInfo
                formData.ReceiveInfo = res.ReceiveInfo
                formData.ReceiptInfo = res.ReceiptInfo
            }
            break
        case 'order_sell_edit':
            if (props.selectedRow.sellerinfo) {
                const res = JSON.parse(props.selectedRow.sellerinfo)
                formData.ContactInfo = res.ContactInfo
            }
            break
    }
})
</script>