Option Strict Off
Imports System.Data.Odbc
Public Class frmHasilAHP
    Dim jlhBulat As Integer = 2
    Dim lstImportir() As String
    Dim lstAhp() As String
    Dim jlhKriteria As Byte
    Sub TampilPeriode()
        cboPeriode.Items.Clear()
        cmd = New OdbcCommand("select distinct Periode from tbnilaialternatif order by periode desc", Conn)
        rd = cmd.ExecuteReader
        While rd.Read
            cboPeriode.Items.Add(rd.Item(0))
        End While
    End Sub
    Sub loading()
        Koneksi()
        dg1.Columns.Clear()
        dg2.Columns.Clear()
        Dim lstKriteria, lstkode_Kriteria As New List(Of String)
        Dim sql As String = "select `nama_kriteria`,kode_kriteria from `tbkriteria` order by `kode_kriteria` asc"
        Dim cmd As New OdbcCommand(sql, Conn)
        Dim rd As OdbcDataReader = cmd.ExecuteReader
        While rd.Read
            dg1.Columns.Add(rd(0), rd(0))
            lstKriteria.Add(rd(0))
            lstkode_Kriteria.Add(rd(1))
        End While
        jlhKriteria = lstkode_Kriteria.Count
        rd.Close()
        dg1.Columns.Add("total", "Total")

        sql = "select a.no_importir,b.nama_barang from tbnilaialternatif a, tbhasil b " & _
              " where a.no_importir=b.no_importir and a. periode = '" & cboPeriode.Text & "' " & _
              " group by no_importir order by no_importir "
        Dim lstcNik As New List(Of String)
        cmd = New OdbcCommand(sql, Conn)
        rd = cmd.ExecuteReader
        Dim i As Integer = 0
        While rd.Read
            dg1.Rows.Add()
            dg1.Rows(i).HeaderCell.Value = rd(1)
            lstcNik.Add(rd(0))
            i += 1
        End While
        rd.Close()
        Dim lstM1 As New List(Of String())
        Dim lstM2 As New List(Of String())
        Dim lstM3 As New List(Of String())
        lstImportir = lstcNik.ToArray

        For alternatif As Integer = 0 To dg1.Rows.Count - 2
            For kriteria As Integer = 0 To dg1.Columns.Count - 2

                Dim M1() As String = {kriteria, alternatif, getPenilaianAlternatif2(lstcNik(alternatif), lstkode_Kriteria(kriteria))}
                lstM1.Add(M1)
                Dim M2() As String = {kriteria, alternatif, Format(getHasilAhp("all", lstkode_Kriteria(kriteria)), "0.00") & "*" & Format(getHasilAhp(lstkode_Kriteria(kriteria), getPenilaianAlternatif(lstcNik(alternatif), lstkode_Kriteria(kriteria))), "0.00")}
                lstM2.Add(M2)
                Dim M3() As String = {kriteria, alternatif, Format(Math.Round(getHasilAhp("all", lstkode_Kriteria(kriteria)) * getHasilAhp(lstkode_Kriteria(kriteria), getPenilaianAlternatif(lstcNik(alternatif), lstkode_Kriteria(kriteria))), jlhBulat), "0.00")}
                lstM3.Add(M3)
                ' dg1.Item(kriteria, alternatif).Value = Format(Math.Round(getHasilAhp("all", lstKriteria(kriteria)) * getHasilAhp(lstKriteria(kriteria), getPenilaianAlternatif(lstcNik(alternatif), lstKriteria(kriteria))), jlhBulat), "0.000")
                dg1.Item(kriteria, alternatif).Value = Math.Round(getHasilAhp("all", lstkode_Kriteria(kriteria)) * getHasilAhp(lstkode_Kriteria(kriteria), getPenilaianAlternatif(lstcNik(alternatif), lstkode_Kriteria(kriteria))), jlhBulat)
            Next
        Next

        Dim lstTotal As New List(Of String)
        '  Dim lstAlt As New List(Of String)
        For total As Integer = 0 To dg1.Rows.Count - 2
            Dim jlhTotal As Double = 0
            For kriteria As Integer = 0 To dg1.Columns.Count - 2
                jlhTotal += Val(dg1.Item(kriteria, total).Value)
            Next
            Dim M1() As String = {dg1.Columns.Count - 1, total, ""}
            Dim M3() As String = {dg1.Columns.Count - 1, total, Format(Math.Round(jlhTotal, jlhBulat), "0.00")}
            ' Dim M3() As String = {dg1.Columns.Count - 1, total, Math.Round(jlhTotal, jlhBulat)}
            lstM1.Add(M1)
            lstM2.Add(M1)
            lstM3.Add(M3)
            dg1.Item(dg1.Columns.Count - 1, total).Value = Format(Math.Round(jlhTotal, jlhBulat), "0.00")
            'dg1.Item(dg1.Columns.Count - 1, total).Value = Math.Round(jlhTotal, jlhBulat)
            lstTotal.Add(Format(Math.Round(jlhTotal, jlhBulat), "0.00"))
            ' lstAlt.Add(dg1.Item(total, 0).Value)
        Next

        Matriks1 = lstM1.ToArray
        Matriks2 = lstM2.ToArray
        Matriks3 = lstM3.ToArray
        steps = 3


        lstAhp = lstTotal.ToArray
        lstTotal.Sort()


    End Sub

    Dim steps As Integer = 0

    Dim Matriks1()() As String
    Dim Matriks2()() As String
    Dim Matriks3()() As String

    Function getPenilaianAlternatif(ByVal cNik As String, ByVal kriteria As String) As String
        Dim sql As String = "Select `kode_sub` from `tbnilaialternatif`  " & _
            "where  periode = '" & cboPeriode.Text & "' and no_importir='" & cNik & "' and kode_kriteria='" & kriteria & "'"
        Dim cmd As New OdbcCommand(sql, Conn)
        Dim rd As OdbcDataReader = cmd.ExecuteReader
        Dim strPenilaian As String = ""
        Dim strPenilaianSub As String = ""
        While rd.Read
            strPenilaian = rd(0)
        End While
        rd.Close()
        Return strPenilaian
    End Function
    Function getPenilaianAlternatif2(ByVal cNik As String, ByVal kriteria As String) As String
        Dim sql As String = "select A.sub_kriteria from tbsub_kriteria A,`tbnilaialternatif` B " & _
            "where A.Kode_sub=B.Kode_Sub and b.`no_importir`='" & cNik & "' and b.`kode_kriteria`='" & kriteria & "'"
        Dim cmd As New OdbcCommand(sql, Conn)
        Dim rd As OdbcDataReader = cmd.ExecuteReader
        Dim strPenilaian As String = ""
        Dim strPenilaianSub As String = ""
        While rd.Read
            strPenilaianSub = rd(0)
        End While
        rd.Close()
        Return strPenilaianSub
    End Function
    Function getHasilAhp(ByVal kriteria As String, ByVal subkriteria As String) As Double
        Dim sql As String = "select `hasil` from `tbprioritas` where `kode_kriteria`='" & kriteria & "' and `kode_sub`='" & subkriteria & "'"
        Dim cmd As New OdbcCommand(sql, Conn)
        Dim rd As OdbcDataReader = cmd.ExecuteReader
        Dim nilaiAhp As Double = 0
        While rd.Read
            nilaiAhp = Val(rd(0))
        End While
        rd.Close()
        Return nilaiAhp
    End Function

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub SimpanHasil()
        Dim cKet, cBulat As String
        If steps = 3 Then
            Dim sql As String = ""
            ' Dim cmd As New OdbcCommand(sql, Conn)
            'cmd.ExecuteNonQuery()
            For i As Integer = 0 To lstImportir.Length - 1
                cmd = New OdbcCommand("Select * from tbhasil where periode='" & cboPeriode.Text & "' and no_importir='" & lstImportir(i) & "'", Conn)
                rd = cmd.ExecuteReader
                rd.Read()
                cBulat = Replace(lstAhp(i), ",", ".")
                If lstAhp(i) >= 0.57 Then
                    cKet = "Layak"
                Else
                    cKet = "Tidak Layak"
                End If
                If Not rd.HasRows Then

                    Dim sqltambah As String = "Insert into tbhasil (periode,no_importir,hasil,ket) values " & _
                    "('" & cboPeriode.Text & "','" & lstImportir(i) & "','" & cBulat & "','" & cKet & "')"
                    cmd = New OdbcCommand(sqltambah, Conn)

                Else
                    Dim sqledit As String = "Update tbhasil set " & _
                    "hasil='" & cBulat & "',ket='" & cKet & "' " & _
                    " where periode='" & cboPeriode.Text & "' and no_importir='" & lstImportir(i) & "'"
                    cmd = New OdbcCommand(sqledit, Conn)

                End If
                cmd.ExecuteNonQuery()

                'sql = "insert into `tbhasil` values('" & cboPeriode.Text & "','" & lstImportir(i) & "','" & lstAhp(i) & "','" & cKet & "')"
                'cmd = New OdbcCommand(sql, Conn)
                'cmd.ExecuteNonQuery()
            Next
            MsgBox("Data Tersimpan")
        Else
            MsgBox("Tampilan Hasil AHP diatas, tidak dapat disimpan")
        End If
        Call TampilHasil()
    End Sub
   
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If steps < 3 Then
            steps += 1
            Select Case steps
                Case 2
                    setDG(Matriks2)
                Case 3
                    setDG(Matriks3)
            End Select
        End If
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        If steps > 1 Then
            steps -= 1
            Select Case steps
                Case 1
                    setDG(Matriks1)
                Case 2
                    setDG(Matriks2)
            End Select
        End If
    End Sub

    Sub setDG(ByVal M As String()())
        For i As Integer = 0 To M.Length - 1
            Dim column As Integer = Integer.Parse(M(i)(0))
            Dim rows As Integer = Integer.Parse(M(i)(1))
            Dim value As String = M(i)(2)
            dg1.Item(column, rows).Value = value
        Next
    End Sub

    Private Sub btnHitung_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHitung.Click
        Call loading()
        Call SimpanHasil()
        Call AturGrid1()
    End Sub


    Private Sub frmHasilAHP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call TampilPeriode()
       
    End Sub



    Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
        Me.Dispose()

    End Sub

    Sub TampilHasil()
        da = New OdbcDataAdapter("Select A.no_importir,B.nama_importir,B.Asal_Negara,A.Nama_Barang,A.Hasil,A.Ket from tbhasil A, tbimportir B where " & _
                                 " a.periode = '" & cboPeriode.Text & "' and a.no_importir=b.no_importir order by Ket,hasil Desc", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "tbimportir")
        dg2.DataSource = (ds.Tables("tbimportir"))
        dg2.ReadOnly = True
        dg2.AllowUserToAddRows = False
        dg2.Columns(0).Width = 100
        dg2.Columns(1).Width = 180
        dg2.Columns(2).Width = 100
        dg2.Columns(3).Width = 80

        dg2.GridColor = Color.Blue
        dg2.DefaultCellStyle.ForeColor = Color.Blue
        dg2.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        dg2.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        dg2.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dg2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg2.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg2.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg2.Columns(4).DefaultCellStyle.Format = "0.00"
        dg2.Columns(0).HeaderText = "No.Importir"
        dg2.Columns(1).HeaderText = "Nama Importir"
        dg2.Columns(2).HeaderText = "Asal Negara"
        dg2.Columns(4).HeaderText = "Hasil"
    End Sub
    Sub AturGrid1()
        dg1.ReadOnly = True
        dg1.GridColor = Color.Blue
        dg1.DefaultCellStyle.ForeColor = Color.Blue
        dg1.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        dg1.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        dg1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dg1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        For k As Integer = 0 To jlhKriteria
            dg1.Columns(k).DefaultCellStyle.Format = "0.00"
            dg1.Columns(k).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Next
    End Sub
    Private Sub nudPeriode_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call loading()
        Call AturGrid1()
        'Call TampilHasil()
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        If TabControl1.SelectedIndex = 1 Then
            Call TampilHasil()
            Panel2.Visible = False
        Else
            Panel2.Visible = True
        End If
    End Sub

    Private Sub cboPeriode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPeriode.SelectedIndexChanged
        Call loading()
        Call AturGrid1()
        ' Call TampilHasil()
    End Sub
End Class
