<?php
    class TRA_CANCELTIC extends TRA
    {
        public function tra_canceltic_javainit()
        {

        }

        public function fn_canceltic_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_canceltic();
                break;

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

                default:
                    $this->fn_default_canceltic();
                break;
            }
        }
        public function fn_default_canceltic()
        {
            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_idticketing"),
                                                                      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_cancelticketing()'>Cancel Ticketing</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"=>"16");
                            $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,152,left,int,false",
                                                  "Ticket Number"=>"center,ro,194,center,str,false",
                                                  "tiketNumber"=>"center,ro,194,center,str,false,true",
                                                  "No. Sequence&nbsp;"=>"right,ro,110,right,int,false",
                                                  "Ship Number"=>"left,ro,238,left,str,false",
                                                  "Mrs/ Mr"=>"left,ro,206,left,str,false",
                                                  "Picture"=>"left,ro,206,left,str,false",
                                                  "Dates"=>"center,ro,120,center,date,false",
                                                  "From"=>"center,ro,112,center,str,false",
                                                  "To"=>"center,ro,112,center,str,false",
                                                  "Seat Number"=>"center,ro,90,center,str,false",
                                                  "Quantity&nbsp;"=>"right,ro,110,right,int,false",
                                                  "Price&nbsp;"=>"right,ro,120,right,int,false",
                                                  "Baggage&nbsp;"=>"right,ro,136,right,int,false",
                                                  "Price&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 idticketing=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_idticketing',idticketing+'|'+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_cancelticketing()
                      {";
                           //var idticketing=myFormsearch.getItemValue('txt_idticketing');                            
                           //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 idticketing=myFormsearch.getItemValue('txt_idticketing');";
                           $rtn.="var dhxcancel=new dhtmlXWindows();";
                           $rtn.="var wincancelticket=dhxcancel.createWindow(\"wincancelticket\",0,0,410,190);";            
                           $rtn.="dhxcancel.window('wincancelticket').progressOn();";
                           $rtn.="wincancelticket.setText('Confirmation Ticket Cancel');";
                           $rtn.="dhxcancel.window('wincancelticket').denyResize();";
                           $rtn.="dhxcancel.window('wincancelticket').setModal(true);";
                           $rtn.="dhxcancel.window('wincancelticket').bringToTop();";
                           $rtn.="dhxcancel.window('wincancelticket').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"=>"btncancelticketing","value"=>"Cancel Booking","offsetLeft"=>"0","offsetTop"=>"0",
                                                                                          "className"=>"button_delete")
                                                           )
                                             )
                                     );
                           $formdata=json_encode($formdata);
                           $rtn.="var myformcancel=wincancelticket.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_idticketing').split('|');";
                           $onbuttonclickbtncancel.="var idticketing=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_canceltic")."&task=".$this->enc("canceltic")."&idticketing='+idticketing+'&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("btncancelticketing"=>"$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('wincancelticket').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_canceltic")."&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='cancelticket_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>PASSENGER TICKET 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,tiketnumber,inctiket,shipname,shipnumber,passengername,picdata,dates,idroutestart,
                         routenamestart,idrouteend,routenameend,seatnumber,quantity,price,baggageinkg,pricebaggage,modifcount,status
                  from ".DB_APORTIL.".tbl_passenger_tiket
                  where (tiketnumber 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_passenger_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:black'>".$row["shipname"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["tiketnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA[".$row["tiketnumber"]."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["inctiket"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["shipnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["passengername"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["picdata"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".date("Y-m-d",strtotime($row["dates"]))."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["routenamestart"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["routenameend"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["seatnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["quantity"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>"."\$".$row["price"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>".$row["baggageinkg"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:black'>"."\$".$row["pricebaggage"]."</span>"."]]></cell>");
                  echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                  echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

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

                
            }
            
                
        }
    }
?>