<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-row>

                    <el-col :span="8">
                        <el-form-item label="日记工单号" prop="attendance_id">
                            <el-input v-model="formData.attendance_code" clearable placeholder="点击选择考勤记录" readonly
                                @click="showDialog = true" />
                        </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="系统自动填写"
                                :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="日期" prop="attendance_date">
                            <el-date-picker class="flex-1 !flex" clearable v-model="formData.attendance_date" 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="person_id" @click="showDialog1 = true">
                            <el-input v-model="formData.person_name" clearable readonly placeholder="请选择人员"
                                :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="身份证号" prop="person_id">
                            <el-input v-model="idcard" clearable disabled placeholder="系统自动填写"
                                :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工种" prop="person_id">
                            <el-input v-model="work_type" clearable disabled placeholder="系统自动填写"
                                :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="上班时间" prop="work_start_time">

                            <el-time-select v-model="formData.work_start_time" start="06:30" step="00:15" end="23:30"
                                placeholder="请选择" class="flex-1 flex" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="下班时间" prop="work_end_time" :rules="{
                            validator: chekcDate,
                            trigger: ['change']
                        }">
                            <el-time-select v-model="formData.work_end_time" start="06:30" step="00:15" end="23:30"
                                placeholder="请选择" class="flex-1 flex" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="记工数量" prop="work_record_num">
                            <el-input v-model="formData.work_record_num" type="number" clearable placeholder="请输入记工数量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="日工资" prop="daily_salary">
                            <el-input v-model="formData.daily_salary" type="number" clearable placeholder="请输入日工资" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="日生活费" prop="daily_living">
                            <el-input v-model="formData.daily_living" type="number" clearable placeholder="请输入日生活费" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="日补贴" prop="daily_subsidy">
                            <el-input v-model="formData.daily_subsidy" type="number" clearable placeholder="请输入日补贴" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="日其它" prop="daily_other">
                            <el-input v-model="formData.daily_other" type="number" clearable placeholder="请输入日其它" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="日收入" prop="daily_income">
                            <el-input v-model="formData.daily_income" type="number" 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-row>
            </el-form>
            <el-dialog v-model="showDialog" title="选择考勤记录" width="70%">
                <dialogTable :config="project_attendance_record" @customEvent="customEvent">
                </dialogTable>
            </el-dialog>
            <div v-if="showDialog1">
                <el-dialog v-model="showDialog1" title="选择项目人员" width="70%">
                    <customDialog @customEvent="customEvent1" :project_id="formData.project_id"></customDialog>
                </el-dialog>
            </div>
        </popup>


    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { attendancedetailAdd, attendancedetailEdit, attendancedetailDetail } from '@/api/project_attendance_detail'
import { project_attendance_record } from "@/components/dialogTable/dialogTableConfig";



import customDialog from '@/components/project_personnel/index.vue'
let props = defineProps({
    project: Object
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog = ref(false)
const showDialog1 = ref(false)
const project_name = ref('')
const project_code = ref('')
const idcard = ref('')
const work_type = ref('')
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
const userInfo = userStore.userInfo
// 表单数据
const formData = reactive({
    id: '',
    attendance_id: "",
    person_id: '',
    attendance_code: "",
    attendance_date: '',
    work_start_time: '',
    work_end_time: '',
    work_record_num: '',
    daily_salary: '',
    daily_living: '',
    daily_subsidy: '',
    daily_other: '',
    daily_income: '',
    remark: '',
    people: "",
    person_name: ""

})
//获取值
const customEvent = (e) => {
    console.log(e)
    formData.attendance_id = e.id
    formData.attendance_code = e.attendance_code
    showDialog.value = false
}


// if (props.project) customEvent(props.project);

const customEvent1 = (e) => {
    formData.person_id = e.id
    formData.person_name = e.name
    idcard.value = e.idcard
    work_type.value = e.work_type_text
    showDialog1.value = false

}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑考勤明细' : '新增考勤明细'
})

const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date('2024-4-9 ' + formData.work_end_time) < new Date('2024-4-9 ' + formData.work_start_time)) {
        callback(new Error('下班时间时间不能早于上班时间'))
    } else {
        callback()
    }
}

// 表单验证
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]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code
    idcard.value = data.person_idcard
    work_type.value = data.work_type_text
}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    // formData.work_start_time = formData.work_start_time ? formData.attendance_date + ` ${formData.work_start_time}` : ""
    // formData.work_end_time = formData.work_end_time ? formData.attendance_date + ` ${formData.work_end_time}` : ""
    const data = { ...formData }
    mode.value == 'edit'
        ? await attendancedetailEdit(data)
        : await attendancedetailAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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


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