<!-- 添加商品-成批裸钻 -->
<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="平均重量" prop="WeightAvg">
            <el-input type="number" placeholder="请输入" v-model.number="formData.WeightAvg">
                <template #append>(克拉)</template>
            </el-input>
        </el-form-item>
        <el-form-item label="克拉单位">
            <span>{{ currencyInfo?.param2 }}{{ caratUnit }} (总价/总重量)</span>
        </el-form-item>
        <el-form-item class="el-form-item--row" label="形状" prop="ZSShapeType">
            <app-multiple class="el-form-item--col" :data-list="enums.shapeTypeList" v-model="formData.ZSShapeType"
                checkbox />
        </el-form-item>
        <el-form-item label="颜色" prop="ZSColorType">
            <app-multiple class="el-form-item--col" :data-list="enums.colorTypeList" v-model="selectedType.colorType" />
        </el-form-item>
        <el-form-item label="切工" prop="ZSCutType">
            <app-multiple class="el-form-item--col" :data-list="enums.cutTypeList" v-model="selectedType.cutType" />
        </el-form-item>
        <el-form-item label="净度" prop="ZSClarityType">
            <app-multiple class="el-form-item--col" :data-list="enums.clarityTypeList"
                v-model="selectedType.clarityType" />
        </el-form-item>
        <el-form-item label="抛光" prop="ZSPolishType">
            <app-multiple class="el-form-item--col" :data-list="enums.polishTypeList"
                v-model="selectedType.polishType" />
        </el-form-item>
        <el-form-item label="对称" prop="ZSSymmetryType">
            <app-multiple class="el-form-item--col" :data-list="enums.symmetryTypeList"
                v-model="selectedType.symmetryType" />
        </el-form-item>
        <el-form-item label="荧光" prop="ZSFluorescenceType">
            <app-multiple class="el-form-item--col" :data-list="enums.fluorescenceTypeList"
                v-model="selectedType.fluorescenceType" />
        </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="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 AppMultiple from '@pc/components/base/multiple/index.vue'
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, caratUnit, selectedType, enums, warehouseList, currencyInfo, formSubmit } = useDiamondForm(Category.Diamonds)
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: '请输入总重量' }],
    WeightAvg: [{ required: true, type: 'number', message: '请输入平均重量' }],
    ZSShapeType: [{ required: true, message: '请选择形状' }],
    WarehouseID: [{ required: true, message: '请选择仓库' }],
    PerformanceTemplateID: [{
        required: true,
        validator: (rule, value, callback) => {
            if (performanceId.value) {
                callback()
            } else {
                callback(new Error('请选择履约方式'))
            }
        }
    }],
    ZSColorType: [{
        required: true,
        validator: (rule, value, callback) => {
            if (formData.ZSColorType1 && formData.ZSColorType2) {
                callback()
            } else {
                callback(new Error('请选择颜色'))
            }
        }
    }],
    ZSClarityType: [{
        required: true,
        validator: (rule, value, callback) => {
            if (formData.ZSClarityType1 && formData.ZSClarityType2) {
                callback()
            } else {
                callback(new Error('请选择净度'))
            }
        }
    }],
    ZSCutType: [{
        validator: (rule, value, callback) => {
            if ((!formData.ZSCutType1 && !formData.ZSCutType2) || (formData.ZSCutType1 && formData.ZSCutType2)) {
                callback()
            } else {
                callback(new Error('请选择切工'))
            }
        }
    }],
    ZSPolishType: [{
        validator: (rule, value, callback) => {
            if ((!formData.ZSPolishType1 && !formData.ZSPolishType2) || (formData.ZSPolishType1 && formData.ZSPolishType2)) {
                callback()
            } else {
                callback(new Error('请选择抛光'))
            }
        }
    }],
    ZSSymmetryType: [{
        validator: (rule, value, callback) => {
            if ((!formData.ZSSymmetryType1 && !formData.ZSSymmetryType2) || (formData.ZSSymmetryType1 && formData.ZSSymmetryType2)) {
                callback()
            } else {
                callback(new Error('请选择对称'))
            }
        }
    }],
    ZSFluorescenceType: [{
        validator: (rule, value, callback) => {
            if ((!formData.ZSFluorescenceType1 && !formData.ZSFluorescenceType2) || (formData.ZSFluorescenceType1 && formData.ZSFluorescenceType2)) {
                callback()
            } else {
                callback(new Error('请选择荧光'))
            }
        }
    }]
}

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

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