<?php
    class DATA_ROUTE extends DATA
    {
        public function data_route_javainit()
        {

        }

        public function fn_route_init()
        {
            if(isset($_REQUEST['task'])){$task=trim(strtolower($_REQUEST['task']));}
            if(isset($_POST['task'])){$task=trim(strtolower($_POST['task']));}
            if($_REQUEST||$_POST){if($_REQUEST['sec']||$_POST['sec']){$task=$this->dec($task);}}

            switch($task)
            {
                case "default":
                    $this->fn_default_route();
                break;

                case $task:
                    $task="fn_".$task;
                    if(method_exists($this,$task)){$this->$task();}
                break;

                default:
                    $this->fn_default_route();
                break;
            }
        }
        public function fn_default_route()
        {
            echo $this->FN_CREATEHEADER();

            echo "<style>
                      .besidebtnsave {margin-left:108px;margin-top:-6px;position:absolute;}
                      .besidebtnupdate {margin-left:210px;margin-top:-6px;position:absolute;}
                      .besidebtnsubmit {margin-left:292px;margin-top:-6px;position:absolute;}
                      .besidebtndelete {margin-left:306px;margin-top:-6px;position:absolute;}
                  </style>";
            echo "<script>
                       var myForm,myFormsearch,dgroute;
                       function initObject()
                       {";
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"block","width"=>"500",
                                                     "list"=>array(
                                                                      array("type"=>"hidden","name"=>"txt_id"),
                                                                      array("type"=>"input","name"=>"txt_routename","label"=>"Route Name","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"",
                                                                            "tooltip"=>"Masukkan Route Name terlebih dahulu. TERIMA KASIH","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft"),
                                                                      array("type"=>"input","name"=>"txt_description","label"=>"Description","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"",
                                                                            "tooltip"=>"Masukkan Description terlebih dahulu. TERIMA KASIH","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft"),
                                                                      array("type"=>"input","name"=>"txt_timedeparture","label"=>"Time Departure","required"=>"true","info"=>"true","inputWidth"=>"120","value"=>"00:00:00",
                                                                            "tooltip"=>"Masukkan tanggal berangkat terlebih dahulu. TERIMA KASIH","validate"=>"NotEmpty","maxLength"=>"20","className"=>"txtcenter"),
                                                                      array("type"=>"button","name"=>"btnsave","value"=>"Save","className"=>"button_update","offsetLeft"=>"20","offsetTop"=>"15"),
                                                                      array("type"=>"button","name"=>"btnupdate","value"=>"Update","className"=>"button_saveas besidebtnsave","offsetLeft"=>"0","disabled"=>"1"),
                                                                      //array("type"=>"button","name"=>"btnsubmit","value"=>"Submit","className"=>"button_submit besidebtnupdate","offsetLeft"=>"0","disabled"=>"1"),
                                                                      array("type"=>"button","name"=>"btndelete","value"=>"Delete","className"=>"button_delete besidebtnupdate","offsetLeft"=>"0","disabled"=>"1"),
                                                                      array("type"=>"button","name"=>"btncancel","value"=>"Cancel","className"=>"button_cancel besidebtndelete","offsetLeft"=>"0","disabled"=>"0")
                                                     )
                                              )
                                       );

                            $this->FN_DHTMLXFORM("myForm","divroutenew",$formdata);

                            $arrerror=array("txt_routename"=>"Error Found : Harap Isikan <b>Route Name</b> terlebih dahulu.TERIMA KASIH",
                                            "txt_description"=>"Error Found : Harap Isikan <b>Description</b> terlebih dahulu.TERIMA KASIH",
                                            "txt_timedeparture"=>"Error Found : Harap Isikan <b>Tanggal Berangkat</b> terlebih dahulu.TERIMA KASIH"
                                      );
                            $this->FN_DHTMLXFORMVALIDATIONERROR("myForm",$arrerror);

                            $arrayControls=array("txt_id"=>"",
                                                 "txt_routename"=>"",
                                                 "txt_description"=>"",
                                                 "txt_timedeparture"=>"00:00:00"
                                          );

                            // === Form Event
                            $onobjkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();if(myForm.isItemEnabled('btnupdate')){myForm.setItemFocus('btnupdate');dhtmlXclickButton('btnupdate');}else{myForm.setItemFocus('btnsave');}return false;}";

                            $onbuttonclickbtnsave="if(myForm.validate())
                                                   {
                                                        dhtmlx.confirm({
                                                              title:'Confirmation',text:'Are you will be saved and data is correctly?',type:'confirm',
                                                              callback:function(result){
                                                                  if(result)
                                                                  {";
                                                                      $params="'obj=".$this->enc("data_route")."&task=".$this->enc("saverecord")."'";
                                                                      $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_data")."',$params);
                                                                      myForm.send(url,\"get\",function(loader, response){";
                                                                          $onformsenddata.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();
                                                                          $onformsenddata.="var sp=trim(response).split('|');
                                                                          //alert(response);
                                                                          if(sp[0]=='ok')
                                                                          {
                                                                              fn_dhtmlxmessage('','Data <b>'+myForm.getItemValue('txt_routename')+'</b> has been saved');";
                                                                              $onformsenddata.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"txt_routename");
                                                                              $onformsenddata.="fn_grid_loaddata();
                                                                          }
                                                                          else if(sp[0]=='failed')
                                                                          {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                          else
                                                                          {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                      });";
                                                                      $onbuttonclickbtnsave.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                            $onbuttonclickbtnsave.="              }
                                                              }
                                                        });
                                                    }";

                            $onbuttonclickbtnupdt="if(myForm.validate())
                                                   {
                                                        dhtmlx.confirm({
                                                             title:'Confirmation',text:'Are you will be updated and data is correctly?',type:'confirm',
                                                             callback:function(result){
                                                                if(result)
                                                                {";
                                                                   $params="'obj=".$this->enc("data_route")."&task=".$this->enc("updrecord")."'";
                                                                   $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_data")."',$params);
                                                                   myForm.send(url,\"get\",function(loader, response){";
                                                                      $onformsenddata.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();
                                                                      $onformsenddata.="var sp=trim(response).split('|');
                                                                      //alert(response);
                                                                      if(sp[0]=='ok')
                                                                      {
                                                                           fn_dhtmlxmessage('','Data <b>'+myForm.getItemValue('txt_routename')+'</b> has been update');";
                                                                           $onformsenddata.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"txt_routename");
                                                                           $onformsenddata.="myForm.enableItem('btnsave');";
                                                                           $onformsenddata.="myForm.disableItem('btnupdate');";
                                                                           $onformsenddata.="myForm.disableItem('btndelete');";
                                                                           $onformsenddata.="fn_grid_loaddata();
                                                                      }
                                                                      else if(sp[0]=='failed')
                                                                      {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                      else
                                                                      {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                   });";
                                                                   $onbuttonclickbtnupdt.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                            $onbuttonclickbtnupdt.="            }
                                                             }
                                                        });
                                                   }";

                            $eventafterdeleted=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"txt_routename");
                            $eventafterdeleted.="myForm.enableItem('btnsave');";
                            $eventafterdeleted.="myForm.disableItem('btnupdate');";
                            $eventafterdeleted.="myForm.disableItem('btndelete');";
                            $eventafterdeleted.="fn_grid_loaddata();";
                            $onbuttonclickbtndeleted=$this->FN_DHTMLXREMARKFORDELETED2("obj_data","data_route","myForm.getItemValue('txt_id')","myForm.getItemValue('txt_routename')",$eventafterdeleted);

                            $onbuttonclickbtncancel=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls);
                            $onbuttonclickbtncancel.="myForm.enableItem('btnsave');";
                            $onbuttonclickbtncancel.="myForm.disableItem('btnupdate');";
                            $onbuttonclickbtncancel.="myForm.disableItem('btndelete');";
                            $onbuttonclickbtncancel.="myForm.setItemFocus('txt_routename');";
                            $onbuttonclickbtncancel.="fn_grid_loaddata();";

                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnsave"=>"$onbuttonclickbtnsave",
                                                                                "btnupdate"=>"$onbuttonclickbtnupdt",
                                                                                "btndelete"=>"$onbuttonclickbtndeleted",
                                                                                "btncancel"=>"$onbuttonclickbtncancel"
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                                "txt_description"=>"$onobjkeydown"
                                                          )
                                                )
                            );


                            $this->FN_DHTMLXFORMEVENT("myForm",$arrevent);

                            // === FORM SEARCH AND GRID
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"fieldset","width"=>"580","label"=>"Search",
                                                     "list"=>array(
                                                                      array("type"=>"input","name"=>"txtsearch","label"=>"Insert Your Keyword","info"=>"true","inputWidth"=>"240",
                                                                            "tooltip"=>"Insert Your Keyword","maxLength"=>"","className"=>"txtleft","labelWidth"=>"210"),
                                                                      array("type"=>"newcolumn"),
                                                                      array("type"=>"button","name"=>"btnfind","value"=>"","className"=>"button_find","offsetLeft"=>"10","offsetTop"=>"0")
                                                     )
                                              )
                            );

                            $this->FN_DHTMLXFORM("myFormsearch","divroutesearch",$formdata,true);
                            $ontxtsearchkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();myFormsearch.setItemFocus('btnfind');}";
                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnfind"=>"fn_grid_loaddata()"
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                                "txtsearch"=>"$ontxtsearchkeydown"
                                                          )
                                                )
                            );
                            $this->FN_DHTMLXFORMEVENT("myFormsearch",$arrevent);

                            $grdinit=array("gridname"=>"dgroute","divtarget"=>"div_dgroute","divpaging"=>"div_dgroutepaging","paginglimit"=>PAGE_LIMIT,
                                           "showloader"=>"true");
                            $gridproperties=array("id"=>"right,ro,96,right,int,false,true",
                                                  "No.&nbsp;"=>"right,ro,35,center,int,false",
                                                  "Route Name"=>"left,ro,230,left,str,false",
                                                  "Description"=>"left,ro,238,left,str,false",
                                                  "Time Departure"=>"center,ro,130,center,str,false");
                            $this->FN_DHTMLXGRIDVIEW($grdinit,$gridproperties,"bricks","","fn_grid_loaddata()");
                            $arrfields=array("txt_id"=>"","txt_routename"=>"","txt_description"=>"","txt_timedeparture"=>"");
                            $otherevent="";
                            $this->FN_DHTMLXGRIDVIEWTOFORMNEW("dgroute","myForm","tbl_route_information",0,$arrfields,"");
            echo "    }

                      function fn_grid_loaddata()
                      {
                          var keyword=myFormsearch.getItemValue('txtsearch');
                          url=fn_dhtmlxloadobject('".$this->enc("obj_data")."','obj=".$this->enc("data_route")."&task=".$this->enc("grdrecord")."&keyword='+keyword);
                          //alert(url);
                          dgroute.clearAndLoad(url);";
            echo "    }
                   </script>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");

            echo "<table width=\"98%\" border=\"0\">
                      <tr>
                          <td>";
                              $url="location.reload();";
                              $myuri=array("<b>ROUTE INFORMATION</b>"=>"$url|id='a_route'");
                              echo $this->MULTIPLE_LINK($myuri);
            echo "       </td>
                      </tr>
                      <tr>
                          <td>
                              <div id=\"divroutecontent\" style=\"margin-left:0px;\">
                                  <table width=\"98%\" border=\"0\">
                                      <tr>
                                          <td width=\"45%\" valign=\"top\">
                                              <div id=\"divroutenew\" style=\"\"></div>
                                          </td>
                                          <td valign=\"top\">
                                              <table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\">
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <div id=\"divroutesearch\" style=\"width:560px;margin-top:-6px;\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dgroute\" style=\"height:380px;width:580px;overflow:hidden\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dgroutepaging\" style=\"width:598px;margin-top:1px\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr><td valign=\"top\"><div id=\"loadergrid\" style=\"z-index:100;position:absolute;margin-top:-220px;margin-left:287px;display:visibility;\">".$this->CREATE_IMAGE("loadergrid.gif")."</div></td></tr>
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <table>
                                                              <tr>
                                                                  <td style='color:red;font-weight:bold;'>Notes</td>
                                                                  <td>:</td>
                                                                  <td>- Click Row For Edit</td>
                                                              </tr>
                                                          </table>
                                                      </td>
                                                  </tr>
                                              </table>
                                          </td>
                                      </tr>
                                  </table>
                              </div>
                          </td>
                      </tr>
                  </table>";
            echo $this->FN_CREATEFOOTER();

        }

        public function fn_grdrecord()
        {
            $keyword=$_REQUEST["keyword"];
            header("Content-type: text/xml");
            echo('<?xml version="1.0" encoding="utf-8"?>');
            echo "<rows id='0'>";

            $qry="select id,routename,description,timedeparture
                  from ".DB_APORTIL.".tbl_route_information
                  where routename like '%$keyword%' and isdeleted=0
                  order by id asc limit ".TBL_PAGE_LIMIT;

            $rs=$this->selectquery($qry);
            if($rs)
            {
               $i=1;
               while($row=$this->data_row_array($rs))
               {
                  echo ("<row id='".$row["id"]."'>");
                        print("<cell><![CDATA[".$row["id"]."]]></cell>");
                        print("<cell><![CDATA[".$i."]]></cell>");
                        print("<cell><![CDATA[".$row["routename"]."]]></cell>");
                        print("<cell><![CDATA[".$row["description"]."]]></cell>");
                        print("<cell><![CDATA[".$row["timedeparture"]."]]></cell>");
                  echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                  echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

        }

        public function fn_saverecord($isupdate=null)
        {
            if($isupdate){$id=$_REQUEST['txt_id'];}
            $routename=$_REQUEST['txt_routename'];
            $description=$_REQUEST['txt_description'];
            $timedeparture=$_REQUEST['txt_timedeparture'];

            $qry="insert into ".DB_APORTIL.".tbl_route_information 
                  set routename='$routename',description='$description',timedeparture='$timedeparture',iby='".$_SESSION['nik']."',idt=now()";
            if($isupdate)
            {
                $qry="update ".DB_APORTIL.".tbl_route_information
                      set routename='$routename',description='$description',timedeparture='$timedeparture',uby='".$_SESSION['nik']."',udt=now()
                      where id='$id'";
            }

            if($this->executequery($qry)){echo "ok|";return true;}
        }
        public function fn_updrecord(){$this->fn_saverecord(true);}

        public function fn_delrecord()
        {
            $id=$_REQUEST['txt_id'];
            $remark=$_REQUEST['txtremark'];
            $qry="update ".DB_APORTIL.".tbl_route_information
                  set isdeleted=1,remark='$remark',dby='".$_SESSION['nik']."',ddt=now()
                  where id='$id'";
            if($this->executequery($qry)){echo "ok|";return true;}
            else
            {
              echo "failed|".$this->getErrorQuery();
            }
        }
    }
?>