package com.syaroful.agrilinkvocpro.presentation.screen.detail.component

import android.app.DatePickerDialog
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.core.utils.extention.toFormattedString
import java.util.Calendar


@Composable
fun DatePickerComponent(
    modifier: Modifier = Modifier,
    onDateSelected: (String) -> Unit,

    ) {
    val context = LocalContext.current
    val calendar = remember { Calendar.getInstance() }

    IconButton(
        onClick = {
            val datePickerDialog = DatePickerDialog(
                context,
                { _, year, month, dayOfMonth ->
                    calendar.set(year, month, dayOfMonth)
                    val selectedDate = calendar.time.toFormattedString()
                    onDateSelected(selectedDate)
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            )
            datePickerDialog.show()
        },
        modifier = modifier
    ) {
        Icon(
            painter = painterResource(id = R.drawable.ic_calendar),
            modifier = Modifier.size(24.dp),
            contentDescription = "Pilih Tanggal"
        )
    }
}
