<?php
    class TRA_CANCELCRG extends TRA
    {
        public function tra_cancelcrg_javainit()
        {

        }

        public function fn_cancelcrg_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_cancelcrg();
                break;

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

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

            echo "<style>
                      .besidebtnsave {margin-left:104px;margin-top:-6px;position:absolute;}
                      .besidebtnupdate {margin-left:198px;margin-top:-6px;position:absolute;}
                      .besidebtndelete {margin-left:288px;margin-top:-6px;position:absolute;}                      
                  </style>";
            echo "<script>
                       var myForm,myFormsearch,dgtra,dwins;
                       function initObject()
                       {
                           dwins=new dhtmlXWindows();dwins.attachViewportTo(\"divchecklistdetail\");";
                           // === FORM SEARCH AND GRID
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"fieldset","width"=>"1080","label"=>"Search",
                                                     "list"=>array(
                                                                      array("type"=>"hidden","name"=>"txt_idcargo"),
                                                                      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","divcancelchecklistsearch",$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);

                            $footerevent="<div style='height:20px;padding-top:5px;'>";
                            $footerevent.="<a id='a_exporttoxls' href='javascript:void(0)' style='color:blue;font-size:10pt;' onClick='fn_cancelcrg()'>Cancel Cargo</a>&nbsp;&nbsp;&nbsp;";
                            $footerevent.="</div>";

                            $grdinit=array("gridname"=>"dgresult","divtarget"=>"div_dgresult","divpaging"=>"div_dgresultpaging","paginglimit"=>PAGE_LIMIT,
                                           "showloader"=>"true","enableMultiline"=>"true","footer"=>"$footerevent","footerspan"=>"17");
                            $gridproperties=array("id"=>"right,ro,96,right,int,false,true",
                                                  "<font color='green'><b>C&nbsp;&nbsp;</b></font>"=>"center,ra,30,center,int,false",
                                                  "No.&nbsp;"=>"right,ro,35,center,int,false",
                                                  "Status"=>"center,ro,110,center,str,false",
                                                  "statuscode"=>"center,ro,110,center,str,false,true",
                                                  "Ship Name"=>"left,ro,140,left,str,false",
                                                  "Ticket Number"=>"center,ro,130,center,str,false",
                                                  "Voyage Number"=>"center,ro,130,center,str,false",
                                                  "Passenger Name"=>"left,ro,262,left,str,false",
                                                  "ID Card"=>"center,ro,136,center,str,false",
                                                  "Dates"=>"center,ro,120,center,date,false",
                                                  "From"=>"left,ro,112,left,str,false",
                                                  "Category"=>"left,ro,144,left,str,false",                                                  
                                                  "Price&nbsp;"=>"right,ro,120,right,int,false",
                                                  "Quantity&nbsp;"=>"right,ro,144,right,int,false",
                                                  "Weight&nbsp;"=>"right,ro,144,right,int,false",
                                                  "Total&nbsp;"=>"right,ro,120,right,int,false");
                            $this->FN_DHTMLXGRIDVIEW($grdinit,$gridproperties,"bricks","","fn_grid_loaddata()");
                            
                            $oneditcell="if(colid==1&&stage==1)";
                            $oneditcell.="{";
                            $oneditcell.="   var status=dgresult.cells(rowid,4).getValue();";
                            $oneditcell.="   var idcargo=dgresult.cells(rowid,0).getValue();";
                            $oneditcell.="   var tiketnumber=dgresult.cells(rowid,7).getValue();";
                            $oneditcell.="   if(status!=0){fn_dhtmlxmessage('error','I\'m sorry you can\'t cancel this Tiket number!!');return false;}";                            
                            $oneditcell.="   myFormsearch.setItemValue('txt_idcargo',idcargo+'|'+tiketnumber);";                            
                            $oneditcell.="}";
                            $oneditcell.="return true;";
                            $arrevent=array(
                                                array("eventname"=>"onEditCell","eventparam"=>"stage,rowid,colid",
                                                      "statement"=>"$oneditcell"
                                                ),
                                                array("eventname"=>"onXLE","eventparam"=>"",
                                                      "statement"=>""
                                                )
                            );

                            $this->FN_DHTMLXGRIDVIEWEVENT("dgresult",$arrevent);

            echo "    }
                      
                         
                      
                      function fn_cancelcrg()
                      {";
                           //var idcargo=myFormsearch.getItemValue('txt_idcargo');                            
                           //var url='?ajax=1&sec=1&option=".$this->enc("obj_ss")."&obj=".$this->enc("ss_necheck")."&task=".$this->enc("viewchecklist")."&idchecklist='+idchecklist+'&checklistnumber='+checklistnumber+'&category='+category;";
                           $rtn="var idcargo=myFormsearch.getItemValue('txt_idcargo');";
                           $rtn.="var dhxcancel=new dhtmlXWindows();";
                           $rtn.="var wincancelcrgket=dhxcancel.createWindow(\"wincancelcrgket\",0,0,410,190);";            
                           $rtn.="dhxcancel.window('wincancelcrgket').progressOn();";
                           $rtn.="wincancelcrgket.setText('Confirmation Ticket Cancel');";
                           $rtn.="dhxcancel.window('wincancelcrgket').denyResize();";
                           $rtn.="dhxcancel.window('wincancelcrgket').setModal(true);";
                           $rtn.="dhxcancel.window('wincancelcrgket').bringToTop();";
                           $rtn.="dhxcancel.window('wincancelcrgket').center();";

                           // === FOrm Deleted
                           $formdata=array(
                                             array("type"=>"settings","position"=>"label-left","labelWidth"=>"160","inputWidth"=>"120"),
                                             array("type"=>"fieldset","label"=>"<font color='red'>Insert Remark</font> First","width"=>"255","offsetLeft"=>"5",
                                                   "list"=>array(
                                                                   array("type"=>"hidden","name"=>"txt_id"),
                                                                   array("type"=>"input","name"=>"txtremark","inputWidth"=>"320","className"=>"txtleft","maxLength"=>"90",
                                                                                         "note"=>array("text"=>"<span id='spjlhchar' style='color:blue;'>0/90</span> characters.")),
                                                                   array("type"=>"button","name"=>"btncancelcrg","value"=>"Cancel Booking","offsetLeft"=>"0","offsetTop"=>"0",
                                                                                          "className"=>"button_delete")
                                                           )
                                             )
                                     );
                           $formdata=json_encode($formdata);
                           $rtn.="var myformcancel=wincancelcrgket.attachForm($formdata, true);";
                           $rtn.="myformcancel.setFocusOnFirstActive();";
                           //$rtn.="myformcancel.setSkin('dhx_skyblue');";
                           
                           // === FOrm Event
                           $ontxtremarkonkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();myformcancel.setItemFocus('btndeleted');}";
                           $ontxtremarkonkeydown.="if(ev.keyCode==27){dhxcancel.unload();}";
                           $ontxtremarkonkeyup="var remarklength=myformcancel.getItemValue('txtremark');";
                           $ontxtremarkonkeyup.="document.getElementById('spjlhchar').innerHTML=remarklength.toString().length+'/90';";
                           
                           
                           $onbuttonclickbtncancel="var lengthremark=myformcancel.getItemValue('txtremark').trim().length;";
                           $onbuttonclickbtncancel.="var sptiket=myFormsearch.getItemValue('txt_idcargo').split('|');";
                           $onbuttonclickbtncancel.="var idcargo=sptiket[0];";
                           $onbuttonclickbtncancel.="var tiketnumber=sptiket[1];";
                           $onbuttonclickbtncancel.="if(lengthremark<=15){fn_dhtmlxmessage('error','Minimal 15 Character for the reason. Thank You!');myformcancel.setItemFocus('txtremark');return false;}";
                           $onbuttonclickbtncancel.="if(myformcancel.validate())
                                                      {
                                                          dhtmlx.confirm({
                                                                title:'Confirmation',text:'Are you sure to cancel this ticketing ?',type:'confirm',
                                                                callback:function(result){
                                                                     if(result)
                                                                     {";
                                                                        $params="'obj=".$this->enc("tra_cancelcrg")."&task=".$this->enc("cancelcrg")."&idcargo='+idcargo+'&remark='+myformcancel.getItemValue('txtremark')";
                                                                        $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_tra")."',$params);
                                                                        myformcancel.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>'+tiketnumber+'</b> has been deleted');";
                                                                              $onformsenddata.=$eventafterdeleted;   
                                                                              $onformsenddata.="dhxcancel.unload();fn_grid_loaddata();
                                                                          }
                                                                          else if(sp[0]=='failed')
                                                                          {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                          else
                                                                          {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                        });";
                                                                        $onbuttonclickbtncancel.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                           $onbuttonclickbtncancel.="               }
                                                                }
                                                          });
                                                      }";
                           $arrevent=array(
                                               array("eventname"=>"onButtonClick","eventparam"=>"id,val",
                                                                                  "switch"=>array("btncancelcrg"=>"$onbuttonclickbtncancel")
                                               ),
                                               array("eventname"=>"onkeyup","eventparam"=>"inp,ev,id,val",
                                                                   "switch"=>array("txtremark"=>"$ontxtremarkonkeyup")
                                               ),
                                               array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                                   "switch"=>array("txtremark"=>"$ontxtremarkonkeydown")
                                               )
                                     );  
                           $rtn.="myformcancel.setItemValue('txt_id',$idrecord);";                          
                           $rtn.=$this->FN_DHTMLXFORMEVENT("myformcancel",$arrevent,true);
                           $rtn.="dhxcancel.window('wincancelcrgket').progressOff();";
                           $rtn.="dhxcancel.attachEvent('onClose',function(win){return true;})";
                           echo $rtn;
                           
            echo "    }
  
                      function fn_grid_loaddata()
                      {
                          var keyword=myFormsearch.getItemValue('txtsearch');
                          url=fn_dhtmlxloadobject('".$this->enc("obj_tra")."','obj=".$this->enc("tra_cancelcrg")."&task=".$this->enc("grdrecord")."&keyword='+keyword);
                          //alert(url);
                          dgresult.clearAndLoad(url);";
            echo "    }
                   </script>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");

            echo "<div id=\"divchecklistdetail\" style=\"height:1020px;\">";
            echo "<table width=\"98%\" border=\"0\">
                      <tr>
                          <td>";
                              $qry="select filepdf from ".DB_APORTIL.".tbl_manualbook where isdeleted=0 and filepdf='cancelcrgket_passenger.pdf'";
                              $filepdf=$this->fn_get_fieldvalue("filepdf",$qry);  
                              $urlmanualbook="<a href='".UPLOAD_DIR."/manualbook/$filepdf"."' target='_blank'><img src='".PATH_THEME."/images/pdf.png' />&nbsp;<b>MANUAL BOOK</b></a>";
                              $url="location.reload();";
                              $myuri=array("<b>CARGO INFORMATION</b>"=>"$url|id='a_newticket'","$urlmanualbook"=>"|id='a_manualbook'");
                              echo $this->MULTIPLE_LINK($myuri);
            echo "       </td>
                      </tr>
                      <tr>
                          <td>
                              <div id=\"divcancelchecklistcontent\" style=\"margin-left:0px;\">
                                  <table width=\"98%\" border=\"0\">
                                      <tr>
                                          <td valign=\"top\">
                                              <table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\">
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <div id=\"divcancelchecklistsearch\" style=\"width:1060px;margin-top:-6px;\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dgresult\" style=\"height:480px;width:1080px;overflow:hidden\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dgresultpaging\" style=\"width:1080px;margin-top:1px\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr><td valign=\"top\"><div id=\"loadergrid\" style=\"z-index:100;position:absolute;margin-top:-320px;margin-left:587px;display:visibility;\">".$this->CREATE_IMAGE("loadergrid.gif")."</div></td></tr>
                                              </table>
                                          </td>
                                      </tr>
                                  </table>
                              </div>
                          </td>
                      </tr>
                  </table>
                  </div>";
                  
            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,0 ischecklist,idship,shipname,tiketnumber,tiketnumberpassenger,voynumber,inctiket,idcategory,cargodescription,passengername,
                        idcardtype,cardtype,cardnumber,dates,idroutestart,routenamestart,idrouteend,
                        routenameend,price,quantity,weight,totalprice,units,modifcount,status
                  from ".DB_APORTIL.".tbl_cargo_tiket
                  where (tiketnumber like '%$keyword%' or voynumber like '%$keyword%' or passengername like '%$keyword%') and isdeleted=0 and status=0
                  order by id desc limit ".TBL_PAGE_LIMIT;

            $rs=$this->selectquery($qry);
            if($rs)
            {
               $i=1;
               while($row=$this->data_row_array($rs))
               {
                   $color='green';
                   if($row["status"]==1){$color='blue';}
                  echo ("<row id='".$row["id"]."'>");
                        print("<cell><![CDATA[".$row["id"]."]]></cell>");
                        print("<cell><![CDATA[".$row["ischecklist"]."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$i."</span>"."]]></cell>");
                        $status=$this->fn_get_fieldvalue("description","select description from ".DB_APORTIL.".tbl_cargo_tiket_status where status='".$row['status']."'");
                        print("<cell><![CDATA["."<span style='color:$color'>".$status."</span>"."]]></cell>");
                        print("<cell><![CDATA[".$row['status']."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["shipname"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["tiketnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["voynumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["passengername"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["cardtype"]."/".$row["cardnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".date("Y-m-d",strtotime($row["dates"]))."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["routenamestart"]." to ".$row['routenameend']."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["cargodescription"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["price"]." \$</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["quantity"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["weight"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["totalprice"]." \$</span>"."]]></cell>");
                  echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                  echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

        }
        
        public function fn_cancelcrg()
        {
            $idcargo=$_REQUEST['idcargo'];
            $remark=$_REQUEST['remark'];
            $qry="update ".DB_APORTIL.".tbl_cargo_tiket set status=-2,remark='$remark',uby='".$_SESSION['nik']."',udt=now() ";
            $qry.="where id='$idcargo'";
            if($this->executequery($qry)){
                $qry="insert into ".DB_APORTIL.".tbl_cargo_tiket_log 
                      set idcargoticket='$idcargo',dates='".date("Y-m-d")."',status=-2,remarkstatus='Cancel Cargo Tiket',
                          remark='$remark',iby='".$_SESSION['nik']."',idt=now()";
                if($this->executequery($qry)){
                    echo "ok|$idcargo";
                }

                
            }
            
                
        }
    }
?>