Public Class Form_Status

    Enum E_SaveType
        WorkList = 0
        FileList = 1
        SrcList = 2
    End Enum

    Public AutoRun As Boolean = False
    Public Winhide As Boolean = False

    '//뷰의 내용을 파일로 저장한다.
    Public Sub SaveList(ByVal Div As E_SaveType, ByVal UseOD As Boolean)
        Dim NowString As String = Format(Now, "yyMMddHHmm")
        Dim Filename As String = ""
        Select Case Div
            Case E_SaveType.FileList
                Filename = "전체파일목록"
            Case E_SaveType.SrcList
                Filename = "원본목록"
            Case E_SaveType.WorkList
                Filename = "작업파일목록"
        End Select
        Filename = Filename & NowString & ".xls"

        If UseOD Then
            Dim OD As New SaveFileDialog
            OD.InitialDirectory = My.Application.Info.DirectoryPath
            OD.FileName = Filename
            OD.Filter = "엑셀워크시트(*xls)|*.xls"
            If OD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Select Case Div
                    Case E_SaveType.WorkList
                        PLog.Add("Save Work List : " + OD.FileName)
                        Me.FpSpread3.SaveExcel(OD.FileName, FarPoint.Excel.ExcelSaveFlags.SaveBothCustomRowAndColumnHeaders)
                    Case E_SaveType.FileList
                        PLog.Add("Save File List : " + OD.FileName)
                        Me.FpSpread1.SaveExcel(OD.FileName, FarPoint.Excel.ExcelSaveFlags.SaveBothCustomRowAndColumnHeaders)
                    Case E_SaveType.SrcList
                        PLog.Add("Save Source List : " + OD.FileName)
                        Me.FpSpread2.SaveExcel(OD.FileName, FarPoint.Excel.ExcelSaveFlags.SaveBothCustomRowAndColumnHeaders)
                End Select
                MsgBox("목록이 저장되었습니다" & vbCrLf & OD.FileName, MsgBoxStyle.Critical, "화인")
            End If
        Else
            Filename = PathList & "\" & Filename
            Select Case Div
                Case E_SaveType.WorkList
                    PLog.Add("Save Work List : " + Filename)
                    Me.FpSpread3.SaveExcel(Filename, FarPoint.Excel.ExcelSaveFlags.SaveBothCustomRowAndColumnHeaders)
                Case E_SaveType.FileList
                    PLog.Add("Save File List : " + Filename)
                    Me.FpSpread1.SaveExcel(Filename, FarPoint.Excel.ExcelSaveFlags.SaveBothCustomRowAndColumnHeaders)
                Case E_SaveType.SrcList
                    PLog.Add("Save Source List : " + Filename)
                    Me.FpSpread2.SaveExcel(Filename, FarPoint.Excel.ExcelSaveFlags.SaveBothCustomRowAndColumnHeaders)
            End Select
            'MsgBox("목록이 저장되었습니다" & vbCrLf & Filename, MsgBoxStyle.Critical, "화인")
        End If
    End Sub

    Private Sub bt_out_worklist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_out_worklist.Click
        Me.SaveList(E_SaveType.WorkList, True)
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Me.SaveList(E_SaveType.FileList, True)
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Me.SaveList(E_SaveType.SrcList, True)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRun.Click

        If Me.FpSpread3_Sheet1.RowCount <= 0 Then
            If Winhide Then
                MDiMain.Noti("작업목록이 없으므로 실행할 수 없습니다")
            Else
                MsgBox("작업목록이 존재하지 않습니다", MsgBoxStyle.Critical, "확인")
            End If
            Return
        End If

        ' MsgBox("1")
        If Not AutoRun Then
            If MsgBox("[복사/이동/삭제] 실행하시겠습니까?" + vbCrLf + "FTP의경우 백업정책이 적용되지 않습니다", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "실행확인") <> MsgBoxResult.Ok Then Return
        End If

        Me.btRun.Enabled = False
        MDiMain.Timer1.Enabled = False
        ' MsgBox("2")
        If GetrunType() = E_Runtype.Copy Then
            Try
                MoveFileRCopy(Winhide)
            Catch ex As Exception
                Errlog("MoveFileRCopy" & ex.Message.ToString)
            End Try

        ElseIf GetrunType() = E_Runtype.Move Then
            Try
                MoveFileRMove(Winhide)
            Catch ex As Exception
                Errlog("MoveFileRMove" & ex.Message.ToString)
            End Try

        Else
            Try
                DeleteFileR(Winhide)
            Catch ex As Exception
                Errlog("DeleteFileR" & ex.Message.ToString)
            End Try

        End If
        Try
            MakeBakLog1()
        Catch ex As Exception
            Errlog("MakeBakLog1" & ex.Message.ToString)
        End Try
        Application.DoEvents()

        MDiMain.Timer1.Enabled = True
        If Winhide Then
            MDiMain.Noti("완료되었습니다")
            ' Me.Close()
        Else
            MsgBox("완료되었습니다", MsgBoxStyle.Information, "확인")
        End If

    End Sub

    Private Sub bt_makebat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_makebat.Click
        Dim FN As String = ""
        Dim Scr As String = ""
        Select Case GetrunType()
            Case E_Runtype.Copy
                FN = "복사("
                Scr = "copy /y "
            Case E_Runtype.Move
                FN = "이동("
                Scr = "move /y "
            Case E_Runtype.Delete
                FN = "삭제("
                Scr = "del /f /q "
        End Select
        FN = "c:\" & FN & Format(Now, "yyyyMMdd") & ").bat"
        Dim Fs As New FileStream(FN, FileMode.Create)
        Dim Sw As New StreamWriter(Fs, System.Text.Encoding.Default)

        MDiMain.MSG("스크립트생성중")
        Dim index As Integer = 0
        Dim endcnt As Integer = MDiMain.DataSet1.WorkList.Rows.Count - 1

        Sw.WriteLine("@echo off")
        For Each Dr As ireaDataSet.WorkListRow In MDiMain.DataSet1.WorkList.Rows
            index += 1
            MDiMain.ShowCnt1(index, endcnt, "스크립트생성")
            Sw.WriteLine("echo " & index & "/" & MDiMain.DataSet1.WorkList.Rows.Count)
            Select Case GetrunType()
                Case E_Runtype.Delete
                    Sw.WriteLine("del /f /q " & Chr(&H22) & Dr.원본경로 & "\" & Dr.원본파일명 & Chr(&H22))
                Case E_Runtype.Copy
                    Sw.WriteLine("copy /y " & Dr.원본경로 & "\" & Dr.원본파일명 & Space(1) & Dr.대상경로 & "\" & Dr.대상파일명)
                Case E_Runtype.Move
                    Sw.WriteLine("move /y " & Dr.원본경로 & "\" & Dr.원본파일명 & Space(1) & Dr.대상경로 & "\" & Dr.대상파일명)
            End Select
        Next
        Sw.Flush()
        Sw.Close()
        Fs.Close()
        Sw = Nothing
        Fs = Nothing
        MDiMain.MSG("스크립트생성완료")
        MsgBox("배치파일이 생성되엇습니다" & vbCrLf & "파일명 : " & FN, MsgBoxStyle.Information, "확인")
    End Sub

    Private Sub Form_Status_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
       RefreshCount
    End Sub

    Private Sub RefreshCount()
        Me.TabPage1.Text = "작업대상목록(" & Me.FpSpread3_Sheet1.RowCount & ")"
        Me.TabPage2.Text = "파일목록(" & Me.FpSpread1_Sheet1.RowCount & ")"
        Me.TabPage3.Text = "원본목록(" & Me.FpSpread2_Sheet1.RowCount & ")"
    End Sub

    Private Sub Form_Status_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.FpSpread1.DataSource = MDiMain.DataSet1.FileList
        Me.FpSpread2.DataSource = MDiMain.DataSet1.SrcList
        Me.FpSpread3.DataSource = MDiMain.DataSet1.WorkList
        RefreshCount()

        If Winhide AndAlso WindowState <> FormWindowState.Minimized Then Me.WindowState = FormWindowState.Minimized
    End Sub
End Class