<template>
    <div class="edit-popup">

        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">

            <el-form ref="formRef" :model="formData" label-width="auto" :rules="formRules">



                <el-row :gutter="10">

                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id" @click="showDialog2 = true">
                            <el-input v-model="project_name" clearable placeholder="请选择项目" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id">
                            <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="工序步骤号" prop="process_id" @click="showDialog1 = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="process_step_no" clearable placeholder="请选择施工工序" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工序步骤" prop="process_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="process_step" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="anneximg && anneximg.length > 0">
                        <el-form-item label="图例说明">
                            <!-- <div v-if="anneximg && anneximg.length > 0">
                                <material-picker v-model="anneximg" />
                            </div>
                            <div v-else>暂无图例</div> -->


                            <div v-if="formData.file?.length > 0">
                                <img :src="item.uri" v-for="item in formData.file" style="width:200px" />
                            </div>
                            <div v-else>暂无图例</div>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="质量控制点" prop="process_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="quality_control_points" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="计划开始日期" prop="plan_start_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.plan_start_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择计划开始日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计划结束日期" prop="plan_end_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.plan_end_date" clearable type="date"
                                value-format="YYYY-MM-DD " placeholder="选择计划结束日期">
                            </el-date-picker>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="班组长" prop="project_member_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formDatamember" clearable readonly placeholder="点击选择项目人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="施工人员" prop="work_user">
                            <el-input v-model="formData.work_user" clearable placeholder="请输入施工人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="施工内容" prop="work_content">
                            <el-input v-model="formData.work_content" type="textarea" clearable placeholder="请输入施工内容" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="作业量" prop="workload"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.workload" type="number" clearable placeholder="请输入作业量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单位" prop="unit"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.unit" clearable placeholder="请输入单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单价" prop="price"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.price" type="number" clearable placeholder="请输入单价" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="金额" prop="amount"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.amount" type="number" clearable placeholder="请输入金额" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <div v-if="showDialog">
                    <el-dialog v-model="showDialog" title="选择项目成员" width="70%">
                        <customDialog @customEvent="customEvent" :project_id="formData.project_id"></customDialog>
                    </el-dialog>
                </div>

                <el-dialog v-model="showDialog1" title="选择施工工序" width="70%">
                    <builddivisionDialog @customEvent="customEvent1" division_id=""></builddivisionDialog>
                </el-dialog>

                <el-dialog v-model="showDialog2" title="选择项目" width="70%">
                    <projectDialog @customEvent="customEvent2"></projectDialog>
                </el-dialog>

            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import customDialog from '@/components/project-member/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { buildplanAdd, buildplanEdit, buildplanDetail } from '@/api/build/build_plan'
import builddivisionDialog from '@/components/build_process/index.vue'
import { toChinesNum } from "@/utils/util";
import { getAllProjectTypes } from '@/api/projecttype'
import { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
import type { PropType } from 'vue'
import feedback from '@/utils/feedback'



const project_name = ref('')
const project_code = ref('')
const formDatamember = ref('')
const process_step_no = ref('')
const process_step = ref('')
const anneximg = ref([])
const quality_control_points = ref('')

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 showDialog1 = ref(false)
const showDialog2 = ref(false)
const showDialog = ref(false)
const customEvent = (e: any) => {
    formData.project_member_id = e.id;
    formDatamember.value = e.admin_name;
    showDialog.value = false;
};
const customEvent1 = (e: any) => {
    formData.process_id = e.id;
    process_step_no.value = e.process_step_no
    process_step.value = e.process_step
    if (e.file && e.file.length > 0) {
        anneximg.value = e.file
        console.log(e.file, '1')
    }

    quality_control_points.value = e.quality_control_points
    showDialog1.value = false;
};

const customEvent2 = (e: any) => {
    formData.project_id = e.id;
    project_name.value = e.name;
    project_code.value = e.project_code;
    showDialog2.value = false;
};


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑施工计划表' : '新增施工计划表'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: "",
    process_id: "",
    project_member_id: 0,
    plan_start_date: '',
    plan_end_date: '',
    work_user: "",
    work_content: "",
    unit: "",
    price: "",
    amount: "",
    workload: "",
    file: ""

})


// 表单验证
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]
        }
    }
    formDatamember.value = data.project_member_name;
    project_name.value = data.project_name
    project_code.value = data.project_code
    process_step_no.value = data.process_step_no
    process_step.value = data.process_step
    if (data.file && data.file.length > 0) {
        anneximg.value = data.file
    }

    quality_control_points.value = data.quality_control_points



}

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


// 提交按钮
const handleSubmit = async () => {
    const endTime = new Date(formData.plan_end_date)
    const startTime = new Date(formData.plan_start_date)
    if (endTime < startTime) {
        feedback.msgError("结束时间不能小于开始时间");
        return
    } else if (startTime > endTime) {
        feedback.msgError("开始时间不能大于结束时间");
        return
    } else {
        console.log("开始时间和结束时间合法");
    }

    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await buildplanEdit(data)
        : await buildplanAdd(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>
