package com.za.ui.servicing.in_servicing_setting

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
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.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.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
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 com.za.base.theme.headBgColor
import com.za.base.theme.white80
import com.za.base.view.CommonButton
import com.za.base.view.HeadView
import com.za.bean.db.order.OrderInfo
import com.za.ext.finish
import com.za.ui.servicing.order_give_up.OrderGiveUpActivity
import kotlinx.coroutines.launch

@Composable
fun OrderDetailScreen(orderInfo : OrderInfo?) {
	val context = LocalContext.current
	val titleList = listOf("订单详情", "案件要求")
	val pagerState = rememberPagerState(initialPage = 0, pageCount = { titleList.size })
	val scope = rememberCoroutineScope()
	Scaffold(topBar = {
		HeadView(title = "订单信息", onBack = { context.finish() })
	}, bottomBar = {
		CommonButton(text = "客户放弃") {
			OrderGiveUpActivity.goOrderGiveUpActivity(context,
				orderInfo = orderInfo,
				userOrderId = orderInfo?.userOrderId,
				giveUpType = 0)
		}
	}) {
		Column(modifier = Modifier
			.fillMaxSize()
			.padding(it)) {
			Row(modifier = Modifier
				.fillMaxWidth()
				.horizontalScroll(state = rememberScrollState())
				.height(30.dp)
				.background(color = headBgColor),
				verticalAlignment = Alignment.CenterVertically) {
				for (i in 0 until pagerState.pageCount) {
					if (pagerState.currentPage == i) {
						Column(modifier = Modifier.weight(1f),
							verticalArrangement = Arrangement.Center,
							horizontalAlignment = Alignment.CenterHorizontally) {
							Text(text = titleList[i], color = Color.White)
							HorizontalDivider(thickness = 0.dp,
								modifier = Modifier
									.width(25.dp)
									.height(3.dp)
									.background(color = Color.White,
										shape = RoundedCornerShape(8.dp)))
						}
					} else {
						Box(modifier = Modifier
							.weight(1f)
							.clickable {
								scope.launch {
									pagerState.animateScrollToPage(page = i)
								}
							}, contentAlignment = Alignment.Center) {
							Text(text = titleList[i], color = white80)
						}
					}
				}
			}

			HorizontalPager(state = pagerState,
				modifier = Modifier
					.fillMaxSize()
					.padding(10.dp),
				verticalAlignment = Alignment.CenterVertically) {
				when (pagerState.currentPage) {
					0 -> OrderDetailItemScreen(orderInfo = orderInfo)
					1 -> OrderRequirementsScreen(orderInfo = orderInfo)
					2 -> OrderSettleScreen(orderInfo = orderInfo)
					3 -> OrderEleScreen(orderInfo = orderInfo)
					4 -> OrderTriceScreen(orderInfo = orderInfo)
				}
			}


		}
	}
}