<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="auto" :rules="formRules">
                <el-row>

                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id" @click="showDialog = true">
                            <el-input v-model="project_name" clearable placeholder="请输入项目名称" :disabled="project" />
                        </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="请选择" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="下班时间" prop="work_end_time">

                            <el-time-select v-model="formData.work_end_time" start="06:30" step="00:15" end="23:30"
                                placeholder="请选择" />
                        </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%">
                <projectDialog @customEvent="customEvent"></projectDialog>
            </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 customDialog from '@/components/project_personnel/index.vue'
import type { PropType } from 'vue'
import { useDictData } from '@/hooks/useDictOptions'
const { dictData } = useDictData('labor_contract_status,labor_contract_type,labor_contract_name')
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 people = ref('')
const work_type = ref('')
import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const formDataannex = reactive([])
const userInfo = userStore.userInfo
// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    person_id: '',
    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) => {
    formData.project_id = e.id
    project_name.value = e.name
    project_code.value = e.project_code
    idcard.value = ''
    work_type.value = ''
    showDialog.value = false
    formData.people = ''
    console.log(people.value, "name")
}


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

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

}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑考勤明细' : '新增考勤明细'
})
// 上传文件
const handleAvatarSuccess_four = (
    response,
    uploadFile
) => {
    if (response.code == 0) {
        ElMessage.error(response.msg);
        return;
    }
    formDataannex.push(
        { uri: response.data.uri, name: response.data.name }


    );
};
// 删除上传的文件
const delFileFn = (index: number) => {
    formDataannex.splice(index, 1)
}


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

})




// 获取详情
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]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code
    idcard.value = data.person_idcard
    // people.value = data.person_name
    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 () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item: any) => item.uri)
    }
    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}` : ""
    console.log(formData)
    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>
