package com.iscs.bozzys.ui.pages.detail.task

import android.Manifest
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.app.ActivityCompat
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.iscs.bozzys.R
import com.iscs.bozzys.api.Task
import com.iscs.bozzys.api.Ticket
import com.iscs.bozzys.ui.common.PageBase
import com.iscs.bozzys.ui.common.Title
import com.iscs.bozzys.ui.pages.compose.CardBox
import com.iscs.bozzys.ui.pages.compose.FormBox
import com.iscs.bozzys.ui.pages.vm.VMDetailTask
import com.iscs.bozzys.ui.theme.Text
import com.iscs.bozzys.utils.LogUtil
import com.iscs.bozzys.utils.ble.BleFrameExt
import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEGetPowerCMD
import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEGetStatusCMD
import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEGetTicketInfoCMD
import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLEPowerOffCMD
import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLESwitchRunModeCMD
import com.iscs.bozzys.utils.ble.BleFrameExt.buildBLETicketDataCMDList
import com.iscs.bozzys.utils.ble.BleFrameExt.getPower
import com.iscs.bozzys.utils.ble.BleFrameExt.getRunMode
import com.iscs.bozzys.utils.ble.BleFrameExt.getSendTicketResult
import com.iscs.bozzys.utils.ble.BleFrameExt.getSwitchRunModeResult
import com.iscs.bozzys.utils.ble.BleFrameExt.getTicketPackageInfo
import com.iscs.bozzys.utils.ble.BleFrameExt.getToken
import com.iscs.bozzys.utils.ble.BleManager
import com.iscs.bozzys.utils.ble.BleRunMode
import com.iscs.bozzys.utils.ble.BleTicketDataPackage
import com.iscs.bozzys.utils.byteArrayToHexString
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json

/**
 * 打开任务详情页面
 */
fun Context.openPageDetailTask(task: Task) {
    startActivity(Intent(this, PageDetailTask::class.java).apply {
        putExtra("task", task)
    })
}

/**
 * 任务详情页面
 */
class PageDetailTask : PageBase() {

    // 页面携带数据对象
    private lateinit var task: Task

    /**
     * 发送作业票到设备
     */
    private suspend fun sendTicket2Key(application: Application, vm: VMDetailTask, mac: String, step: Int) {
        vm.updateTicketStatus(status = "正在搜索钥匙...")
        // 测试蓝牙扫描
        val bm = BleManager(application, mac = mac)
        val result = bm.connect()
        if (result.connected) {
            if (step == 0 || step == 2) vm.updateTicketStatus(status = "正在下发任务...")
            if (step == 1 || step == 3) vm.updateTicketStatus(status = "正在读取任务...")
            // 获取设备token
            val token = bm.writeByResponse(BleFrameExt.buildBLEGetTokenCMD()).getToken()
            LogUtil.i("xiaoming $mac", "获取设备token ${token.byteArrayToHexString()}")
            // 获取设备电量
            val power = bm.writeByResponse(token.buildBLEGetPowerCMD()).getPower()
            LogUtil.i("xiaoming $mac", "当前设备电量：$power")
            if (power < 0) {
                vm.updateTicketStatus(status = "连接失败，请稍后重试")
                vm.updateTicketStatus(status = "", delay = 2000)
                bm.disconnect()
                return
            }
            // 获取当前设备运行模式
            val runMode = bm.writeByResponse(token.buildBLEGetStatusCMD()).getRunMode()
            LogUtil.i("xiaoming $mac", "当前工作模式：$runMode")
            if (step == 0 || step == 2) {
                // 下发作业票
                val tickets = token.buildBLETicketDataCMDList(vm.createTicketJson())
                var ticketSendOk = true
                tickets.forEach {
                    val ticket = bm.writeByResponse(it).getSendTicketResult()
                    if (ticket != 0) {
                        ticketSendOk = false
                        return@forEach
                    }
                }
                LogUtil.i("xiaoming $mac", "下发作业票：$ticketSendOk")
                // 作业票下发成功，修改设备运行模式为工作模式
                val switch = bm.writeByResponse(token.buildBLESwitchRunModeCMD(BleRunMode.WORK))
                    .getSwitchRunModeResult()
                bm.writeByResponse(token.buildBLEPowerOffCMD())
                LogUtil.i("xiaoming $mac", "切换工作模式：$switch")
                if (step == 0) {
                    vm.updateTicketStatus(1, "任务下发成功")
                    vm.updateTicketStatus(1, "", 2000)
                } else {
                    vm.updateTicketStatus(3, "任务下发成功")
                    vm.updateTicketStatus(3, "", 2000)
                }
            } else {
                // 读取作业票信息
                val pkgList = ArrayList<BleTicketDataPackage>()
                val ticketInfo = bm.writeByResponse(token.buildBLEGetTicketInfoCMD()).getTicketPackageInfo()
                pkgList.add(ticketInfo)
                LogUtil.i("xiaoming $mac", "读取作业票：首包信息：$ticketInfo")
                // 校验是否有子包，如果有，继续读取子包数据
                for (idx in 1 until ticketInfo.pkgTotal) {
                    val ticketSubPackageInfo =
                        bm.writeByResponse(token.buildBLEGetTicketInfoCMD(idx, ticketInfo.pkgTotal))
                            .getTicketPackageInfo()
                    pkgList.add(ticketSubPackageInfo)
                    LogUtil.i("xiaoming $mac", "读取作业票：子包信息：$ticketSubPackageInfo")
                }
                var datas = byteArrayOf()
                pkgList.forEach { datas += it.pkgData }
                val ticketJson = String(datas)
                LogUtil.i("xiaoming $mac", "读取作业票：${ticketJson}")
                bm.writeByResponse(token.buildBLESwitchRunModeCMD(BleRunMode.STBY)).getSwitchRunModeResult()
                bm.writeByResponse(token.buildBLEPowerOffCMD())
                val json = Json { ignoreUnknownKeys = true }
                val ticket = json.decodeFromString<Ticket>(ticketJson)
                vm.updateTicketStatus(status = "任务读取完成")
                vm.updateTicketStatus(status = "", delay = 2000)
                vm.updateTicket(ticket)
            }
        } else {
            // 进行重连，这里可以封装尝试次数
            sendTicket2Key(application, vm, mac, step)
        }
        bm.disconnect()
    }


    /**
     * 获取页面携带的数据
     */
    private fun getPageData(): Boolean {
        task = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getSerializableExtra("task", Task::class.java) ?: return false
        } else {
            @Suppress("DEPRECATION")
            (intent.getSerializableExtra("task") as? Task) ?: return false
        }
        return true
    }

    @Composable
    override fun GetViews(pv: PaddingValues) {
        if (!getPageData()) return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN),
                1
            )
        }
        val vm: VMDetailTask = viewModel()
        LaunchedEffect(Unit) {
            vm.toast.initToast()
            vm.loading.initLoading()
            vm.getTaskFormInfo(task)
        }
        val state by vm.state.collectAsState()
        Column(Modifier.fillMaxSize()) {
            Title(pv, task.name)
            Column(
                modifier = Modifier
                    .weight(1f)
                    // .verticalScroll(state = rememberScrollState())
                    .padding(16.dp)
            ) {
                TaskInfo(task)
                // 盲板表单数据存在即显示
                if (state.mbForms.isNotEmpty() && state.node.checkCanOptionByPhone()) MbForm(vm)
                // 表单数据存在即显示
                if (state.forms.isNotEmpty() && state.node.checkCanOptionByPhone()) TaskForm(vm)
                // 处理不能在手机上操作的
                if (!state.node.checkCanOptionByPhone()) TaskDevice(vm)
            }
            // 底部操作功能封装
            TaskOptions(pv, vm)
            if (state.step < 4) Button(
                {
                    lifecycleScope.launch {
                        sendTicket2Key(
                            this@PageDetailTask.application,
                            vm,
                            "CC:BA:97:21:72:0A",
                            state.step
                        )
                    }
                }, Modifier
                    .padding(bottom = (pv.calculateBottomPadding().value + 16).dp)
                    .fillMaxWidth(0.8f)
                    .height(40.dp)
                    .align(Alignment.CenterHorizontally)
            ) {
                Text(
                    when (state.step) {
                        1 -> "完成上锁"
                        2 -> "去解锁"
                        3 -> "完成解锁"
                        else -> "去上锁"
                    }, fontSize = 18.sp, fontWeight = FontWeight.Bold
                )

            }
        }
    }

    /**
     * 任务基础信息
     */
    @OptIn(ExperimentalLayoutApi::class)
    @Composable
    fun TaskInfo(task: Task) {
        CardBox(modifier = Modifier.fillMaxWidth()) {
            Column(Modifier.padding(10.dp)) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.tasks),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .size(16.dp),
                        tint = MaterialTheme.colorScheme.primary
                    )
                    Text(task.currentNodeName ?: "", fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Text)
                }
                FlowRow(
                    verticalArrangement = Arrangement.Center, modifier = Modifier
                        .padding(top = 6.dp)
                        .offset(x = (-3).dp)
                ) {
                    // 任务编号
                    Row(
                        modifier = Modifier
                            .padding(horizontal = 3.dp, vertical = 3.dp)
                            .clip(RoundedCornerShape(50))
                            .background(Color(0xFFFFF8E6))
                            .padding(horizontal = 10.dp, vertical = 2.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            painterResource(R.drawable.number),
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary,
                            modifier = Modifier
                                .padding(end = 3.dp)
                                .size(14.dp)
                        )
                        Text(task.orderNo, fontSize = 12.sp, color = Text)
                    }
                    // 岗位区间
                    if (false) Row(
                        modifier = Modifier
                            .padding(horizontal = 3.dp, vertical = 3.dp)
                            .clip(RoundedCornerShape(50))
                            .background(Color(0xFFFFF8E6))
                            .padding(horizontal = 10.dp, vertical = 2.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            painterResource(R.drawable.location),
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary,
                            modifier = Modifier
                                .padding(end = 3.dp)
                                .size(14.dp)
                        )
                        Text("车间A区", fontSize = 12.sp, color = Text)
                    }
                    // 负责人
                    if (!task.initiatorName.isNullOrEmpty()) Row(
                        modifier = Modifier
                            .padding(horizontal = 3.dp, vertical = 3.dp)
                            .clip(RoundedCornerShape(50))
                            .background(Color(0xFFFFF8E6))
                            .padding(horizontal = 10.dp, vertical = 2.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            painterResource(R.drawable.user),
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary,
                            modifier = Modifier
                                .padding(end = 3.dp)
                                .size(14.dp)
                        )
                        Text(task.initiatorName, fontSize = 12.sp, color = Text)
                    }
                    // 任务状态
                    Row(
                        modifier = Modifier
                            .padding(horizontal = 3.dp, vertical = 3.dp)
                            .clip(RoundedCornerShape(50))
                            .background(Color(0xFFFFF8E6))
                            .padding(horizontal = 10.dp, vertical = 2.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            painterResource(R.drawable.target),
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary,
                            modifier = Modifier
                                .padding(end = 3.dp)
                                .size(14.dp)
                        )
                        Text(task.getTaskStatusName(), fontSize = 12.sp, color = Text)
                    }
                    // 任务创建时间
                    if (false) Row(
                        modifier = Modifier
                            .padding(horizontal = 3.dp, vertical = 3.dp)
                            .clip(RoundedCornerShape(50))
                            .background(Color(0xFFFFF8E6))
                            .padding(horizontal = 10.dp, vertical = 2.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            painterResource(R.drawable.job_todo),
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary,
                            modifier = Modifier
                                .padding(end = 3.dp)
                                .size(14.dp)
                        )
                        Text("8小时", fontSize = 12.sp, color = Text)
                    }
                }
            }
        }
    }

    /**
     * 表单信息
     */
    @Composable
    fun TaskForm(vm: VMDetailTask) {
        val state by vm.state.collectAsState()
        CardBox(Modifier.padding(top = 10.dp)) {
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.jobs),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .size(16.dp),
                        tint = MaterialTheme.colorScheme.primary
                    )
                    Text("录入信息", fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Text)
                }
                FormBox(state.forms, {}, enabled = state.node.approvalStatus != "approved")
            }
        }
    }

    /**
     * 盲板表单
     */
    @Composable
    fun MbForm(vm: VMDetailTask) {
        val state by vm.state.collectAsState()
        CardBox(Modifier.padding(top = 10.dp)) {
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.jobs),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .size(16.dp),
                        tint = MaterialTheme.colorScheme.primary
                    )
                    Text("补充信息", fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Text)
                }
                FormBox(state.mbForms, {}, enabled = state.node.approvalStatus != "approved")
            }
        }
    }

    /**
     * 设备相关的操作
     */
    @Composable
    fun TaskDevice(vm: VMDetailTask) {
        val state by vm.state.collectAsState()
        CardBox(
            Modifier
                .padding(top = 10.dp)
                .fillMaxWidth()
        ) {
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(10.dp),
                horizontalAlignment = Alignment.Start,
            ) {
//                Icon(
//                    painter = painterResource(R.drawable.job_warning),
//                    contentDescription = null,
//                    modifier = Modifier.size(60.dp),
//                    tint = MaterialTheme.colorScheme.primary
//                )
//                Text("请前往锁控柜进行取锁，取钥匙操作", fontSize = 14.sp, color = Text)
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        painterResource(R.drawable.location),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .size(16.dp),
                        tint = MaterialTheme.colorScheme.primary
                    )
                    Text("隔离点位", fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Text)
                }
                Row(
                    Modifier
                        .padding(top = 10.dp)
                        .fillMaxWidth()
                ) {
                    Row(
                        Modifier
                            .weight(1f)
                            .height(80.dp)
                            .background(Color(0xFFFFF8E6), shape = RoundedCornerShape(16.dp)),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Image(
                            painterResource(R.mipmap.point),
                            contentDescription = null,
                            modifier = Modifier.scale(1.2f)
                        )
                        Column {
                            Text(
                                "E-29",
                                fontSize = 14.sp,
                                lineHeight = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = Text
                            )
                            Text(
                                state.points.first.equipRfidNo,
                                fontSize = 12.sp,
                                lineHeight = 14.sp,
                                color = Text.copy(alpha = 0.5f)
                            )
                            Text(
                                if (state.step <= 1) {
                                    if (state.points.first.closed == 1) "已隔离" else "待隔离"
                                } else {
                                    if (state.points.first.closed == 1) "已解除隔离" else "待解除隔离"
                                },
                                fontSize = 12.sp,
                                lineHeight = 14.sp,
                                color = if (state.step <= 1) {
                                    if (state.points.first.closed == 1) Color.Red else Color.Green
                                } else {
                                    if (state.points.first.closed == 1) Color.Green else Color.Red
                                }
                            )
                        }
                    }
                    Spacer(Modifier.width(10.dp))
                    Row(
                        Modifier
                            .weight(1f)
                            .height(80.dp)
                            .background(Color(0xFFFFF8E6), shape = RoundedCornerShape(16.dp)),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Image(
                            painterResource(R.mipmap.point),
                            contentDescription = null,
                            modifier = Modifier.scale(1.2f)
                        )
                        Column {
                            Text(
                                "E-39",
                                fontSize = 14.sp,
                                lineHeight = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = Text
                            )
                            Text(
                                state.points.second.equipRfidNo,
                                fontSize = 12.sp,
                                lineHeight = 14.sp,
                                color = Text.copy(alpha = 0.5f)
                            )
                            Text(
                                if (state.step <= 1) {
                                    if (state.points.second.closed == 1) "已隔离" else "待隔离"
                                } else {
                                    if (state.points.second.closed == 1) "已解除隔离" else "待解除隔离"
                                },
                                fontSize = 12.sp,
                                lineHeight = 14.sp,
                                color = if (state.step <= 1) {
                                    if (state.points.second.closed == 1) Color.Red else Color.Green
                                } else {
                                    if (state.points.second.closed == 1) Color.Green else Color.Red
                                }
                            )
                        }
                    }
                }
                // 获取设备列表
                if (state.step == 0 || state.step == 2) Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.padding(vertical = 16.dp)
                ) {
                    Icon(
                        painterResource(R.drawable.category),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .size(16.dp),
                        tint = MaterialTheme.colorScheme.primary
                    )
                    Text(
                        "${if (state.step <= 1) "上锁" else "解除"}隔离所需设备",
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Bold,
                        color = Text
                    )
                }
                if (state.step == 0 || state.step == 2) Row(Modifier.height(110.dp)) {
                    state.keys.forEach {
                        Column(
                            Modifier
                                .padding(end = 10.dp)
                                .fillMaxHeight()
                                .width(90.dp)
                                .background(Color(0xFFFFF8E6), shape = RoundedCornerShape(16.dp)),
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Image(
                                painterResource(R.mipmap.key),
                                contentDescription = null,
                                modifier = Modifier
                                    .size(80.dp)
                                    .scale(0.9f)
                            )
                            Text(
                                it,
                                fontSize = 14.sp,
                                lineHeight = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Text
                            )
                        }
                    }
                    // 当前使用中的挂锁
                    state.useLocks.forEach {
                        Column(
                            Modifier
                                .padding(end = 10.dp)
                                .fillMaxHeight()
                                .width(90.dp)
                                .background(Color(0xFFFFF8E6), shape = RoundedCornerShape(16.dp)),
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Image(
                                painterResource(R.mipmap.lock),
                                contentDescription = null,
                                modifier = Modifier
                                    .size(80.dp)
                                    .scale(0.9f)
                            )
                            Text(
                                it,
                                fontSize = 14.sp,
                                lineHeight = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Text
                            )
                        }
                    }
                }
            }
        }
    }

    /**
     * 底部多功能按钮
     */
    @Composable
    fun TaskOptions(pv: PaddingValues, vm: VMDetailTask) {
        val pb = pv.calculateBottomPadding()
        val state by vm.state.collectAsState()
        Column(
            Modifier
                .padding(bottom = if (pb.value <= 0) 10.dp else pb)
                .fillMaxWidth()
                .padding(top = 5.dp)
        ) {
            // 配置功能按键
            Row(
                modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .fillMaxWidth()
            ) {
                if (listOf(
                        "complete",
                        "inputInfo",
                        "confirm"
                    ).contains(state.node.type) || state.node.checkCanOptionByPhone()
                ) {
                    Button(
                        { destroy() }, modifier = Modifier
                            .padding(horizontal = 8.dp)
                            .weight(1f)
                            .height(50.dp)
                            .clip(RoundedCornerShape(12.dp)),
                        colors = ButtonDefaults.buttonColors(containerColor = Color(0xFFFFF0E6)),
                        shape = RoundedCornerShape(12.dp)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                painter = painterResource(R.drawable.job_close),
                                contentDescription = null,
                                modifier = Modifier
                                    .padding(end = 8.dp)
                                    .size(18.dp),
                                tint = Color(0xFFFF4D4F)
                            )
                            Text(
                                "取消",
                                fontSize = 16.sp,
                                lineHeight = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFFFF4D4F)
                            )
                        }
                    }
                    Button(
                        { vm.onSubmit { destroy() } },
                        enabled = state.node.approvalStatus != "approved",
                        modifier = Modifier
                            .padding(horizontal = 8.dp)
                            .weight(1f)
                            .height(50.dp)
                            .clip(RoundedCornerShape(12.dp)),
                        shape = RoundedCornerShape(12.dp)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                painter = painterResource(R.drawable.job_finish),
                                contentDescription = null,
                                modifier = Modifier
                                    .padding(end = 8.dp)
                                    .size(18.dp),
                                tint = Color.White
                            )
                            Text(
                                if (state.node.type == "complete") "完成" else "提交",
                                fontSize = 16.sp,
                                lineHeight = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color.White
                            )
                        }
                    }
                } else if (listOf("review").contains(state.node.type)) {
                    Button(
                        { vm.onSubmit("rejected") { destroy() } },
                        enabled = state.node.approvalStatus != "approved", // 审核通过禁用
                        modifier = Modifier
                            .padding(horizontal = 8.dp)
                            .weight(1f)
                            .height(50.dp)
                            .clip(RoundedCornerShape(12.dp)),
                        colors = ButtonDefaults.buttonColors(containerColor = Color(0xFFFFF0E6)),
                        shape = RoundedCornerShape(12.dp),
                        contentPadding = PaddingValues(horizontal = 5.dp)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                painter = painterResource(R.drawable.job_close),
                                contentDescription = null,
                                modifier = Modifier
                                    .padding(end = 8.dp)
                                    .size(18.dp),
                                tint = if (state.node.approvalStatus == "approved") Color.White else Color(0xFFFF4D4F)
                            )
                            Text(
                                "审核不通过",
                                fontSize = 16.sp,
                                lineHeight = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = if (state.node.approvalStatus == "approved") Color.White else Color(
                                    0xFFFF4D4F
                                )
                            )
                        }
                    }
                    Button(
                        { vm.onSubmit { destroy() } },
                        enabled = state.node.approvalStatus != "approved", // 审核通过禁用
                        modifier = Modifier
                            .padding(horizontal = 8.dp)
                            .weight(1f)
                            .height(50.dp)
                            .clip(RoundedCornerShape(12.dp)),
                        shape = RoundedCornerShape(12.dp)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                painter = painterResource(R.drawable.job_finish),
                                contentDescription = null,
                                modifier = Modifier
                                    .padding(end = 8.dp)
                                    .size(18.dp),
                                tint = Color.White
                            )
                            Text(
                                "审核通过",
                                fontSize = 16.sp,
                                lineHeight = 16.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color.White
                            )
                        }
                    }
                }
            }
        }
    }

}