package com.yzx.ebrand.model

import android.os.Parcelable
import com.yzx.ebrand.utils.durationStr
import kotlinx.android.parcel.Parcelize

@Parcelize
data class StuLeave(
    val activities: MutableList<LeaveActivity> = mutableListOf(),
    val add_time: Int = 0,
    val add_user_id: Int = 0,
    val add_user_name: String = "",
    val add_user_type: Int = 0,
    val approve_time: Int = 0,
    val can_reviewed: Int = 0,
    val class_id: Int = 0,
    val class_name: String = "",
    val copy_recipients: String = "",
    val end_time: Int = 0,
    val entry_flag: Int = 0,
    val face_identity: String = "",
    val face_key: String = "",
    val ftime: Int = 0,
    val grade_id: Int = 0,
    val grade_name: String = "",
    val headteacher_id: Int = 0,
    val id: Int = 0,
    val is_cancel_leave: Int = 0,
    val leave_request_reason: String = "",
    val leave_request_type: Int = 0,
    val leave_request_type_name: String = "",
    val leave_student_id: Int = 0,
    val leave_student_name: String = "",
    val relation_id: Int = 0,
    val school_id: Int = 0,
    val school_name: String = "",
    val start_time: Int = 0,
    val status: Int = 0,
    val todo_list_id: Int = 0,
    val todo_status: Int = 0
) : Parcelable {

    fun getStep(): MutableList<LeaveActivity> {
        val logList = mutableListOf<LeaveActivity>()
        for (item in activities) {
            if (item.child.isNotEmpty()) {
                for (it in item.child) {
                    it.activity_name = when (it.approve_user_type) {
                        0 -> "领导审批"
                        1 -> "自选"
                        2 -> if (it.role_type == 3) {
                            "班主任审批"
                        } else {
                            "任课老师审批"
                        }
                        3 -> "专业部领导审批"
                        4 -> "值班领导审批"
                        else -> "领导审批"
                    }
                }
                val approvedList = item.child.filter {
                    return@filter it.approval_status > 1
                }
                if (approvedList.isNotEmpty()) {
                    if (item.approve_way == 2) {
                        logList.addAll(approvedList)
                    } else {
                        logList.addAll(item.child)
                    }
                } else {
                    item.activity_name = when (item.approve_user_type) {
                        0 -> "领导审批"
                        1 -> "自选"
                        2 -> if (item.role_type == 3) {
                            "班主任审批"
                        } else {
                            "任课老师审批"
                        }
                        3 -> "专业部领导审批"
                        4 -> "值班领导审批"
                        else -> "领导审批"
                    }
                    if (item.child.size > 1) {
                        item.activity_name += when (item.approve_way) {
                            0 -> "（依次审批）"
                            1 -> "（会签）"
                            2 -> "（或签）"
                            else -> ""
                        }
                    }
                    item.add_user_name = item.child.map {
                        return@map it.add_user_name
                    }.joinToString(separator = "、")

                    item.approval_status = 1
                    item.is_approve = 1
                    logList.add(item)
                }
            } else if (item.is_approve == 0) {  //提交人
                if (item.activity_name.contains("并直接通过") || item.is_auto_pass == 1) {
                    item.activity_name = "提交申请（直接通过）"
                } else {
                    item.activity_name = "提交申请"
                }
                if (item.add_user_type == 0) {
                    item.add_user_type_name = "老师"
                } else if (item.add_user_type == 1) {
                    item.add_user_type_name = "学生家长"
                }
                logList.add(item)
            } else if (item.is_approve == 2) { //学生返校销假
                if (item.vacation_type == 2) { //销假/返校类型（1自动销假 2老师操作销假）
                    val remark = item.approval_comments
                    item.approval_comments = "人工销假，销假操作人：${item.add_user_name}。"
                    if (item.add_time > end_time) { //超时
                        item.approval_comments += "超出请假时长：${(item.add_time - end_time).durationStr()}。"
                        item.color = "#ff4040"
                    }
                    if (remark.isNotEmpty()) {
                        item.approval_comments += "备注：${remark}"
                    }
                } else if (item.vacation_type == 1) {
                    item.approval_comments = "刷脸进校，自动销假。"
                    if (item.add_time > end_time) { //超时
                        item.approval_comments += "超出请假时长：${(item.add_time - end_time).durationStr()}。"
                        item.color = "#ff4040"
                    }
                }else if(item.vacation_type == 3){
                    item.approval_comments = "系统自动销假。"
                }
                logList.add(item)
            }
        }

        return logList
    }
}

@Parcelize
data class LeaveID(var id: Int,var type:Int) : Parcelable
@Parcelize
data class LeaveActivity(
    var activity_name: String = "",
    var add_time: Int = 0,
    var add_user_id: Int = 0,
    var add_user_name: String = "",
    var add_user_type: Int = 0,
    var approval_comments: String = "",
    var approval_status: Int = 0,
    var approve_node: Int = 0,
    var approve_sort: Int = 0,
    var approve_user_type: Int = 0,
    var approve_way: Int = 0,
    var child: MutableList<LeaveActivity> = mutableListOf(),
    var id: Int = 0,
    var is_approve: Int = 0,
    var related_id: Int = 0,
    var relation_id: Int = 0,
    var report_end_date: Int = 0,
    var report_start_date: Int = 0,
    var role_type: Int = 0,
    var school_name: String = "",
    var sign_identity: String = "",
    var sign_key: String = "",
    var status: Int = 0,
    var sub_type: Int = 0,
    var type: Int = 0,
    var unit_id: Int = 0,
    var unit_name: String = "",
    var add_user_type_name: String = "",
    var is_auto_pass: Int = 0,
    var vacation_type: Int = 0,
    var color: String = "",
    val face_infos: MutableList<FaceInfo> = mutableListOf()
) : Parcelable {
    fun getInImg(): FaceInfo? {
        val imgList = face_infos.filter { it.isImg() }
        if (imgList.isNotEmpty()) {
            return imgList[0]
        }
        return null
    }
}
@Parcelize
data class FaceInfo(
    var attachment_name: String = "",
    var attachment_id: String = "",
    var identity: String = ""
) : Parcelable {
    fun isImg(): Boolean {
        if (attachment_name.isNullOrBlank()) {
            return false
        }
        if (attachment_name.endsWith(".jpg", true) || attachment_name.endsWith(".png", true)) {
            return true
        }
        return false
    }
}
