using System;
using System.ComponentModel;

namespace CLIP.eForm.Consent.UI {
  /// <summary>
  /// 동의서 목록 클래스
  /// </summary>
  /// <remarks>
  /// <p>[설계자]</p>
  /// <p>  클립소프트 연구소 홍지철 (jchong@clipsoft.co.kr)</p>
  /// <p>[원본 작성자]</p>
  /// <p>  클립소프트 기술부 이창훈 (chlee@clipsoft.co.kr)</p>
  /// <p>[수정 작성자]</p>
  /// <p>  클립소프트 기술부 이인희</p>
  /// <p>----------------------------------------------------------------------------------------</p>
  /// <p>[HISTORY]</p>
  /// <p> 2016-06-21 : 최초작성</p>
  /// <p>----------------------------------------------------------------------------------------</p>
  /// </remarks>  
  public partial class ConsentListCtrl: ConsentListCtrlBase {
    private IConsentMain consentMain = null;
    private ConsentCommandCtrl commandControl = null;

    public ConsentListCtrl() {
      InitializeComponent();
    }

    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);

      if(this.DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime) {
        return;
      }

      consentMain = ConsentMainControl.GetConsentMainInterface(this);
      commandControl = consentMain.ConsentCommandCtrl as ConsentCommandCtrl;
      consentMain.OnLoadPartControls += ConsentMain_OnLoadPartControls;
      // dbs227, 임시 탭 삭제
      // knuh dependency
      tabControlConsentSelect.TabPages.RemoveAt(1);
    }

    private void ConsentMain_OnLoadPartControls(object sender, EventArgs e) {
      if(this.DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime) {
        return;
      }

      if(consentMain == null)
        consentMain = ConsentMainControl.GetConsentMainInterface(this);
      if(commandControl == null)
        commandControl = consentMain.ConsentCommandCtrl as ConsentCommandCtrl;

      // Ku2.0 연동 시에는 이벤트 태우지 않음
      if(consentMain.ConsentExecuteInfo["printYN"].Equals("Y")) {
        return;
      }

      SetConsentListSplitPanelVisibility(consentMain);
      // dbs227, 기본탭 변경
      this.tabControlConsentSelect.SelectedTab = this.tabControlConsentSelect.TabPages["tabPageAll"];
      //this.tabControlConsentSelect.SelectedTab = this.tabControlConsentSelect.TabPages["tabPageUnfinished"];
    }

    /// <summary>
    /// 동의서목록 탭 보이기 여부
    /// </summary>
    /// <param name="consentMain">The consent main.</param>
    private static void SetConsentListSplitPanelVisibility(IConsentMain consentMain) {
      if(!(string.IsNullOrEmpty(consentMain.ConsentExecuteInfo["useListView"])) && consentMain.ConsentExecuteInfo["useListView"].Equals("Y")) {
        consentMain.SetConsentListSplitPanelVisibility(true);
      }
      else {
        consentMain.SetConsentListSplitPanelVisibility(false);
      }
    }

    /// <summary>
    /// tabControlConsentSelect 컨트롤의 SelectedIndexChanged 이벤트 핸들러
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    private void tabControlConsentSelect_SelectedIndexChanged(object sender, EventArgs e) {
      // 탭이 선택될때 재조회해서 그리드 바인딩
      if(tabControlConsentSelect.SelectedIndex == 0) {
        if(this.tabControlConsentSelect.TabPages["tabPageAll"].Controls.Count > 0) {
          ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;

          consentSelectTabPageAll.BindDataGridRows();
        }
      }

      //            if (tabControlConsentSelect.SelectedIndex == 1) {
      //                if (this.tabControlConsentSelect.TabPages["tabPageUnfinished"].Controls.Count > 0) {
      //                    ConsentSelectTabPageUnfinished consentSelectTabPageUnfinished = this.tabControlConsentSelect.TabPages["tabPageUnfinished"].Controls[0] as ConsentSelectTabPageUnfinished;

      //consentSelectTabPageUnfinished.BindDataGridRows(-1);
      //}
      //} else 
      if(tabControlConsentSelect.SelectedIndex == 2) {
        if(this.tabControlConsentSelect.TabPages["tabPageConsentSet"].Controls.Count > 0) {
          ConsentSelectTabPageConsentSet consentSelectTabPageConsentSet = this.tabControlConsentSelect.TabPages["tabPageConsentSet"].Controls[0] as ConsentSelectTabPageConsentSet;

          consentSelectTabPageConsentSet.InitConsentSetTreeView();
        }
      }
      //if (consentMain == null) consentMain = ConsentMainControl.GetConsentMainInterface(this);
      //consentMain.ClearConsent();
    }

    //환자목록에서 더블클릭했을 경우, 선택된 환자 정보로 상단정보를 재조회한다.
    public override void OnRefeashPartControls() {
      if (consentMain == null)
        consentMain = ConsentMainControl.GetConsentMainInterface(this);

      if (consentMain.multiParams == null)
      { 
        consentMain.ReInitializeViewer();
      }
      // 동의서 이력 탭을 자동 조회 하도록
      ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;
      consentSelectTabPageAll.BindDataGridRows();

      // dbs227, 임시 탭 삭제
      // knuh dependency
      //pageMove_tabPageConsentUnfinished(consentMain); //미작성 탭을 기본으로 선택
      SetConsentListSplitPanelVisibility(consentMain);
    }

    // 동의서 목록 재조회 (미작성 동의서 삭제, 임시저장용 재조회, 인쇄출력용 재조회, 저장용(저장후 닫힘이 아닌 경우)) 
    public override void InquiryConsentData(int consentMstRid) {

      if(consentMstRid > 0) {
        // dbs227 임시 탭 삭제
        // knuh dependency
        this.tabControlConsentSelect.SelectedIndex = 0;

        ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;
        consentSelectTabPageAll.BindDataGridRows();

        // -- 기존 루틴
        //this.tabControlConsentSelect.SelectedIndex = 1;

        //ConsentSelectTabPageUnfinished consentSelectTabPageUnfinished = this.tabControlConsentSelect.TabPages["tabPageUnfinished"].Controls[0] as ConsentSelectTabPageUnfinished;
        //consentSelectTabPageUnfinished.SelectAllUnfinishedConsents(consentMstRid);

      }
      else {

        if(this.tabControlConsentSelect.SelectedIndex != 0) {
          this.tabControlConsentSelect.SelectedIndex = 0;
        }
        else {
          ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;
          consentSelectTabPageAll.BindDataGridRows();
        }
      }
      // 기저 클래스의 매서드는 아무 동작을 하지 않는다
      base.InquiryConsentData(-1);
    }

    /// <summary>
    /// 발행리스트 탭 갱신, dbs227, 경대병원 dependency
    /// </summary>
    public override void consentSelectTabPageAllRefresh() {
      ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;
      consentSelectTabPageAll.BindDataGridRows();
    }

    // 미작성 탭으로 이동
    public override void pageMove_tabPageConsentUnfinished(IConsentMain consentMain) {
      if(this.tabControlConsentSelect.TabPages["tabPageUnfinished"].Controls.Count > 0) {
        this.tabControlConsentSelect.SelectedTab = this.tabControlConsentSelect.TabPages["tabPageUnfinished"];
        ConsentSelectTabPageUnfinished consentSelectTabPageUnfinished = this.tabControlConsentSelect.TabPages["tabPageUnfinished"].Controls[0] as ConsentSelectTabPageUnfinished;
        consentSelectTabPageUnfinished.BindDataGridRows(-1);
      }

      ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;
      consentSelectTabPageAll.clearSearchStartDate();
    }

    public void changeInDd(string inDd)
    {
      ConsentSelectTabPageAll consentSelectTabPageAll = this.tabControlConsentSelect.TabPages["tabPageAll"].Controls[0] as ConsentSelectTabPageAll;
      consentSelectTabPageAll.dateTimePickerStartDate.Value = DateTime.ParseExact(inDd, "yyyyMMdd", null);
    }

  }
}
