package com.za.ui.servicing.in_servicing_setting

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Call
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
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.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.za.base.theme.bgColor
import com.za.base.theme.black5
import com.za.base.theme.headBgColor
import com.za.base.view.ChoiceMapDialog
import com.za.base.view.CommonDialog
import com.za.bean.db.order.OrderInfo
import com.za.ext.callPhone
import com.za.ext.convertToFlowName
import com.za.ext.copy
import com.za.servicing.R

@Composable
fun OrderDetailItemScreen(orderInfo : OrderInfo?) {
	Column(modifier = Modifier
		.fillMaxSize()
		.verticalScroll(state = rememberScrollState())
		.background(color = bgColor)
		.padding(5.dp)) {
		OrderDetailBaseInformationView(orderInfo = orderInfo)
		OrderDetailServiceInformationView(orderInfo = orderInfo)
		OrderDetailTime(orderInfo = orderInfo)
	}
}

@Composable
private fun OrderDetailBaseInformationView(orderInfo : OrderInfo?) {
	val context = LocalContext.current
	val titleSize = 12.sp
	val titleColor = Color(0xFF7A7A7A)
	val contentColor = Color.Black

	var showCallPhoneDialog by remember { mutableStateOf(false) }
	if (showCallPhoneDialog) {
		CommonDialog(title = "联系客户",
			message = orderInfo?.customerName,
			confirmText = "拨打电话",
			cancelText = "取消",
			confirm = {
				showCallPhoneDialog = false
				context.callPhone(orderInfo?.customerPhone)
			},
			cancel = {
				showCallPhoneDialog = false
			},
			dismiss = {
				showCallPhoneDialog = false
			})
	}

	Column(modifier = Modifier
		.fillMaxWidth()
		.background(color = Color.White, shape = RoundedCornerShape(4.dp))
		.padding(10.dp),
		verticalArrangement = Arrangement.Top) {
		Box(contentAlignment = Alignment.CenterStart) {
			Text(text = "基本信息",
				color = Color.Black,
				fontWeight = FontWeight.Medium,
				fontSize = 16.sp)
		}
		HorizontalDivider(color = black5, modifier = Modifier.padding(vertical = 10.dp))

		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "合同名称",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.orderSource}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "结算类型",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.settleTypeStr}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "工单编号",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.taskCode}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
			Spacer(modifier = Modifier.width(5.dp))
			AsyncImage(model = R.drawable.sv_copy,
				contentDescription = "",
				modifier = Modifier
					.size(15.dp)
					.clickable {
						orderInfo?.taskCode?.copy(context = context)
					})
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "客户姓名",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.customerName}",
				color = headBgColor,
				textDecoration = TextDecoration.Underline,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.clickable {
					showCallPhoneDialog = true
				})
			Icon(imageVector = Icons.Default.Call,
				contentDescription = "",
				tint = headBgColor,
				modifier = Modifier.size(10.dp))
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "客户车牌号",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.carNo}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
			Spacer(modifier = Modifier.width(5.dp))
			AsyncImage(model = R.drawable.sv_copy,
				contentDescription = "",
				modifier = Modifier.size(15.dp))
		}


		if (! orderInfo?.carModel.isNullOrBlank()) {
			Spacer(modifier = Modifier.height(10.dp))
			Row(modifier = Modifier.fillMaxWidth(),
				verticalAlignment = Alignment.CenterVertically) {
				Text(text = "车型",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.carModel}",
					color = contentColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium)
			}
		}

		//拖车责任险
		if (! orderInfo?.policyNo.isNullOrBlank()) {
			Spacer(modifier = Modifier.height(10.dp))
			Row(modifier = Modifier.fillMaxWidth(),
				verticalAlignment = Alignment.CenterVertically) {
				Text(text = "拖车责任险",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.policyNo}",
					color = contentColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium)
			}
		}
	}

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


@Composable
private fun OrderDetailServiceInformationView(orderInfo : OrderInfo?) {
	val titleSize = 12.sp
	val titleColor = Color(0xFF7A7A7A)
	val contentColor = Color.Black

	// 1 事发地  2 目的地
	var showChoiceMapDialog by remember { mutableStateOf<Int?>(null) }
	if (showChoiceMapDialog != null) {
		ChoiceMapDialog(
			dismiss = { showChoiceMapDialog = null },
			lat = orderInfo?.lat.takeIf { showChoiceMapDialog == 1 } ?: orderInfo?.distLat,
			lng = orderInfo?.lng.takeIf { showChoiceMapDialog == 1 } ?: orderInfo?.distLng,
			address = orderInfo?.address.takeIf { showChoiceMapDialog == 1 }
				?: orderInfo?.distAddress,
		)
	}

	Column(modifier = Modifier
		.fillMaxWidth()
		.background(color = Color.White, shape = RoundedCornerShape(4.dp))
		.padding(10.dp),
		verticalArrangement = Arrangement.Top) {
		Box(contentAlignment = Alignment.CenterStart) {
			Text(text = "服务信息",
				color = Color.Black,
				fontWeight = FontWeight.Medium,
				fontSize = 16.sp)
		}
		HorizontalDivider(color = black5, modifier = Modifier.padding(vertical = 10.dp))

		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "服务类型",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.serviceTypeName}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "车辆位于",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.addressProperty}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}
		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) {
			Text(text = "事发地",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.address}",
				color = headBgColor,
				textDecoration = TextDecoration.Underline,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.clickable { showChoiceMapDialog = 1 })
		}
		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) {
			Text(text = "事发地备注",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.addressRemark}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}

		if (! orderInfo?.distAddress.isNullOrBlank()) {
			Spacer(modifier = Modifier.height(10.dp))
			Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) {
				Text(text = "目的地",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.distAddress}",
					color = headBgColor,
					textDecoration = TextDecoration.Underline,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.clickable { showChoiceMapDialog = 2 })
			}
			Spacer(modifier = Modifier.height(10.dp))
			Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) {
				Text(text = "目的地备注",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.distAddressRemark}",
					color = contentColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium)
			}
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "订单状态",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = "${orderInfo?.convertToFlowName()}",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}

		Spacer(modifier = Modifier.height(10.dp))
		Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
			Text(text = "备注",
				color = titleColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium,
				modifier = Modifier.width(75.dp))
			Spacer(modifier = Modifier.width(5.dp))
			Text(text = orderInfo?.importantTip ?: "",
				color = contentColor,
				fontSize = titleSize,
				fontWeight = FontWeight.Medium)
		}
	}

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

}

@Composable
fun OrderDetailTime(orderInfo : OrderInfo?) {
	val titleSize = 12.sp
	val titleColor = Color(0xFF7A7A7A)
	val contentColor = Color.Black

	Column(modifier = Modifier
		.fillMaxWidth()
		.background(color = Color.White, shape = RoundedCornerShape(4.dp))
		.padding(10.dp),
		verticalArrangement = Arrangement.Top) {
		Box(contentAlignment = Alignment.CenterStart) {
			Text(text = "订单时间",
				color = Color.Black,
				fontWeight = FontWeight.Medium,
				fontSize = 16.sp)
		}
		HorizontalDivider(color = black5, modifier = Modifier.padding(vertical = 10.dp))

		if (! orderInfo?.acceptTime.isNullOrBlank()) {
			Row(modifier = Modifier.fillMaxWidth(),
				verticalAlignment = Alignment.CenterVertically) {
				Text(text = "接单时间",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.acceptTime}",
					color = contentColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium)
			}
		}

		if (! orderInfo?.arriveTime.isNullOrBlank()) {
			Spacer(modifier = Modifier.height(10.dp))
			Row(modifier = Modifier.fillMaxWidth(),
				verticalAlignment = Alignment.CenterVertically) {
				Text(text = "到达事发地时间",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.arriveTime}",
					color = contentColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium)
			}
		}

		if (! orderInfo?.arriveDestTime.isNullOrBlank()) {
			Spacer(modifier = Modifier.height(10.dp))
			Row(modifier = Modifier.fillMaxWidth(),
				verticalAlignment = Alignment.CenterVertically) {
				Text(text = "到达目的地时间",
					color = titleColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium,
					modifier = Modifier.width(75.dp))
				Spacer(modifier = Modifier.width(5.dp))
				Text(text = "${orderInfo?.arriveDestTime}",
					color = contentColor,
					fontSize = titleSize,
					fontWeight = FontWeight.Medium)
			}
		}

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