<!-- 添加商品-彩色钻石 -->
<template>
    <el-form ref="formRef" class="el-form--horizontal" label-width="100px" :model="formData" :rules="formRules">
        <el-form-item label="商品编号" prop="GoodsNo">
            <el-input placeholder="请输入" v-model="formData.GoodsNo" />
        </el-form-item>
        <el-form-item label="履约方式" prop="PerformanceTemplateID" v-if="code === 'warehousing_goods_sell'">
            <component :is="components.performance" v-model="performanceId" />
        </el-form-item>
        <el-form-item label="货币类型" prop="ZSCurrencyType">
            <el-radio-group v-model="formData.ZSCurrencyType">
                <el-radio :label="item.value" v-for="(item, index) in enums.currencyTypeList" :key="index">
                    {{ item.label }}
                </el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="价格" prop="Price">
            <el-input type="number" placeholder="请输入" v-model.number="formData.Price">
                <template #append>{{ currencyInfo?.enumitemvalue }}</template>
            </el-input>
        </el-form-item>
        <el-form-item label="克拉重量" prop="Weight">
            <el-input type="number" placeholder="请输入" v-model.number="formData.Weight">
                <template #append>(克拉)</template>
            </el-input>
        </el-form-item>
        <el-form-item label="克拉单位">
            <span>{{ currencyInfo?.param2 }}{{ caratUnit }} (总价/总重量)</span>
        </el-form-item>
        <el-form-item label="尺寸" prop="Size">
            <div class="el-form-item--col">
                <el-input-number v-model="formData.Size1" :controls="false" />
                <span>×</span>
                <el-input-number v-model="formData.Size2" :controls="false" />
                <span>×</span>
                <el-input-number v-model="formData.Size3" :controls="false" />
            </div>
        </el-form-item>
        <el-form-item label="形状" prop="ZSShapeType">
            <el-select v-model="shapeId" @change="shapeChange">
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in enums.shapeTypeList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="颜色" prop="ZSCZColorType">
            <div class="el-form-item--col">
                <el-select v-model="formData.ZSCZColor1Type">
                    <el-option :label="item.label" :value="item.value"
                        v-for="(item, index) in enums.fancyColorType1List" :key="index" />
                </el-select>
                <el-select v-model="formData.ZSCZColor2Type">
                    <el-option :label="item.label" :value="item.value"
                        v-for="(item, index) in enums.fancyColorType2List" :key="index" />
                </el-select>
                <el-select v-model="formData.ZSCZColor3Type">
                    <el-option :label="item.label" :value="item.value"
                        v-for="(item, index) in enums.fancyColorType3List" :key="index" />
                </el-select>
            </div>
        </el-form-item>
        <el-form-item label="净度" prop="ZSClarityType1">
            <el-select v-model="formData.ZSClarityType1">
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in enums.clarityTypeList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="抛光" prop="ZSPolishType1">
            <el-select v-model="formData.ZSPolishType1" clearable>
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in enums.polishTypeList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="对称" prop="ZSSymmetryType1">
            <el-select v-model="formData.ZSSymmetryType1" clearable>
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in enums.symmetryTypeList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="荧光" prop="ZSFluorescenceType1">
            <el-select v-model="formData.ZSFluorescenceType1" clearable>
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in enums.fluorescenceTypeList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="仓库" prop="WarehouseID">
            <el-select v-model="formData.WarehouseID">
                <el-option :label="item.warehousename" :value="item.autoid" v-for="(item, index) in warehouseList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="市场价" prop="MarketPrice">
            <el-input type="number" placeholder="请输入" v-model.number="formData.MarketPrice">
                <template #append>{{ currencyInfo?.enumitemvalue }}</template>
            </el-input>
        </el-form-item>
        <el-form-item label="证书类型" prop="ZSCertType">
            <el-select v-model="formData.ZSCertType" clearable>
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in enums.certTypeList"
                    :key="index" />
            </el-select>
        </el-form-item>
        <el-form-item label="证书编号" prop="CerNo">
            <el-input placeholder="请输入" v-model="formData.CerNo" />
        </el-form-item>
        <el-form-item label="商品照片" prop="ImagePath">
            <app-upload />
        </el-form-item>
        <el-form-item class="el-form-item--row" label="备注" prop="Remark">
            <el-input type="textarea" v-model="formData.Remark" />
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { ref, PropType, defineAsyncComponent } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { Category } from '@/constants/diamond'
import { useDiamondForm } from '@/business/goods'
import AppUpload from '@/components/base/upload/index.vue'

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

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

const emit = defineEmits(['cancel'])
const { loading, formData, performanceId, shapeId, caratUnit, enums, warehouseList, currencyInfo, addOrUpdate } = useDiamondForm(Category.Fancy)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    ZSCategory: [{ required: true, message: '请选择商品分类' }],
    GoodsNo: [{ required: true, message: '请输入商品编号', trigger: 'blur' }],
    ZSCurrencyType: [{ required: true, message: '请选择货币类型' }],
    Price: [{ required: true, type: 'number', message: '请输入价格' }],
    Weight: [{ required: true, type: 'number', message: '请输入克拉重量' }],
    ZSClarityType1: [{ required: true, message: '请选择净度' }],
    WarehouseID: [{ required: true, message: '请选择仓库' }],
    PerformanceTemplateID: [{
        required: true,
        validator: (rule, value, callback) => {
            if (performanceId.value) {
                callback()
            } else {
                callback(new Error('请选择履约方式'))
            }
        }
    }],
    ZSShapeType: [{
        required: true,
        validator: (rule, value, callback) => {
            if (shapeId.value) {
                callback()
            } else {
                callback(new Error('请选择形状'))
            }
        }
    }],
    ZSCZColorType: [{
        required: true,
        validator: (rule, value, callback) => {
            if (formData.ZSCZColor1Type && formData.ZSCZColor2Type && formData.ZSCZColor3Type) {
                callback()
            } else {
                callback(new Error('请选择颜色'))
            }
        }
    }],
    Size: [{
        validator: (rule, value, callback) => {
            const { Size1, Size2, Size3 } = formData
            if ((!Size1 && !Size2 && !Size3) || (Size1 && Size2 && Size3)) {
                callback()
            } else {
                callback(new Error('请输入尺寸'))
            }
        }
    }],
}

// 选择形状
const shapeChange = (value: number) => {
    if (value) {
        formData.ZSShapeType = [value]
    } else {
        formData.ZSShapeType = []
    }
}

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

// 暴露组件属性给父组件调用
defineExpose({
    loading,
    submit: onSubmit
})
</script>