{* Smarty *}

{literal}
<script type="text/javascript">
//<![CDATA[
function PurchaseAdd(){
  $.ajax({
    async: false,
    timeout: 3000,
    type: 'GET',
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/PurchaseEdit.ajax.php{literal}",
    success: function(answ){
      DialogStandartOpen('PurchaseEditFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessRecordAdd}{literal}");
    }
  });
}

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

function PurchaseDel(){
  var VarId = $("#GridPurchase").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/PurchaseDel.ajax.php{literal}",
              data: {
                purchase_id: VarId
              },
              success: function(answ){
                if("undefined" == typeof(answ["error"])){
                  $("#GridPurchase").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.error.ErrUnknownError}{literal}");
  }
}

$(function BuildGrid(){
  $("#GridPurchase").jqGrid('jqGridImport',{imptype:"jsonstring", impstring:window.localStorage.getItem("{/literal}{$smarty.session.SessionAuth.id}-GridPurchase{literal}")});
  $("#GridPurchase").jqGrid({
    caption:{/literal}'{$MS_SP->Sp_LangMessage.GridsCaptions.Purchase}'{literal},
    datatype: "json",
    url:"{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/PurchaseList.ajax.php{literal}",
    colNames:[{/literal}
      '{$MS_SP->Sp_LangMessage.GridsColNames.name}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.structureorganization_title}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.status}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.price}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.count}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.cost}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.cartridge_buydate}',
      '{$MS_SP->Sp_LangMessage.GridsColNames.note}'{literal}
    ],
    colModel:[
      {name:'purchase_name', index:'purchase_name', width:350},
      {name:'structureorganization_title', index:'structureorganization_title', width:120},
      {name:'purchasestatus_name', index:'purchasestatus_name', width:100},
      {name:'purchase_price', index:'purchase_price', width:100, formatter: 'number', sorttype: 'number', align: 'right', summaryType:'sum'},
      {name:'purchase_quantity', index:'purchase_quantity', width:80, formatter: 'number', sorttype: 'number', align: 'right'},
      {name:'purchase_value', index:'purchase_value', width:100, formatter: 'number', align: 'right', summaryType:'sum'},
      {name:'purchase_datetill', index:'purchase_datetill', width:80, align:"right", sorttype:'date', datefmt: 'dd.mm.YYYY', formatter: 'date', 
        searchoptions:{
          dataInit:function(el){ 
            $(el).prop("VarGridName", 'GridPurchase');
            GridDatepickerSetParam(el);
          }
        }
      },
      {name:'purchase_note', index:'purchase_note', width:120}
    ],
    multiselect: true,
    multiboxonly:true,
    rowNum:100,
    rowList:[10,25,100,500],
    pager: "#GridPurchasePager",
    sortname: 'purchasestatus_order, purchase_id',
    sortorder: "desc",
    viewrecords: true,
    rownumbers: true,
    toolbar: [true,"top"],
    shrinkToFit:false, 
    gridview: true,
    autowidth: true,
    height: 290,
    footerrow: true,
    userDataOnFooter: true
  });
  $("#GridPurchase").toolbarButtonAdd("#t_GridPurchase",{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('GridPurchase');}});
  $("#GridPurchase").toolbarButtonAdd("#t_GridPurchase",{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:PurchaseAdd });
  $("#GridPurchase").toolbarButtonAdd("#t_GridPurchase",{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:PurchaseEdit });
{/literal}
  {if isset($smarty.session.SessionAuth.SvtAdmin.reg) && 1 eq $smarty.session.SessionAuth.SvtAdmin.reg}
{literal} 
  $("#GridPurchase").toolbarButtonAdd("#t_GridPurchase",{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:PurchaseDel });
{/literal}
  {/if}
  {if isset($VarPurchaseSelect) && 1 eq $VarPurchaseSelect}
  {literal}
    $("#GridPurchase").toolbarButtonAdd("#t_GridPurchase",{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:PurchaseSelect });
  {/literal}
  {/if}
  {literal}
$("#GridPurchase").jqGrid("navButtonAdd","#GridPurchasePager",{
    caption: "",
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.Refresh}{literal}",
    buttonicon :'ui-icon-refresh',
    onClickButton : function (){
      $("#GridPurchase").trigger("reloadGrid");
    }
  });
  $("#GridPurchase").jqGrid("navButtonAdd","#GridPurchasePager",{
    caption: "",
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.ColumnChooser}{literal}",
    buttonicon :'ui-icon-transfer-e-w',
    onClickButton : function (){
      $("#GridPurchase").jqGrid('columnChooser');
    }
  });
  $("#GridPurchase").navButtonAdd("#GridPurchasePager",{ 
    caption: "", 
    title: "{/literal}{$MS_SP->Sp_LangMessage.GridsButtons.SaveSettings}{literal}", 
    buttonicon :'ui-icon-disk',
    onClickButton: function () {
      if (typeof window.localStorage !== 'undefined') {
        delete $("#GridPurchase").jqGrid('getGridParam' ,'postData' )['filters'];
        window.localStorage.setItem("{/literal}{$smarty.session.SessionAuth.id}-GridPurchase{literal}",$("#GridPurchase").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}");
      }
    }
  });
  $("#GridPurchase").navButtonAdd("#GridPurchasePager",{ 
    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}-GridPurchase{literal}");
            $(this).dialog('close');
            $("#GridPurchase").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');
          }
        }
      });
    }
  });
  $("#GridPurchase").jqGrid('filterToolbar',{stringResult: true, searchOnEnter : true, defaultSearch:"cnof"});
  UpdateJqGrid("GridPurchase","GridPurchaseWrapper");
});
//]]>
</script>
{/literal}

<div id="GridPurchaseWrapper" class="ui-widget-content ui-corner-all">
  <table id="GridPurchase"><tr><td></td></tr></table>
  <div id="GridPurchasePager"></div>
  <div id="t_toolbar1"></div>
</div>