package com.iscs.bozzys.ui.pages.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.iscs.bozzys.R
import com.iscs.bozzys.api.Job
import com.iscs.bozzys.ui.pages.detail.job.openPageDetailJob
import com.iscs.bozzys.ui.theme.Text
import com.iscs.bozzys.utils.DateUtil.getShowDateOrTime

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun JobListItem(job: Job) {
    val ctx = LocalContext.current
    // 获取等级名称和颜色
    val levelInfo = job.getLevelNameAndColor()
    CardBox(
        modifier = Modifier
            .padding(horizontal = 16.dp, vertical = 5.dp)
            .fillMaxWidth()
            .heightIn(max = 200.dp),
        shadowBlur = 6.dp
    ) {
        Column(
            Modifier
                .fillMaxWidth()
                .clickable(onClick = { ctx.openPageDetailJob(job.id) })
                .padding(16.dp)
        ) {
            Row {
                Text(
                    job.name,
                    fontSize = 15.sp,
                    fontWeight = FontWeight.Medium,
                    color = Text,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier
                        .padding(end = 10.dp)
                        .weight(1f)
                )
                if (job.urgencyLevel != "0") Text(
                    levelInfo.first, modifier = Modifier
                        .height(24.dp)
                        .clip(RoundedCornerShape(12.dp))
                        .background(levelInfo.second)
                        .padding(horizontal = 10.dp),
                    fontSize = 12.sp,
                    lineHeight = 24.sp,
                    color = Color.White
                )
            }
            FlowRow(
                Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth()
            ) {
                // 当前任务号
                Row(Modifier.padding(end = 12.dp), verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.number), contentDescription = null, modifier = Modifier
                            .padding(end = 5.dp)
                            .size(13.dp),
                        tint = Color(0xFF666666)
                    )
                    Text(job.orderNo, fontSize = 13.sp, color = Color(0xFF666666))
                }
                // 负责人
                if (job.initiatorName.isNotEmpty()) Row(Modifier.padding(end = 12.dp), verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.user), contentDescription = null, modifier = Modifier
                            .padding(end = 5.dp)
                            .size(13.dp),
                        tint = Color(0xFF666666)
                    )
                    Text("作业发起人：${job.initiatorName}", fontSize = 13.sp, color = Color(0xFF666666))
                }
                // 岗位区间，暂时没有字段匹配，先隐藏
                if (false) Row(Modifier.padding(end = 12.dp), verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.location), contentDescription = null, modifier = Modifier
                            .padding(end = 5.dp)
                            .size(13.dp),
                        tint = Color(0xFF1E90FF)
                    )
                    Text("车间A区", fontSize = 13.sp, color = Color(0xFF666666))
                }
                // Text("作业内容：${task.currentNodeName ?: "--"}", fontSize = 13.sp, color = Color(0xFF666666))
            }
            Row(
                Modifier
                    .padding(top = 5.dp)
                    .fillMaxWidth()
                    .height(20.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text("当前任务：", fontSize = 13.sp, lineHeight = 13.sp, color = Text)
                Text(
                    job.currentNodeName ?: "--",
                    modifier = Modifier
                        .fillMaxHeight()
                        .clip(RoundedCornerShape(4.dp))
                        .background(Color(0xFF1E90FF))
                        .padding(horizontal = 6.dp),
                    fontSize = 12.sp,
                    lineHeight = 20.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White,
                    textAlign = TextAlign.Center
                )
                Spacer(modifier = Modifier.weight(1f))
                // 显示创建日期
                Text(job.createTime.getShowDateOrTime(), fontSize = 12.sp, color = Text.copy(alpha = 0.6f))
            }
        }
    }
}

