<!-- 询价消息-卖方询价-同意/拒绝 -->
<template>
    <app-drawer :title="code === 'bargain_sell_agree' ? '同意' : '拒绝'" :width="720" v-model:show="show" :loading="loading"
        :refresh="refresh">
        <app-table-details :label-width="100" :data="selectedRow" :cell-props="detailProps" :column="2">
            <!-- 卖方出价 -->
            <template #applyprice>
                {{ selectedRow.zscurrencytypedisplayunit + formatDecimal(selectedRow.applyprice) }}
            </template>
            <!-- 申请状态 -->
            <template #applystatus>
                {{ getApplyStatusName(selectedRow.applystatus) }}
            </template>
        </app-table-details>
        <el-form ref="formRef" label-width="60px" :model="formData" :rules="formRules" style="margin-top: 20px;">
            <el-form-item label="备注" prop="AuditRemark">
                <el-input type="textarea" maxlength="120" :rows="3" v-model="formData.AuditRemark" />
            </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, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { formatDecimal } from '@/filters'
import { getApplyStatusName } from '@/constants/order'
import { useBargainOperate } from '@/business/bargain'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTableDetails from '@pc/components/base/table-details/index.vue'

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

const operateType = (() => {
    switch (props.code) {
        case 'bargain_sell_agree': {
            return 3
        }
        case 'bargain_sell_refuse': {
            return 2
        }
    }
    return 0
})()

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

const formRules: FormRules = {
    AuditRemark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
}

const detailProps = [
    { prop: 'zsshapetypedisplay', label: '形状' },
    { prop: 'zscolortypedisplay', label: '颜色' },
    { prop: 'zsclaritytypedisplay', label: '净度' },
    { prop: 'zscuttypedisplay', label: '切工' },
    { prop: 'zspolishtypedisplay', label: '抛光' },
    { prop: 'zssymmetrytypedisplay', label: '对称' },
    { prop: 'zsfluorescencetypedisplay', label: '荧光' },
    { prop: 'zssizedisplay', label: '尺寸' },
    { prop: 'sellusername', label: '卖方' },
    { prop: 'goodsno', label: '卖方商品' },
    { prop: 'applyqty', label: '克拉重量' },
    { prop: 'applyprice', label: '卖方出价' },
    { prop: 'applyremark', label: '备注' },
    { prop: 'applystatus', label: '申请状态' },
    { prop: 'applytime', label: '申请时间' },
]

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            ElMessageBox.confirm(`是否${props.code === 'bargain_sell_agree' ? '同意' : '拒绝'}此询价？`).then(() => {
                formSubmit(operateType).then(() => {
                    ElMessage.success('提交成功')
                    onCancel(true)
                }).catch((err) => {
                    ElMessage.error('提交失败：' + err)
                })
            })
        }
    })
}
</script>