package com.framework.mvvm.ui.screen

import android.content.Intent
import android.os.Bundle
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.runtime.*
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.compose.ui.unit.sp
import com.framework.mvvm.model.db.entity.ProjectEntity
import com.framework.mvvm.model.db.entity.UserEntity
import com.framework.mvvm.mv.AdmViewModel
import com.sybotan.android.demo.activities.PocActivity
import org.kodein.di.compose.localDI
import org.kodein.di.instance

@Composable
fun ProjectScreen() {
    val di = localDI()
    val vm by di.instance<AdmViewModel>()
    val context = LocalContext.current

    var projects by remember { mutableStateOf<List<ProjectEntity>>(emptyList()) }
    var user by remember { mutableStateOf<UserEntity?>(null) }

    Column(modifier = Modifier.fillMaxWidth()) {
        Row(
            modifier = Modifier
                .height(56.dp)
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "项目",
                fontSize = 22.sp,
            )
        }

        LaunchedEffect(key1 = "project") {
            projects = vm.getProjects()
            user = vm.getCurrentUser()
        }

        LazyColumn {
            items(items = projects) { project ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(64.dp)
                        .padding(top = 8.dp, bottom = 8.dp)
                        .background(Color.Gray)
                        .clickable {
                            val intent = Intent(context, PocActivity::class.java)
                            val bundle = Bundle()
                            bundle.putString("userId", user?.username ?: "")
                            bundle.putString("groupCode", project.groupCode)
                            bundle.putString("projectId", project.projectId)
                            intent.putExtra("project", bundle)
                            context.startActivity(intent)
                        },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(text = project.projectName, fontSize = 18.sp)
                }
            }
        }

    }
}