<template>
    <app-drawer :title="detail?.OrderIndex ? '修改批次' : '新增批次'" :width="860" v-model:show="show">
        <el-form ref="formRef" class="el-form--horizontal" label-width="130px" :model="formItem" :rules="formRules">
            <el-form-item label="货物编号" prop="GZNo">
                <el-input placeholder="请输入" v-model="formItem.GZNo" />
            </el-form-item>
            <el-form-item label="形状" prop="GZCJShapeType">
                <el-select v-model="formItem.GZCJShapeType">
                    <el-option :label="item.label" :value="item.value" v-for="(item, index) in getGZCJShapeTypeList()"
                        :key="index" />
                </el-select>
            </el-form-item>
            <el-form-item label="重量(ct)" prop="Weight">
                <el-input type="number" placeholder="请输入" v-model="formItem.Weight" />
            </el-form-item>
            <el-form-item label="参考货值(USD)" prop="Amount">
                <el-input type="number" placeholder="请输入" v-model="formItem.Amount" />
            </el-form-item>
            <template v-if="formData.GZCJAccountType === 2">
                <el-form-item label="刻印服务" prop="GZCJMarkType">
                    <el-select v-model="formItem.GZCJMarkType">
                        <el-option :label="item.label" :value="item.value" v-for="(item, index) in getGZCJMarkTypeList()"
                            :key="index" />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否披露处理" prop="GZCJPublishType">
                    <el-select v-model="formItem.GZCJPublishType">
                        <el-option :label="item.label" :value="item.value" v-for="(item, index) in getGZCJPublishTypeList()"
                            :key="index" />
                    </el-select>
                </el-form-item>
                <el-form-item label="服务类别" prop="GZCJServiceType">
                    <el-select v-model="formItem.GZCJServiceType" filterable>
                        <el-option :label="item.enumdicname" :value="item.enumitemname"
                            v-for="(item, index) in gzcjServiceTypeList" :key="index" />
                    </el-select>
                </el-form-item>
                <el-form-item label="原证书号" prop="OriginCertNo">
                    <el-input placeholder="请输入" v-model="formItem.OriginCertNo" />
                </el-form-item>
            </template>
            <el-form-item label="彩钻信息" prop="ColorInfo">
                <el-input placeholder="请输入" v-model="formItem.ColorInfo" />
            </el-form-item>
            <el-form-item class="el-form-item--row" label="其他" prop="Remark">
                <el-input type="textarea" maxlength="120" v-model="formItem.Remark" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel" plain>取消</el-button>
            <el-button type="primary" @click="onSubmit">保存</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, computed } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useEnumStore } from '@/stores'
import { getGZCJShapeTypeList, getGZCJMarkTypeList, getGZCJPublishTypeList } from '@/constants/customs'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const props = defineProps({
    formData: {
        type: Object as PropType<Proto.CJJCApplyReq>,
        required: true,
    },
    detail: {
        type: Object as PropType<Proto.GZCJCategoryDetail>
    }
})

const emit = defineEmits(['update'])
const { enumMap } = useEnumStore()
const show = ref(true)
const formRef = ref<FormInstance>()
const formItem = ref<Partial<Proto.GZCJCategoryDetail>>({ ...props.detail })

// 服务类别列表
const gzcjServiceTypeList = computed(() => {
    const enums = enumMap.get('GZCJServiceType')
    if (enums) {
        return enums.value.filter(({ enumitemvalue }) => {
            switch (props.formData.GZCJCategoryType) {
                case 1:
                case 3: {
                    return enumitemvalue === '0' || enumitemvalue === '1'
                }
                case 2: {
                    return enumitemvalue === '0' || enumitemvalue === '2'
                }
            }
            return false
        })
    }
    return []
})

const formRules: FormRules = {
    GZNo: [{
        required: true,
        message: '请输入货物编号'
    }],
    GZCJShapeType: [{
        required: true,
        message: '请选择形状'
    }],
    Weight: [{
        required: true,
        message: '请输入重量'
    }],
    Amount: [{
        required: true,
        message: '请输入参考货值'
    }],
    GZCJMarkType: [{
        required: true,
        message: '请选择刻印服务'
    }],
    GZCJPublishType: [{
        required: true,
        message: '请选择是否披露处理'
    }],
    GZCJServiceType: [{
        required: true,
        message: '请选择服务类别'
    }],
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            emit('update', formItem.value)
            onCancel()
        }
    })
}
</script>