<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 placeholder="请输入项目编码" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="工序步骤号" prop="process_step_no" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.process_step_no" clearable placeholder="请输入工序步骤号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工序步骤" prop="process_step" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.process_step" clearable placeholder="请输入工序步骤" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="图例说明">
                            <material-picker v-model="formData.annex" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="质量控制点" prop="quality_control_points" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.quality_control_points" clearable placeholder="请输入质量控制点" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="计划开始日期" prop="start_time" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.start_time" 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="deliver_time" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.deliver_time" 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="remark" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.remark" clearable placeholder="请输入班组长" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="施工内容" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入施工内容" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="作业量" prop="remark" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.remark" type="number" clearable placeholder="请输入作业量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="剩余作业量" prop="remark" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.remark" clearable placeholder="请输入剩余作业量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="填报人" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入填报人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="填报日期" prop="deliver_time" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.deliver_time" clearable type="date" value-format="YYYY-MM-DD " placeholder="选择填报日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <div>
                    人工明细
                </div>
                <el-row>

                    <el-table :data="tableData">
                        <el-table-column label="序号" width="105px">
                            <template #default="{ row }">
                                <el-button @click="handleAdd(row)" size="small">+</el-button>
                                <el-button @click="handleDelete(row)" size="small">-</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="一级科目" prop="name1">
                            <template #default="{ row, column, $index }">
                                <el-input v-model="row.name1" @click="subjectmu(row, column, $index)" />
                            </template>
                        </el-table-column>
                        <el-table-column label="二级科目" prop="name2">
                            <template #default="{ row }"> <el-input v-model="row.name2" /> </template></el-table-column>
                        <el-table-column label="1月" prop="month1">
                            <template #default="{ row }"> <el-input v-model="row.month1" type="number" /> </template></el-table-column>

                        <el-table-column label="2月" prop="month2">
                            <template #default="{ row }">
                                <el-input v-model="row.month2" type="number" />
                            </template>
                        </el-table-column>

                        <el-table-column label="3月" prop="month3">
                            <template #default="{ row }"> <el-input v-model="row.month3" type="number" /> </template></el-table-column>

                        <el-table-column label="4月" prop="month4">
                            <template #default="{ row }">
                                <el-input v-model="row.month4" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="5月" prop="month5">
                            <template #default="{ row }">
                                <el-input v-model="row.month5" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="6月" prop="month6">
                            <template #default="{ row }">
                                <el-input v-model="row.month6" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="7月" prop="month7">
                            <template #default="{ row }">
                                <el-input v-model="row.month7" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="8月" prop="month8">
                            <template #default="{ row }">
                                <el-input v-model="row.month8" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="9月" prop="month9">
                            <template #default="{ row }">
                                <el-input v-model="row.month9" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="10月" prop="month10">
                            <template #default="{ row }">
                                <el-input v-model="row.month10" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="11月" prop="month11">
                            <template #default="{ row }">
                                <el-input v-model="row.month11" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="12月" prop="month12">
                            <template #default="{ row }">
                                <el-input v-model="row.month12" type="number" />
                            </template>
                        </el-table-column>

                        <!-- 其他列 -->
                    </el-table>
                </el-row>




                <el-dialog v-model="showDialog" title="选择项目成员" width="70%">
                    <customDialog @customEvent="customEvent"></customDialog>
                </el-dialog>


                <el-dialog v-model="showDialog2" title="选择项目" width="70%">
                    <projectDialog @customEvent="customEvent2"></projectDialog>
                </el-dialog>
                <personnelselector ref="personnel" @confirm="submituser" type="1"></personnelselector>
            </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 salescontractDialog from '@/components/salescontract/index.vue'
import projectDialog from '@/components/project/index.vue'
import { apiProjectplanAdd, apiProjectplanEdit, apiProjectplanDetail } from '@/api/project_plan'

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'


const project_name = ref('')
const project_code = ref('')
const custom_name = ref('')
const manage_name = ref('')
const personnel = ref<any>()

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 showDialog = ref(false)
const showDialog2 = ref(false)
const formDataannex = reactive([])

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

    showDialog2.value = false;
};
const tableData = ref([
    // 表格数据
    { name1: "", name2: "", month1: ' ', month2: ' ', month3: ' ', month4: ' ', month5: ' ', month6: ' ', month7: ' ', month8: ' ', month9: ' ', month10: ' ', month11: ' ', month12: ' ', },
]);

const handleAdd = (row) => {
    // 在 row 后面插入一行数据
    const index = tableData.value.indexOf(row);
    tableData.value.splice(index + 1, 0, { name1: "", name2: "", month1: ' ', month2: ' ', month3: ' ', month4: ' ', month5: ' ', month6: ' ', month7: ' ', month8: ' ', month9: ' ', month10: ' ', month11: ' ', month12: ' ', });
};

const handleDelete = (row) => {
    // 删除 row
    const index = tableData.value.indexOf(row);
    tableData.value.splice(index, 1);
};


//打开弹窗

const userclick = () => {
    // console.log(personnel.value, '1111111')
    personnel.value.open()

}
//确认
const submituser = (e: any) => {
    formData.project_manage = e.id;
    manage_name.value = e.name;
}



//监听输入
const amountinput = (e) => {
    // console.log(e)
    if (e && e > 0) {
        formData.amount_daxie = toChinesNum(e)
    }
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目计划表' : '新增项目计划表'
})

// 表单数据
const formData = reactive({
    id: '',
    org_id: "",
    dept_id: "",
    project_id: 0,
    declare_time: '',
    project_manage: '',
    project_approval_date: "",
    start_time: "",
    deliver_time: "",
    remark: "",
    annex: "",
    process_approval: '{"current_step":"1","action":"1","submit_step":"2","check_user":"2","check_opinions":"dgfdsfds","copy_user":"3,4"}'
})


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

})

//获取所有组织
const getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)
        if (res.length > 0) {
            formData.org_id = res[0].id
            deptAll({ 'org_id': res[0].id }).then((res) => {
                if (res.length > 0) {
                    Object.assign(list2, res)
                    formData.dept_id = res[0].id
                }

            })
        }
    })
}
const deptrmt = (e) => {
    getlist1(e)
}
//获取所有部门
const getlist1 = (id: any) => {
    deptAll({ 'org_id': id }).then((res) => {
        Object.assign(list2, res)
    })
}

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    if (data.annex && data.annex.length > 0) {

        const arry1 = data.annex.map((item: any, index: any) => {
            return {
                name: `文件${index + 1}`,
                uri: item
            };
        });
        Object.assign(formDataannex, arry1)

    }


    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    custom_name.value = data.custom_name
    project_name.value = data.project_name
    project_code.value = data.project_code
    manage_name.value = data.project_manager_name

    if (data && data.contract_name) {
        contract_name.value = data.contract_name;

    }

}

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


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = JSON.stringify(formDataannex.map((item: any) => item.uri))
    }


    await formRef.value?.validate()

    const data = { ...formData }
    mode.value == 'edit'
        ? await apiProjectplanEdit(data)
        : await apiProjectplanAdd(data)
    popupRef.value?.close()
    emit('success')
}

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


    getlist()

}

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



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