
var checkOrderReason;
var checkVisitReason;
var orderEnabled;
var returnEnabled;
var encashmentEnabled;
var forwardIsntAllowedY;
var obligateNumber;
var resinic;
var reseduc;
var obligateBool;
var ObjectPresentation;
var OnPresentation;
var PathForPick;
var GetPreset;
var StartTime;
var EndTime;
var SlidesPick;
var DocumentsPr;
var obligateNumberStart;
var nstep;
var pictID2;
var pictID3;
var SlidesPickS

function OnLoad() {

}

function GetDocPr() {
  return GetPreset;
}

function OnLoading() {
  GetNumberObligate();
  Getresinic();
  GetEducat();
}

function GetNumberObligate() {
  obligateNumber = 0;

  var oblQuest = new Query( " SELECT COUNT(DISTINCT V.Presentation) "+
                            " FROM Document_SetPresentations_Presentations V " +
                            " INNER JOIN Document_SetPresentations DF " +
                            "   ON V.Ref=DF.Id " +
                            " INNER JOIN USR_Presentations USP " +
                            "   ON  USP.Id = DF.Id AND USP.Selected = 1 " +
                            " WHERE V.Obligatori = 1 AND DF.MostActive = 1 AND DF.Display = @filt ");

  oblQuest.AddParameter("filt", DB.Current.Constant.DisplayPresentations.Manager);
  var ObligateTotal = oblQuest.ExecuteScalar().ToString();

  var oblQuestReal = new Query( " SELECT COUNT(DISTINCT V.Slides) " +
                                " FROM Document_Visit_PresentationsResult V " +
                                " LEFT JOIN Document_SetPresentations_Presentations Df " +
                                "   ON  Df.Presentation = V.Presents  " +
                                " LEFT  JOIN Document_SetPresentations Dff  " +
                                "   ON  Dff.Id = Df.Ref " +
                                " INNER JOIN USR_Presentations USP " +
                                "   ON  USP.Id = Dff.Id AND USP.Selected = 1 " +
                                "  WHERE Df.Obligatori = 1 AND Dff.MostActive=1 AND V.Ref = @emptyRef AND Dff.Display = @filt " );

  oblQuestReal.AddParameter("filt", DB.Current.Constant.DisplayPresentations.Manager);
  oblQuestReal.AddParameter("emptyRef", $.workflow.visit);
  var ObligateReal = oblQuestReal.ExecuteScalar().ToString();

  obligateNumber = ObligateTotal - ObligateReal;

  if(obligateNumber <= 0) {
    obligateNumber = 0;
  }

  if (obligateNumber == '0') {
    forwardIsntAllowedY = true;
  }

  $.obligateredButton.Text = obligateNumber+')';

  return obligateNumber;
}

function GetPrivateImage(pictI, pictB) {
	var r = "/shared/catalog.grotem_slides/" + pictB.Id + "/"
	    + pictB.Picture + ".jpg";
	return r;
}

function GetPrivateImage5(pictI, pictB) {
  var r = "/shared/catalog.grotem_slides/" + pictI.Id.ToString() + "/"
	    + pictI.Picture + ".jpg";
	return r;
}

function ActionGetIm(sender, pictID2) {
	ObjectPresentation = "1";
	GetPreset=pictID2;

  Workflow.Refresh([$.snapshotLayout3]);
  $.obligateredButton.Text = obligateNumber+')';
}

function ActionGetIm2(sender, pictID, pictIDG, pictIDF) {
	pictID2 = pictID;
  pictID3= pictIDF;
  PathForPick = pictID2.ToString();
  $.imageindexon.Source = PathForPick;
  Workflow.Refresh([$.grScrollView3]);

  if (DocumentsPr != Null) {
    if (DocumentsPr = pictID.Id && SlidesPick != pictID.Picture) {
      UpdateRecResultTime(Format("{0:yyyy-MM-dd HH:mm:ss}",  DateTime.Now));
    }
  }

  DocumentsPr = pictID.Id;
  SlidesPick = pictID;
  StartTime = Format("{0:yyyy-MM-dd HH:mm:ss}",  DateTime.Now);

  if (SlidesPick != GlobalWorkflow.GetSlideView()) {
    newFile = DB.Create("Document.Visit_PresentationsResult");
    newFile.Ref = $.workflow.visit;
    newFile.Presents = GetPreset;
    newFile.Slides = SlidesPick;
    GlobalWorkflow.SetSlideView(SlidesPick);
    newFile.StartTime = StartTime;
    //newFile.EndTime = DateTime.Now;
    newFile.Save();
  }

  $.obligateredButton.Text = obligateNumber+')';

  intMumber();
}

function intMumber(){
	return $.obligateredButton.Text = obligateNumber+')';
}

function GetImgF(pictID) {
	var q = new Query("SELECT V.Id, V.Picture, V.Extension from Catalog_Slides V WHERE V.Id=@PathToPick LIMIT 1");
	q.AddParameter("PathToPick", pictID2);

	var res = q.Execute();

  if (res.Next()) {
  	var r = "/shared/catalog.grotem_slides/" + pictID2.Id.ToString() + "/"
  		+ res.Picture + res.Extension;

    $.obligateredButton.Text = obligateNumber+')';
    return r;
  } else {
		return null;
  }
}

function Getresinic() {
  if (resinic == null) {
    resinic = 'Продажа инициатив';
  }
}

function GetEducat() {
  if (reseduc == null) {
    reseduc = 'Проведено обучение';
  }
}

function UpdateRecResultTime(NextSrinTime) {
  var q = new Query("SELECT Id FROM Document_Visit_PresentationsResult V WHERE V.EndTime IS NULL LIMIT 1");
  var parameters = q.ExecuteScalar();
  if (q.ExecuteCount() > 0) {
    var ETach =  parameters.GetObject();
    if (NextSrinTime == null) {
    } else {
      ETach.EndTime = NextSrinTime;
      resinic = 'Продажа инициатив';
      reseduc = 'Проведено обучение';
    }

    ETach.Save();
  }

  Workflow.Refresh([]);
}

function UpdateRecResult() {
  var q = new Query("SELECT Id FROM Document_Visit_PresentationsResult V WHERE V.EndTime IS NULL LIMIT 1");
  var parameters = q.ExecuteScalar();
  if (q.ExecuteCount() > 0) {
    var ETach =  parameters.GetObject();

    if (reseduc == 'Проведено обучение'){
      ETach.EnableTeach = 0;
    } else {
      ETach.EnableTeach = 1;
    }

    if (resinic == 'Продажа инициатив'){
      ETach.EnableIniciativ = 0;
    } else {
      ETach.EnableIniciativ = 1;
    }

    ETach.Save();
  }

  Workflow.Refresh([]);
}

function OpenViewPres(par1, par2, par3){
  if(SlidesPick != undefined) {
    var q = new Query("SELECT Id FROM Document_Visit_PresentationsResult V WHERE V.EndTime IS NULL LIMIT 1");
    var parameters = q.ExecuteScalar();

    if (q.ExecuteCount() > 0) {
      var ETach =  parameters.GetObject();
      var NextSrinTime = Format("{0:yyyy-MM-dd HH:mm:ss}",  DateTime.Now);
      ETach.EndTime = NextSrinTime;
      ETach.Save();
    }

    DoAction(par1, par2, SlidesPick.Id);
  }
}

function InInic() {
  if (SlidesPick != undefined) {
    if(resinic == 'Продажа инициатив'){
      resinic = 'Продажа инициатив ✓';
    } else {
      resinic = 'Продажа инициатив';
    }

    UpdateRecResult();
  }
}

function InEducat() {
  if(SlidesPick != undefined) {
    if(reseduc == 'Проведено обучение') {
      reseduc = 'Проведено обучение ✓';
    } else {
      reseduc = 'Проведено обучение';
    }

    UpdateRecResult();
  }
}

function GetSnapShots() {
  var q = new Query("SELECT Df.Id, V.Slide  from   Catalog_Presentations_Slides V  INNER JOIN Catalog_Presentations Df  ON  Df.Id = V.Ref INNER JOIN Document_SetPresentations_Presentations Dm  ON  Df.Id = Dm.Presentation INNER JOIN USR_Presentations Dk ON Dm.Ref = Dk.Id INNER JOIN Document_SetPresentations Dkk ON Dkk.Id = Dk.Id WHERE Dkk.MostActive== 1 AND Dk.Selected == 1 AND Dkk.Display = @filt AND V.LineNumber = 1 GROUP BY Df.Id ORDER BY V.LineNumber");
  q.AddParameter("filt", DB.Current.Constant.DisplayPresentations.Manager);

  return q.Execute();
}

function GetSnapShotsNew() {
  var q = new Query("SELECT Df.Id, V.Slide  from   Catalog_Presentations_Slides V  INNER JOIN Catalog_Presentations Df  ON  Df.Id = V.Ref INNER JOIN Document_SetPresentations_Presentations Dm  ON  Df.Id = Dm.Presentation INNER JOIN USR_Presentations Dk ON Dm.Ref = Dk.Id INNER JOIN Document_SetPresentations Dkk ON Dkk.Id = Dk.Id WHERE Dkk.MostActive== 1 AND Dk.Selected == 1 AND Dkk.Display = @filt GROUP BY Df.Id ORDER BY V.LineNumber");
  q.AddParameter("filt", DB.Current.Constant.DisplayPresentations.Manager);
  return q.ExecuteScalar();
}

function GetSnapShots2() {
  var q = new Query("SELECT V.Id, V.Ref, V.Slide, V.LineNumber from Catalog_Presentations_Slides V WHERE V.Ref=@Ref ORDER BY V.LineNumber");
	q.AddParameter("Ref", GetPreset);

  return q.Execute();

  if (res.Next()) {
    return q.Execute();
  } else {
  	return null;
  }
}

function GetSnapShots2New() {
  var q = new Query("SELECT V.Id, V.Ref, V.Slide, V.LineNumber from Catalog_Presentations_Slides V WHERE V.Ref=@Ref ORDER BY V.LineNumber");
	q.AddParameter("Ref", GetPreset);

  return q.ExecuteScalar();

  if (res.Next()){
	 return q.ExecuteScalar();
  } else {
  	return null;
  }
}

function OnVPresentation() {
  if (ObjectPresentation == null) {
    return false
  } else {
    return true
  }
}

function DoBackPr() {
  var q = new Query("SELECT Id FROM Document_Visit_PresentationsResult V WHERE V.EndTime IS NULL LIMIT 1");
  var parameters = q.ExecuteScalar();

  if (q.ExecuteCount() > 0) {
    var ETach =  parameters.GetObject();
    var NextSrinTime = Format("{0:yyyy-MM-dd HH:mm:ss}",  DateTime.Now);
    ETach.EndTime = NextSrinTime;
    resinic = undefined;
    reseduc = undefined;
    ETach.Save();
  }

  ETach = undefined;
  DocumentsPr = null;
  SlidesPick = undefined;
  obligateNumber = null;
  $.path = null;
  $.imageindexon = null;
  pictID = null;
  GetPreset = null;
  ObjectPresentation = null;
  OnPresentation = null;
  PathForPick = null;
  GetPreset = null;
  StartTime = null;
  EndTime = null;

  DocumentsPr = null;
  obligateNumberStart = null;
  nstep = null;
  pictID2 = undefined;
  resinic = undefined;
  reseduc = undefined;
  StartTime = undefined;

  Workflow.Back();
}

function CheckIfEmptyAndForward(order, wfName) {
  if (obligateNumber == 0) {
    if (StartTime != undefined) {
      if (SlidesPick != GlobalWorkflow.GetSlideView()) {
        newFile = DB.Create("Document.Visit_PresentationsResult");
        newFile.Ref = $.workflow.visit;
        newFile.Presents = GetPreset;
        newFile.Slides = SlidesPick;
        newFile.StartTime = StartTime;
        GlobalWorkflow.SetSlideView(SlidesPick);
        newFile.EndTime = Format("{0:yyyy-MM-dd HH:mm:ss}",  DateTime.Now);

        newFile.Save();
      }
    }

    ETach = null;
    DocumentsPr = null;
    SlidesPick = undefined;
    obligateNumber = null;
    $.path = null;
    $.imageindexon = null;
    pictID = null;
    GetPreset = null;
    ObjectPresentation = null;
    OnPresentation = null;
    PathForPick = null;
    GetPreset = undefined;
    StartTime = null;
    EndTime = null;

    DocumentsPr = null;
    obligateNumberStart = null;
    nstep = null;
    pictID2 = null;
    StartTime = undefined;
    resinic = undefined;
    reseduc = undefined;

    Workflow.Forward([]);
  }
}
