VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} ImportDataDlg 
   Caption         =   "Построение Схемы реестра"
   ClientHeight    =   6810
   ClientLeft      =   120
   ClientTop       =   465
   ClientWidth     =   5835
   OleObjectBlob   =   "ImportDataDlg.frx":0000
   StartUpPosition =   1  'CenterOwner
End
Attribute VB_Name = "ImportDataDlg"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ======= Dialog for creating new Schema =============
Option Explicit

Private isCancelled_ As Boolean
Private xlAppl_ As API_XLWrapper

Private Sub UserForm_Initialize()
  isCancelled_ = True
End Sub

Public Function GetResult() As ItemImportOptions
  Dim iOptions As New ItemImportOptions
  
  iOptions.layout_ = IIf(optV.Value, VLT_VERTICAL, VLT_HORIZONTAL)
  iOptions.sFile_ = tbFileName.Text
  iOptions.links_ = cbRemoveLinks.Value = False
  
  Dim nLvl&
  For nLvl = 1 To MAX_LEVEL
    Call iOptions.levels_.Add(GetLevelValue(nLvl))
  Next nLvl
  
  Set GetResult = iOptions
End Function

Public Property Get Cancelled() As Boolean
  Cancelled = isCancelled_
End Property

Public Function Init(xlAppl As API_XLWrapper)
  Set xlAppl_ = xlAppl
End Function

' ====================
Private Sub btnInput_Click()
  Dim sFileName$: sFileName = UserInteraction.PromptFileFilter( _
                                  sInitialPath:=Visio.ActiveDocument.Path, _
                                  sDescription:="Выбор файла с данными", _
                                  sFilter:="*.xlsx;*.xls;*.xlsm", _
                                  sTitle:="Таблица Excel", _
                                  bNewApplication:=True)
  If sFileName = vbNullString Then _
    Exit Sub
  Call xlAppl_.ReleaseDocument(bCloseApplication:=False)
  If xlAppl_.OpenDocument(sFileName, bReadOnly:=True) Is Nothing Then _
    Exit Sub
    
  tbFileName.Text = sFileName
  Call LoadCounts
End Sub

Private Sub cmdCancel_Click()
  isCancelled_ = True
  Me.Hide
End Sub

Private Sub cmdOk_Click()
  If tbFileName.Text = vbNullString Then
    Call MsgBox("Укажите путь к файлу-источнику!", vbExclamation)
  Else
    If xlAppl_.Document Is Nothing Then _
      Call xlAppl_.OpenDocument(tbFileName.Text, bReadOnly:=True)
    isCancelled_ = False
    Me.Hide
  End If
End Sub

Private Sub LevelTB1_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB2_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB3_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB4_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB5_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB6_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB7_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB8_Change()
  Call UpdateSum
End Sub

Private Sub LevelTB9_Change()
  Call UpdateSum
End Sub

' =================
Private Function UpdateSum()
  Dim val&: val = 0
  Dim i&
  For i = 1 To MAX_LEVEL
    val = val + GetLevelValue(i)
  Next i
  SumTB.Value = val
  TBWidth.Value = Round(630 / 22# * val, 0)
End Function

Private Function GetLevelValue(nLvl&) As Long
  Dim txt$: txt = Me.Controls("LevelTB" & nLvl).Value
  If IsNumeric(txt) Then _
    GetLevelValue = CLng(txt)
End Function

Private Function LoadCounts()
  Dim dataSht As Excel.Worksheet: Set dataSht = xlAppl_.Document.Sheets(1)
  Dim counters(1 To 9) As Long
  Dim nSum&: nSum = 0
  Dim nCounter&
  For nCounter = 1 To 9 Step 1
    Me.Controls("tbCount" & nCounter).Value = xlAppl_.Application.WorksheetFunction.CountIf(dataSht.Columns(2), nCounter)
    nSum = nSum + Me.Controls("tbCount" & nCounter).Value
  Next nCounter
  tbCountSum.Value = nSum
End Function
