<!-- 添加商品-成批毛坯 -->
<template>
    <el-form ref="formRef" class="el-form--horizontal" label-width="130px" :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="`总价(${currencyInfo?.enumitemvalue})`" prop="Price">
            <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formData.Price" />
        </el-form-item>
        <el-form-item label="总重量(克拉)" prop="Weight">
            <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formData.Weight" />
        </el-form-item>
        <el-form-item label="平均重量(克拉)" prop="WeightAvg">
            <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formData.WeightAvg" />
        </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="ZSCrystalType">
            <app-multiple class="el-form-item--col" :data-list="enums.crystalTypeList" v-model="formData.ZSCrystalType"
                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="ZSFluorescenceType">
            <app-multiple class="el-form-item--col" :data-list="enums.fluorescenceTypeList"
                v-model="selectedType.fluorescenceType" />
        </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="Origin">
            <el-input placeholder="请输入" v-model="formData.Origin" />
        </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="CPCertNo">
            <el-input placeholder="请输入" v-model="formData.CPCertNo" />
        </el-form-item>
        <el-form-item :label="`市场价(${currencyInfo?.enumitemvalue})`" prop="MarketPrice">
            <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formData.MarketPrice" />
        </el-form-item>
        <el-form-item label="金伯利重量" prop="KPWeight">
            <el-input type="number" placeholder="请输入" v-model="formData.KPWeight">
                <template #append>(克拉)</template>
            </el-input>
        </el-form-item>
        <el-form-item label="商品图片" prop="ImagePath">
            <app-upload :file-types="['image']" type-message="请选择正确的图片类型" @change="onUploadChange" />
        </el-form-item>
        <el-form-item label="金伯利图片" prop="KPPath">
            <app-upload />
        </el-form-item>
        <el-form-item label="仓单扫描件" prop="WRPath">
            <app-upload />
        </el-form-item>
        <el-form-item class="el-form-item--row" label="备注" prop="Remark">
            <el-input type="textarea" maxlength="120" v-model="formData.Remark" />
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { ref, PropType, defineAsyncComponent } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { regular } from '@/constants/regex'
import { Category } from '@/constants/diamond'
import { maxValue } from '@/constants/common'
import { useDiamondForm } from '@/business/goods'
import AppMultiple from '@pc/components/base/multiple/index.vue'
import AppUpload from '@pc/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, addOrUpdate } = useDiamondForm(Category.Rough)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    ZSCategory: [{ required: true, message: '请选择商品分类' }],
    GoodsNo: [{ required: true, message: '请输入商品编号', trigger: 'blur' }],
    Origin: [{ required: true, message: '请输入原产地', trigger: 'blur' }],
    CPCertNo: [{ required: true, message: '请输入金伯利证书编号', trigger: 'blur' }],
    KPWeight: [{ required: true, message: '请输入金伯利证书重量', trigger: 'blur' }],
    ZSCurrencyType: [{ required: true, message: '请选择货币类型' }],
    Price: [{
        required: true,
        message: '请输入总价',
        pattern: regular.positive.reg
    }],
    Weight: [{
        required: true,
        message: '请输入总重量',
        pattern: regular.positive.reg
    }],
    WeightAvg: [{
        required: true,
        message: '请输入平均重量',
        pattern: regular.positive.reg
    }],
    WarehouseID: [{ required: true, message: '请选择仓库' }],
    KPPath: [{ 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: [{
        validator: (rule, value, callback) => {
            if ((!formData.ZSClarityType1 && !formData.ZSClarityType2) || (formData.ZSClarityType1 && formData.ZSClarityType2)) {
                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 onUploadChange = (file: { filePath: string }) => {
    formData.ImagePath = file.filePath
}

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>