package com.za.ui.servicing.in_servicing_setting

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
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.size
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.za.base.view.CommonDialog
import com.za.base.view.EmptyView
import com.za.base.view.HeadView
import com.za.bean.db.order.OrderInfo
import com.za.ext.finish

@Composable
fun OrderOnSitePhotoScreen(orderInfo: OrderInfo?) {
    val context = LocalContext.current
    val showPreviewPhotoDialog = remember { mutableStateOf<String?>("") }
    if (!showPreviewPhotoDialog.value.isNullOrBlank()) {
        CommonDialog(title = "预览",
            confirm = { showPreviewPhotoDialog.value = null },
            cancelText = "取消",
            confirmText = "确定",
            cancelEnable = true,
            cancel = {
                showPreviewPhotoDialog.value = null
            },
            dismiss = {
                showPreviewPhotoDialog.value = null
            }, content = {
                Box(modifier = Modifier
                    .fillMaxWidth()
                    .height(300.dp), contentAlignment = Alignment.Center) {
                    AsyncImage(model = showPreviewPhotoDialog.value,
                        contentDescription = "",
                        modifier = Modifier.fillMaxSize(),
                        contentScale = ContentScale.FillWidth)
                }
            })
    }

    Scaffold(topBar = { HeadView(title = "现场环境照片", onBack = { context.finish() }) }) { it ->
        if (orderInfo?.customerReportImgs.isNullOrBlank() || orderInfo?.customerReportImgs?.split(",").isNullOrEmpty()) {
            EmptyView()
        } else {
            LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(2),
                contentPadding = PaddingValues(5.dp),
                modifier = Modifier
                    .fillMaxSize()
                    .padding(it)) {
                items(items = orderInfo?.customerReportImgs?.split(",") ?: arrayListOf()) {
                    AsyncImage(model = it, contentDescription = "", modifier = Modifier
                        .size(180.dp, 110.dp)
                        .clickable {
                            showPreviewPhotoDialog.value = it
                        }
                        .padding(3.dp), contentScale = ContentScale.FillHeight)
                }
            }
        }
    }
}