<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="组织id" prop="org_id">
                    <el-select class="flex-1" v-model="formData.org_id" clearable placeholder="请选择组织id">
                        <!-- <el-option 
                            v-for="(item, index) in dictData."
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        /> -->
                    </el-select>
                </el-form-item>
                <el-form-item label="部门id" prop="department_id">
                    <el-select class="flex-1" v-model="formData.department_id" clearable placeholder="请选择部门id">
                        <!-- <el-option 
                            v-for="(item, index) in dictData."
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        /> -->
                    </el-select>
                </el-form-item>
                <el-form-item label="项目id" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                </el-form-item>
                <el-form-item label="客户需求id" prop="customer_demand_id">
                    <el-input v-model="formData.customer_demand_id" clearable placeholder="请输入客户需求id" />
                </el-form-item>
                <el-form-item label="概算来源" prop="estimate_source">
                    <el-radio-group v-model="formData.estimate_source" placeholder="请选择概算来源">
                        <el-radio v-for="(item, index) in dictData.estimate_source" :key="index"
                            :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="制单人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入制单人" />
                </el-form-item>
                <el-form-item label="报价日期" prop="quotation_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.quotation_date" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择报价日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="发票类型" prop="invoice_type">
                    <el-radio-group v-model="formData.invoice_type" placeholder="请选择发票类型">
                        <el-radio v-for="(item, index) in dictData.invoice_type" :key="index" :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="技术人员id" prop="technician">
                    <el-input v-model="formData.technician" clearable placeholder="请输入技术人员id" />
                </el-form-item>
                <el-form-item label="概算金额" prop="estimate_amount">
                    <el-input v-model="formData.estimate_amount" clearable placeholder="请输入概算金额" />
                </el-form-item>
                <el-form-item label="要求" prop="ask">
                    <el-input v-model="formData.ask" clearable placeholder="请输入要求" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectEstimateEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectEstimateAdd, apiProjectEstimateEdit, apiProjectEstimateDetail } from '@/api/project_estimate'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目概算' : '新增项目概算'
})

// 表单数据
const formData = reactive({
    id: '',
    org_id: '',
    department_id: '',
    project_id: '',
    customer_demand_id: '',
    estimate_source: '',
    create_user: '',
    quotation_date: '',
    invoice_type: '',
    technician: '',
    estimate_amount: '',
    ask: '',
    annex: '',
})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }

    //@ts-ignore
    formData.quotation_date = timeFormat(formData.quotation_date, 'yyyy-mm-dd hh:MM:ss')
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiProjectEstimateDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectEstimateEdit(data)
        : await apiProjectEstimateAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
