package com.za.ui.servicing.order_confirm

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.za.base.view.CommonButton
import com.za.base.view.HeadView
import com.za.bean.db.order.PhotoTemplateInfo
import com.za.ext.finish
import com.za.ext.goStatusPage
import com.za.ui.servicing.InServicingPhotoView

@Composable
fun ChangeBatteryScreen(vm: ChangeBatteryVm = viewModel()) {
    val uiState = vm.uiState.collectAsStateWithLifecycle()
    val context = LocalContext.current
    LaunchedEffect(key1 = Unit) {
        vm.dispatch(ChangeBatteryVm.Action.Init)
    }

    if (uiState.value.goNextPage != null) {
        uiState.value.orderInfo?.goStatusPage(context)
    }

    Scaffold(topBar = {
        HeadView(title = "更换电瓶", onBack = { context.finish() })
    }, bottomBar = {
        CommonButton(text = "提交照片") {
            vm.dispatch(ChangeBatteryVm.Action.Upload)
        }
    }) { it ->
        LazyColumn(modifier = Modifier
            .fillMaxSize()
            .padding(it),
            contentPadding = PaddingValues(10.dp)) {

            item {
                Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
                    .background(color = Color.White, shape = RoundedCornerShape(4.dp))
                    .padding(10.dp)) {
                    Text(text = "是否更换电瓶:", color = Color.Black)
                    Spacer(modifier = Modifier.width(10.dp))
                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Text(text = "更换", color = Color.Black)
                        Checkbox(checked = uiState.value.eleWorkOrderBean?.changeBattery == true, onCheckedChange = {
                            val eleWorkOrderBean = uiState.value.eleWorkOrderBean
                            vm.dispatch(ChangeBatteryVm.Action.UpdateState(uiState.value.copy(eleWorkOrderBean = eleWorkOrderBean?.copy(changeBattery = true))))
                        }, colors = CheckboxDefaults.colors()
                            .copy(uncheckedBoxColor = Color.Gray,
                                checkedBorderColor = Color.Transparent,
                                uncheckedBorderColor = Color.Transparent,
                                checkedBoxColor = Color.Red))
                    }

                    Spacer(modifier = Modifier.width(10.dp))

                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Text(text = "不更换", color = Color.Black)
                        Checkbox(checked = uiState.value.eleWorkOrderBean?.changeBattery == false,
                            onCheckedChange = {
                                val eleWorkOrderBean = uiState.value.eleWorkOrderBean
                                vm.dispatch(ChangeBatteryVm.Action.UpdateState(uiState.value.copy(eleWorkOrderBean = eleWorkOrderBean?.copy(changeBattery = false))))
                            }, colors = CheckboxDefaults.colors()
                                .copy(uncheckedBoxColor = Color.Gray,
                                    checkedBorderColor = Color.Transparent,
                                    uncheckedBorderColor = Color.Transparent,
                                    checkedBoxColor = Color.Red))
                    }
                }
            }

            itemsIndexed(items = uiState.value.changeBatteryPhoto
                ?: arrayListOf(), key = { _, item -> item.hashCode() }) { index: Int, item: PhotoTemplateInfo ->
                InServicingPhotoView(photoTemplateInfo = item, index = index, success = {
                    vm.dispatch(ChangeBatteryVm.Action.UpdatePhotoTemplate(it))
                })
                Spacer(modifier = Modifier.height(10.dp))
            }
        }
    }
}
