package com.iscs.bozzys.ui.pages.create.job

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
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.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.iscs.bozzys.api.FormOption
import com.iscs.bozzys.ui.common.PageBase
import com.iscs.bozzys.ui.common.Title
import com.iscs.bozzys.ui.pages.compose.CardBox
import com.iscs.bozzys.ui.pages.compose.FormDateSelect
import com.iscs.bozzys.ui.pages.compose.FormRadio
import com.iscs.bozzys.ui.pages.vm.VMPushJob
import com.iscs.bozzys.ui.theme.Text

/**
 * 打开发布作业
 */
fun Context.openPagePushJob(id: Int) {
    startActivity(Intent(this, PagePushJob::class.java).apply {
        putExtra("id", id)
    })
}

/**
 * 作业发布操作
 */
class PagePushJob : PageBase() {

    @Composable
    override fun GetViews(pv: PaddingValues) {
        val vm: VMPushJob = viewModel()
        val id = intent.getIntExtra("id", 0)
        val state by vm.state.collectAsState()
        LaunchedEffect(Unit) {
            vm.toast.initToast()
            vm.loading.initLoading()
        }
        Column(Modifier.fillMaxSize()) {
            Title(pv, "发布作业")
            CardBox(modifier = Modifier.padding(16.dp)) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                ) {
                    Text("发布作业设置", fontSize = 16.sp, color = Text, fontWeight = FontWeight.Bold)
                    Text("请配置作业的执行时间并最终发布", fontSize = 14.sp, color = Text)
                    FormRadio(
                        label = "发布方式",
                        value = listOf("0"),
                        options = listOf(FormOption("立即发布", "0"), FormOption("定时发布", "1")),
                        onSelectChange = {
                            vm.updatePushType(it.getOrNull(0) ?: "0")
                        },
                        required = true
                    )
                    if (state.pushType == "1") FormDateSelect(
                        label = "发布时间",
                        value = listOf(),
                        onSelectChange = {},
                        required = true,
                        placeholder = listOf("请选择发布时间")
                    )
                }
            }
            Spacer(modifier = Modifier.weight(1f))
            Button(
                onClick = {
                    vm.onPush(id) {
                        destroyDelay(1000)
                    }
                }, modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .padding(bottom = pv.calculateBottomPadding() + 10.dp)
                    .fillMaxWidth()
                    .height(46.dp),
                shape = RoundedCornerShape(6.dp)
            ) {
                Text("发布作业", fontSize = 16.sp, fontWeight = FontWeight.Bold)
            }
        }
    }
}