{* Smarty *}

{literal}
<script type="text/javascript">
//<![CDATA[

function SvtAdd(){
  $.ajax({
    async: false,
    timeout: 3000,
    type: 'GET',
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtEdit.ajax.php{literal}",
    success: function(answ){
      DialogStandartOpen('SvtEditFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessRecordAdd}{literal}");
    }
  });
}

function SvtEdit(){
  var VarCountSelectedRows = $("#GridSvt").jqGrid('getGridParam','selarrrow').length;
  if(0 == VarCountSelectedRows){
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectRecord}{literal}");
  }
  else if(1 == VarCountSelectedRows){
    var VarId = $("#GridSvt").jqGrid('getGridParam','selrow');
    $.ajax({
      async: false,
      timeout: 3000,
      type: 'GET',
      url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtEdit.ajax.php{literal}",
      data: {
        "svt_id": VarId
      },
      success: function(answ){
        DialogStandartOpen('SvtEditFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessRecordEdit}{literal}");
      }
    });
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}

function SvtDel(){
  var VarId = $("#GridSvt").jqGrid('getGridParam','selarrrow');
  var VarCountSelectedRows = VarId.length;
  if(0 == VarCountSelectedRows){
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectRecord}{literal}");
  }
  else if(1 <= VarCountSelectedRows){
    $(function() {
      CreateDivForMessage("DialogConfirmMessage");
      $("#DialogConfirmMessageText").html("{/literal}{$MS_SP->Sp_LangMessage.message.MessAreYouSureDelete}{literal}");
      $("#DialogConfirmMessage").dialog({
        title: "{/literal}{$MS_SP->Sp_LangMessage.label.GridDelete}{literal}",
        resizable: false,
        height:180,
        modal: true,
        buttons: {
          "{/literal}{$MS_SP->Sp_LangMessage.label.GridDeleteYes}{literal}": function() {
            $.ajax({
              async: false,
              timeout: 3000,
              type: "GET",
              url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtDel.ajax.php{literal}",
              data: {
                svt_id: VarId
              },
              success: function(answ){
                if("undefined" == typeof(answ["error"])){
                  $("#GridSvt").trigger("reloadGrid");
                  DialogStandartMessageOpen('message', answ["message"]);
                }
                else{
                  DialogStandartMessageOpen('error', answ["error"]);
                }
              }
            });
            $(this).dialog("close");
          },
          "{/literal}{$MS_SP->Sp_LangMessage.label.GridDeleteNo}{literal}": function() {
            $(this).dialog("close");
          }
        }
      });
    });
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}
    
function SvtPo(){
  var VarCountSelectedRows = $("#GridSvt").jqGrid('getGridParam','selarrrow').length;
  if(0 == VarCountSelectedRows){
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectRecord}{literal}");
  }
  else if(1 == VarCountSelectedRows){
    var VarId = $("#GridSvt").jqGrid('getGridParam','selrow');
    $.ajax({
      async: false,
      timeout: 3000,
      type: 'GET',
      url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtPoGet.ajax.php{literal}",
      data: {
        svt_id: VarId
      },
      success: function(answ){
        DialogStandartOpen('SvtPoGetFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessListPo}{literal}", 740);
        $('#GridSvtPo').setGridWidth(720, false);
      }
    });
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}

function SvtMove(){
  var VarCountSelectedRows = $("#GridSvt").jqGrid('getGridParam','selarrrow').length;
  if(0 == VarCountSelectedRows){
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectRecord}{literal}");
  }
  else if(1 == VarCountSelectedRows){
    var VarId = $("#GridSvt").jqGrid('getGridParam','selrow');
    $.ajax({
      async: false,
      timeout: 3000,
      type: 'GET',
      url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtMoveGet.ajax.php{literal}",
      data: {
        "svt_id": VarId
      },
      success: function(answ){
        DialogStandartOpen('SvtMoveGetFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.GridsCaptions.SvtMove}{literal}", 740);
        $('#GridSvtMove').setGridWidth(720, false);
      }
    });
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}

function PrintSvtQrTHeadPrepare(VarTHead){
  return VarTHead
          .replace(/<\/?[^>]+>/gi, '')
          .replace('&nbsp;', '');
}

function PrintSvtQrTBodyPrepare(VarTBody){
  return VarTBody
          .replace(/<\/?[^>]+>/gi, '')
          .replace('&nbsp;', '')
          .replace(/&/g, "&amp;")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/"/g, "&quot;")
          .replace(/'/g, "&#039;")
          .replace(/^\s+|\s+$/g, "");
}

function PrintSvtQr(jqgridid){
  $('#'+jqgridid).jqGrid('hideCol','cb');
  var VarTHead=PrintSvtQrTHeadPrepare('&thead={/literal}{$MS_SP->Sp_LangMessage.GridsColNames.qr_code}{literal}');
//  var VarTHead='';
  var VarTHeadAdd = '';
  $('#gview_'+jqgridid+' .ui-jqgrid-labels > th:visible').each(function(){
    VarTHeadAdd = PrintSvtQrTHeadPrepare('&thead=' + $(this).html());
//    alert("VarTHeadAdd"+VarTHeadAdd);
    VarTHead = VarTHead + VarTHeadAdd;
  });
  var VarTBody='';
  var VarColspan='';
  var VarRowId='';
  var VarTBodyAdd = '';
  var i = 0;
  $("#"+jqgridid+" input[id^='jqg']:checked").each(function() {
    VarRowId = $(this).parent().parent().find("input[role='checkbox']:first").attr("id").replace("jqg_"+jqgridid+"_", "");
//    alert("ids: " + $(this).parent().parent().children("input[role='checkbox']").attr("id"));
//    alert("ids: " + );
    VarTBody=VarTBody+PrintSvtQrTBodyPrepare('&tbody='+VarRowId);
    $(this).parent().parent().children().filter("td:visible").each(function(){ 
      VarTBodyAdd = PrintSvtQrTBodyPrepare('&tbody=' + $(this).html());
//      alert("VarTHeadAdd"+VarTBodyAdd);
      VarTBody = VarTBody + VarTBodyAdd;
      if(0<$(this).attr('colspan')){
        VarColspan = VarColspan + '&colspan['+i+']=' + $(this).attr('colspan');
      }
      i++;
    });
  });
    $('<form action="./libs/site/AjaxServer/PrintSvtQrDocx.ajax.php" method="post"><input type="hidden" name="PrintSvtQrDocx" value="'+ 'grid=' + jqgridid + VarTHead + VarTBody + VarColspan +'" /></form>')
    .appendTo('body').submit().remove();
  $('#'+jqgridid).jqGrid('showCol','cb');
}

function SvtReloadGrid(VarValue){
//  alert("VarValue: "+VarValue);
  $("#GridSvt")
//    .setGridParam({postData: {svtstatus_id:VarValue}})
    .trigger("reloadGrid");
}

$(function() {
  $("#svtstatusradiobuttonset").buttonset();
  $("#svtstatusradiobuttonset :radio").click(function() {
    var VarValue = $(this).attr("id").replace("svtstatusradio_", "");
    SvtReloadGrid(VarValue);
  });
  
});
//]]>
</script>
{/literal}

{if isset($ArrSvtStatus) && is_array($ArrSvtStatus) && 1 <= count($ArrSvtStatus)}
{foreach $ArrSvtStatus as $val}
  {if $val@first}
<div id="svtstatusradiobuttonset">
  <input type="radio" id="svtstatusradio_all" name="svtstatusradio" checked="checked" /><label for="svtstatusradio_all">Все</label>
  {/if}
  <input type="radio" id="svtstatusradio_{$val.svtstatus_id}" name="svtstatusradio" /><label for="svtstatusradio_{$val.svtstatus_id}">{$val.svtstatus_name}</label>
  {if isset($val.svtstatus_main) && 1 eq $val.svtstatus_main}
    {literal}
      <script type="text/javascript">
      //<![CDATA[
      $(function() {
        $("#svtstatusradio_"+{/literal}{$val.svtstatus_id}{literal}).trigger('click');
      });
      //]]>
      </script>
    {/literal}
  {/if}
  {if $val@last}
</div>
  {/if}
{/foreach}
{/if}

{literal}
<script type="text/javascript">
//<![CDATA[
$(function BuildGrid(){
  $("#GridSvt").jqGrid('jqGridImport',{imptype:"jsonstring", impstring:window.localStorage.getItem("{/literal}{$smarty.session.SessionAuth.id}-GridSvt{literal}")});
  $("#GridSvt").jqGrid({
    caption:{/literal}'{$MS_SP->Sp_LangMessage.GridsCaptions.Svt}'{literal},
    datatype: "json",
    url:"{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtList.ajax.php{literal}",
    colNames:[{/literal}
      '{$MS_SP->Sp_LangMessage.GridsColNames.svt_name}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.svt_buxname}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.svttype_name}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.svt_invnum}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.svt_serialnum}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.structureorganization_title}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.users_fio}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.provider}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.svt_date}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.svt_garant_date}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.note}'{literal}
    ],
    colModel:[
      {name:'svt_name', index:'svt_name', width:110},
      {name:'svt_buxname', index:'svt_buxname', width:110},
      {name:'svttype_name', index:'svttype_name', width:80},
      {name:'svt_invnum', index:'svt_invnum', width:60},
      {name:'svt_serialnum', index:'svt_serialnum', width:90},
      {name:'structureorganization_title', index:'structureorganization_title', width:80},
      {name:'users_fio', index:'users_fio', width:80},
      {name:'organiz_name', index:'organiz_name', width:80},
      {name:'svt_date', index:'svt_date', width:90, sorttype:'date', datefmt: 'dd.mm.YYYY', formatter: 'date', 
        searchoptions:{
          dataInit:function(el){
            $(el).prop("VarGridName", 'GridSvt');
            GridDatepickerSetParam(el);
          }
        }
      },
      {name:'svt_garant_date', index:'svt_garant_date', width:70, sorttype:'date', datefmt: 'dd.mm.YYYY', formatter: 'date', 
        searchoptions:{
          dataInit:function(el){ 
            $(el).prop("VarGridName", 'GridSvt');
            GridDatepickerSetParam(el);
          }
        }
      },
      {name:'svt_note', index:'svt_note', width:180}
    ],
    multiselect: true,
    multiboxonly:true,
    rowNum:100,
    rowList:[10,25,100,500],
    pager: "#GridSvtPager",
    sortname: 'svt_invnum',
    sortorder: "asc",
    viewrecords: true,
    toolbar: [true,"top"],
    shrinkToFit:false,
    autowidth: true, 
    gridview: true,
    height: 290,
    postData: {
      svtstatus_id:function(){
        return $("#svtstatusradiobuttonset :radio:checked").attr('id').replace("svtstatusradio_", "");
      }
    }
  });
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Print}{literal}",position:"first",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Print}{literal}", buttonicon :'ui-icon-print', cursor: "pointer", onClickButton:function(){GridPrintReport('GridSvt');}});
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Add}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Add}{literal}", buttonicon :'ui-icon-plus', cursor: "pointer", onClickButton:SvtAdd });
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Edit}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Edit}{literal}", buttonicon :'ui-icon-pencil', cursor: "pointer", onClickButton:SvtEdit });
{/literal}
  {if isset($smarty.session.SessionAuth.SvtAdmin.reg) && 1 eq $smarty.session.SessionAuth.SvtAdmin.reg}
{literal}
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Del}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Del}{literal}", buttonicon :'ui-icon-trash', cursor: "pointer", onClickButton:SvtDel });
{/literal}
  {/if}
{literal}
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.SvtMove}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.SvtMove}{literal}", buttonicon :'ui-icon-transferthick-e-w', cursor: "pointer", onClickButton:SvtMove });
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.PO}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.PO}{literal}", buttonicon :'ui-icon-link', cursor: "pointer", onClickButton:SvtPo });
  $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.PrintSvtQr}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Print}{literal}", buttonicon :'ui-icon-print', cursor: "pointer", onClickButton:function(){PrintSvtQr('GridSvt');}});
  {/literal}
  {if isset($VarSvtSelect) && 1 eq $VarSvtSelect}
  {literal}
    $("#GridSvt").toolbarButtonAdd("#t_GridSvt",{caption:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Select}{literal}",position:"last",title:"{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Select}{literal}", buttonicon :'ui-icon-check', cursor: "pointer", onClickButton:SvtSelect });
  {/literal}
  {/if}
  {literal}
  $("#GridSvt").jqGrid('navGrid','#GridSvtPager', {edit:false,add:false,del:false,refresh:false});
  $("#GridSvt").jqGrid("navButtonAdd","#GridSvtPager",{
    caption: "",
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Refresh}{literal}",
    buttonicon :'ui-icon-refresh',
    onClickButton : function (){
      $("#GridSvt").trigger("reloadGrid");
    }
  });
  $("#GridSvt").jqGrid("navButtonAdd","#GridSvtPager",{
    caption: "",
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.ColumnChooser}{literal}",
    buttonicon :'ui-icon-transfer-e-w',
    onClickButton : function (){
      $("#GridSvt").jqGrid('columnChooser');
    }
  });
  $("#GridSvt").navButtonAdd("#GridSvtPager",{ 
    caption: "", 
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.SaveSettings}{literal}", 
    buttonicon :'ui-icon-disk',
    onClickButton: function () {
      if (typeof window.localStorage !== 'undefined') {
        delete $("#GridSvt").jqGrid('getGridParam' ,'postData' )['filters'];
        window.localStorage.setItem("{/literal}{$smarty.session.SessionAuth.id}-GridSvt{literal}",$("#GridSvt").jqGrid('jqGridExport',{exptype:'jsonstring'}));
        DialogStandartMessageOpen('message', "{/literal}{$MS_SP->Sp_LangMessage.message.MessSettingsSaved}{literal}");
      }
      else{          
        DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.error.ErrNoLocalStorage}{literal}");
      }
    }
  });
  $("#GridSvt").navButtonAdd("#GridSvtPager",{ 
    caption: "", 
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.ResetSettings}{literal}", 
    buttonicon :'ui-icon-closethick',
    onClickButton: function () {
      CreateDivForMessage("DialogConfirmMessage");
      $("#DialogConfirmMessageText").html('{/literal}{$MS_SP->Sp_LangMessage.message.MessResetSettConfirm}{literal}');
      $("#DialogConfirmMessage").dialog({
        title: "{/literal}{$MS_SP->Sp_LangMessage.DialogTitles.ResetSettings}{literal}",
        resizable: false,
        height:180,
        modal: true,
        buttons: {
          "{/literal}{$MS_SP->Sp_LangMessage.label.GridDeleteYes}{literal}": function() {
            window.localStorage.removeItem("{/literal}{$smarty.session.SessionAuth.id}-GridSvt{literal}");
            $(this).dialog('close');
            $("#GridSvt").jqGrid('GridUnload');
            BuildGrid();
            DialogStandartMessageOpen('message', "{/literal}{$MS_SP->Sp_LangMessage.message.MessResetSettSuccess}{literal}");
          },
          "{/literal}{$MS_SP->Sp_LangMessage.label.GridDeleteNo}{literal}": function() {
            $(this).dialog('close');
          }
        }
      });
    }
  });
  $("#GridSvt").jqGrid('filterToolbar',{stringResult: true, searchOnEnter : true, defaultSearch:"cnof"});
  UpdateJqGrid("GridSvt","GridSvtWrapper");
});
//]]>
</script>
{/literal}

<div id="GridSvtWrapper" class="ui-widget-content ui-corner-all">
  <table id="GridSvt"><tr><td></td></tr></table>
  <div id="GridSvtPager"></div>
  <div id="t_toolbar1"></div>
</div>