<!-- 挂牌大厅-我的出售-我要出售 -->
<template>
    <app-drawer title="我要出售" :width="960" v-model:show="show" :loading="submitLoading" :refresh="refresh">
        <el-form ref="formRef" class="el-form--horizontal" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item label="商品分类" prop="ZSCategory">
                <el-select v-model="selectedCategoryId" :disabled="tableLoading" @change="getDiamondList">
                    <el-option :label="item.label" :value="item.value" v-for="(item, index) in categoryList"
                        :key="index" />
                </el-select>
            </el-form-item>
            <el-form-item label="履约方式" prop="PerformanceTemplateID">
                <component :is="components.performance" v-model="formData.PerformanceTemplateID" />
            </el-form-item>
        </el-form>
        <app-table :data="dataList" :columns="columns" :max-height="400" :show-header="false" :loading="tableLoading"
            selection-type="single" border @select="onTableSelect">
            <!-- 价格 -->
            <template #price="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.price) }}
            </template>
        </app-table>
        <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, defineAsyncComponent } from 'vue'
import Long from 'long'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { formatDecimal } from '@/filters'
import { useSellOrderListing } from '@/business/trade/listing'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTable from '@pc/components/base/table/index.vue'

const components = {
    performance: defineAsyncComponent(() => import('@pc/components/modules/performance/index.vue')),
}

const { tableLoading, submitLoading, formData, dataList, columns, selectedCategoryId, categoryList, getDiamondList, formSubmit } = useSellOrderListing()
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    ZSCategory: [{
        required: true,
        validator: (rule, value, callback) => {
            if (selectedCategoryId.value) {
                callback()
            } else {
                callback(new Error('请选择商品分类'))
            }
        }
    }],
    PerformanceTemplateID: [{ required: true, message: '请选择履约方式' }],
}

// 勾选表格行
const onTableSelect = (rows: Ermcp.MyWRPositionRsp[]) => {
    const row = rows[0]
    if (row) {
        formData.WRStandardID = row.wrstandardid
        formData.LadingBillID = Long.fromString(row.ladingbillid)
        formData.OrderQty = row.ftotalqty - row.ffreezeqty
        formData.SubNum = row.subnum
    } else {
        formData.WRStandardID = 0
    }
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            if (formData.WRStandardID) {
                formSubmit().then(() => {
                    ElMessage.success('提交成功')
                    onCancel(true)
                }).catch((err) => {
                    ElMessage.error('提交失败：' + err)
                })
            } else {
                ElMessage.warning('请选择商品')
            }
        }
    })
}

getDiamondList()
</script>