package com.za.ui.servicing.in_servicing_setting

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import coil.compose.AsyncImage
import com.za.base.theme.black5
import com.za.base.theme.black65
import com.za.base.view.CommonButton
import com.za.bean.TaskNotesBean
import com.za.bean.db.order.OrderInfo
import com.za.servicing.R

@Composable
fun OrderRequirementsScreen(orderInfo : OrderInfo?) {
	Scaffold {
		Column(modifier = Modifier
			.fillMaxSize()
			.verticalScroll(state = rememberScrollState())
			.padding(it)
			.padding(horizontal = 16.dp)) {
			Spacer(modifier = Modifier.height(12.dp))
			OrderRequirementsItemView(title = "特殊提醒", content = orderInfo?.otherNotes)
			OrderRequirementsItemView(title = "收费标准", content = orderInfo?.feeStandard)
			if (! orderInfo?.carModel.isNullOrBlank()) {
				CarModeView(orderInfo = orderInfo)
			}
			OrderRequirementsItemView(title = "救援要求", content = orderInfo?.taskNotes)
			OrderRequirementsItemView(title = "客户要求", content = orderInfo?.customerNotes)
			Spacer(modifier = Modifier.height(16.dp))
		}
	}
}

@Composable
fun OrderRequirementsItemView(title : String?, content : String?) {
	Column(modifier = Modifier
		.fillMaxWidth()
		.padding(vertical = 5.dp)) {
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			AsyncImage(model = R.drawable.sv_warn_red,
				contentDescription = null,
				modifier = Modifier.size(16.dp))
			Spacer(modifier = Modifier.width(8.dp))
			Text(text = title ?: "",
				color = Color.Black,
				fontSize = 15.sp,
				fontWeight = FontWeight.Medium)
		}
		Spacer(modifier = Modifier.height(12.dp))
		Text(text = content?.replace("<br/>", "\n\n") ?: "无",
			fontSize = 14.sp,
			fontWeight = FontWeight.Normal,
			color = black65,
			lineHeight = 20.sp,
			modifier = Modifier.padding(start = 24.dp))
		Spacer(modifier = Modifier.height(12.dp))
		HorizontalDivider(color = black5, modifier = Modifier.fillMaxWidth())
	}
}

@Composable
fun CarModeView(orderInfo : OrderInfo?) {
	Column(modifier = Modifier
		.fillMaxWidth()
		.padding(vertical = 5.dp)) {
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			AsyncImage(model = R.drawable.sv_warn_red,
				contentDescription = null,
				modifier = Modifier.size(16.dp))
			Spacer(modifier = Modifier.width(8.dp))
			Text(text = "车型 ${orderInfo?.carModel}",
				color = Color.Black,
				fontSize = 15.sp,
				fontWeight = FontWeight.Medium)
			if (orderInfo?.linkToDocs == true) {
				Spacer(modifier = Modifier.width(12.dp))
				Text(text = "点击查看相关资料",
					color = Color(0xFFffa500),
					fontSize = 14.sp,
					fontWeight = FontWeight.Medium)
			}
		}
		Spacer(modifier = Modifier.height(12.dp))
		HorizontalDivider(color = black5, modifier = Modifier.fillMaxWidth())
	}
}

@Composable
fun OrderTaskNotesDialog(taskNotesBean : TaskNotesBean?,
	isShowChangeBattery : Boolean? = false,
	dismiss : () -> Unit,
	confirm : () -> Unit) {

	Dialog(onDismissRequest = { dismiss() },
		properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false)) {
		Box(contentAlignment = Alignment.BottomCenter,
			modifier = Modifier.background(color = Color.White,
				shape = RoundedCornerShape(13.dp))) {
			Column(modifier = Modifier
				.fillMaxWidth()
				.wrapContentHeight()
				.verticalScroll(state = rememberScrollState())
				.padding(horizontal = 10.dp),
				horizontalAlignment = Alignment.CenterHorizontally,
				verticalArrangement = Arrangement.Top) {
				Spacer(modifier = Modifier.height(20.dp))

				Row(modifier = Modifier
					.fillMaxWidth()
					.padding(horizontal = 10.dp)) {
					Text("合同名称",
						color = Color.Black,
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium)

					Spacer(Modifier.weight(1f))

					Text("平安保险",
						color = Color.Black,
						fontSize = 15.sp,
						fontWeight = FontWeight.Bold)
				}

				HorizontalDivider(color = black5, modifier = Modifier.padding(vertical = 10.dp))

				if (! taskNotesBean?.otherNotes.isNullOrBlank()) {
					OrderRequirementsItemView(title = "特殊提醒",
						content = taskNotesBean?.otherNotes)
				}

				if (! taskNotesBean?.feeStandard.isNullOrBlank()) {
					OrderRequirementsItemView(title = "收费标准",
						content = taskNotesBean?.feeStandard)
				}

				if (! taskNotesBean?.modelVinNo.isNullOrBlank()) {
					OrderRequirementsItemView(title = "车型", content = taskNotesBean?.modelVinNo)
				}

				if (! taskNotesBean?.taskNotes.isNullOrBlank()) {
					OrderRequirementsItemView(title = "救援要求",
						content = taskNotesBean?.taskNotes)
				}

				if (! taskNotesBean?.customerNotes.isNullOrBlank()) {
					OrderRequirementsItemView(title = "客户要求",
						content = taskNotesBean?.customerNotes)
				}

				if (isShowChangeBattery == true) {
					Spacer(modifier = Modifier.height(10.dp))
					Row(modifier = Modifier.padding(vertical = 10.dp, horizontal = 10.dp),
						verticalAlignment = Alignment.CenterVertically) {
						Icon(imageVector = Icons.Default.Warning,
							tint = Color.Red,
							contentDescription = "",
							modifier = Modifier.size(24.dp))
						Text("此订单不允许销售电瓶",
							color = Color.Red,
							fontSize = 14.sp,
							fontWeight = FontWeight.Medium,
							modifier = Modifier.padding(start = 8.dp))
					}
				}

				Spacer(modifier = Modifier.height(65.dp))
			}

			CommonButton(text = "我已阅读") { confirm() }
		}
	}
}
