Imports System.Data.SqlClient

Public Class FrmPeriodoMes
    Private rutEmpresa As String

    Public Sub New(rutEmp As String)

        InitializeComponent()

        rutEmpresa = rutEmp
    End Sub

    Private Sub FrmPeriodoMes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        mes = 0
        periodo = 0

        CargarPeriodo()
        CargarMes()

        CmbPeriodo.SelectedValue = Year(Now)
        CmbMes.SelectedValue = Month(Now)

        CargarPeriodoMesParams()
    End Sub

    Private Sub CargarPeriodo()
        Dim dtPeriodo As New DataTable
        dtPeriodo.Columns.Add("cod")
        dtPeriodo.Columns.Add("descripcion")

        For periodo = 2023 To 2000 Step -1
            dtPeriodo.Rows.Add(periodo, periodo.ToString())
        Next

        With CmbPeriodo
            .DataSource = dtPeriodo
            .DisplayMember = "descripcion"
            .ValueMember = "cod"
        End With
    End Sub

    Private Sub CargarMes()
        Dim dtMes As New DataTable
        Dim conn As New Conexion(conexionLocal)

        Dim sql As String
        sql = "SELECT 1 AS cod, 'ENERO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 2 AS cod, 'FEBRERO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 3 AS cod, 'MARZO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 4 AS cod, 'ABRIL' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 5 AS cod, 'MAYO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 6 AS cod, 'JUNIO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 7 AS cod, 'JULIO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 8 AS cod, 'AGOSTO' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 9 AS cod, 'SEPTIEMBRE' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 10 AS cod, 'OCTUBRE' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 11 AS cod, 'NOVIEMBRE' AS descripcion "
        sql += "UNION ALL "
        sql += "SELECT 12 AS cod, 'DICIEMBRE' AS descripcion"

        dtMes = conn.ObtenerDataTable(sql, New List(Of SqlParameter))

        With CmbMes
            .DataSource = dtMes
            .DisplayMember = "descripcion"
            .ValueMember = "cod"
        End With
    End Sub

    Private Sub CargarPeriodoMesParams()
        Dim MaestraDataSource = My.Settings.ConexionMaestraDataSource
        Dim MaestraInitialCatalog As String = ""
        Dim MaestraUser = My.Settings.ConexionMaestraUsuario
        Dim MaestraPass = My.Settings.ConexionMaestraPassword

        Dim connexionEmp = CrearCadenaConexion(MaestraDataSource, $"PRIXUS_{rutEmpresa}", MaestraUser, MaestraPass)

        Dim connexion As New Conexion(connexionEmp)

        Dim periodoRem = connexion.ObtenerValor("SELECT periodo FROM RemParametros")

        Dim mesRem = connexion.ObtenerValor("SELECT mes FROM RemParametros")

        If periodoRem <> "" And periodoRem <> "" Then
            CmbPeriodo.SelectedValue = periodoRem
            CmbMes.SelectedValue = mesRem
        Else
            CmbPeriodo.SelectedValue = Year(Now)
            CmbMes.SelectedValue = Month(Now)
        End If

        connexion.CerrarBD()
    End Sub

    Private Sub BtnIngresar_Click(sender As Object, e As EventArgs) Handles BtnIngresar.Click
        periodo = CmbPeriodo.SelectedValue
        mes = CmbMes.SelectedValue
        Me.Close()
    End Sub
End Class