<?php
    class TRA_NEWTICKETC extends TRA
    {
        public function tra_newticketc_javainit()
        {

        }

        public function fn_newticketc_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_newticketc();
                break;

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

                default:
                    $this->fn_default_newticketc();
                break;
            }
        }
        
        public function fn_default_newticketc()
        {
            echo $this->FN_CREATEHEADER();
            $tiketnumber=$_REQUEST['tiketnumber'];
            $childspassenger=$_REQUEST['childspassenger'];
            $idtiketpassenger=$this->fn_get_fieldvalue("id","select id from ".DB_APORTIL.".tbl_passenger_tiket where tiketnumber='$tiketnumber'");
            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,dgchild;
                       var rowid=0;
                       function initObject()
                       {";
                            $formdata=array(
                                            array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                            array("type"=>"block","width"=>"500","label"=>"",
                                                "list"=>array(
                                                        array("type"=>"hidden","name"=>"txt_tiketnumber","value"=>$tiketnumber),
                                                        array("type"=>"hidden","name"=>"txt_idtiketpassenger","value"=>$idtiketpassenger),
                                                        array("type"=>"label","name"=>"lblinfoselect","label"=>"Tiket Number <b>$tiketnumber</b>","className"=>"","labelWidth"=>"280","offsetTop"=>"-5","offsetLeft"=>"0"),
                                                        array("type"=>"newcolumn"),
                                                        array("type"=>"button","name"=>"btnsetchild","value"=>"Set Child","className"=>"button_saveas","offsetLeft"=>"10","disabled"=>"0"),
                                                
                                                )
                                            )
                            );
                            $this->FN_DHTMLXFORM("myForm","divfrmchild",$formdata,true);

                            $arrevent=array(
                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                        "switch"=>array(
                                            "btnsetchild"=>"fn_setchild()"
                                        )
                                )
                            );
                            $this->FN_DHTMLXFORMEVENT("myForm",$arrevent);
                            
                            $footerevent="<div style='height:15pt;padding-top:3pt;font-size:10pt'>";
                            // $footerevent.="<a href='javascript:void(0)' onClick='fn_grid_newrow()'><span>Tambah Record</span></a>&nbsp;&nbsp;";
                            $footerevent.="<a href='javascript:void(0)' onClick='fn_grid_new()' style='font-size:16px'><span>Add</span></a>&nbsp;&nbsp;";
                            $footerevent.="<a href='javascript:void(0)' onClick='fn_grid_delrow()' style='font-size:16px'><span>Delete Record</span></a>&nbsp;&nbsp;";
                            $footerevent.="</div>";
                            $grdinit=array("gridname"=>"dgchild","divtarget"=>"div_dgchild","divpaging"=>"div_dgchildpaging","paginglimit"=>5,
                                            "showloader"=>"true","footer"=>"$footerevent","footerspan"=>"7","footeralign"=>"left",
                                            "enableMultiline"=>"true","setdateformat"=>"%Y-%m-%d"
                            );
                            $gridproperties=array("grid"=>"right,ro,96,right,str,false,true",
                                                  "id&nbsp;&nbsp;"=>"left,ro,210,left,str,false,true",
                                                  "Names&nbsp;&nbsp;"=>"left,ed,210,left,str,false",
                                                  "Birthofdate&nbsp;&nbsp;"=>"center,dhxCalendarA,220,center,str,false",
                                                  "Ages&nbsp;"=>"center,ed,220,center,str,false",
                                                  "Notes&nbsp;"=>"left,ed,220,left,str,false",
                                                  "idtiketpassenger"=>"left,ro,110,left,int,false,true",
                            );
                            $this->FN_DHTMLXGRIDVIEW($grdinit,$gridproperties,"bricks","","fn_grid_loaddata()");
                            echo "dgchild.setStyle('font-size:14px','font-size:14px','');";
                            echo "dgchild.setDateFormat('%Y-%m-%d', '%Y-%m-%d');";

                            $eventrowselect="rowid=(dgchild.cells(rId,1).getValue());";
                            // $eventxle="rwcount=dgtrasoap.getRowsNum();";
                            // $eventxle.="var isvitanam=0;";
                            // $eventxle.="if(rwcount>0){isvitanam=1;}";
                            // $eventxle.="window.parent.fn_setvalidationheader('isvitanam',isvitanam);";
                            // $eventxle.="window.parent.fn_setvalidationheader('iscppt',isvitanam);";
                            // $eventxle.="for(i=1;i<=rwcount;i++){";
                            // $eventxle.="    var insertby=dgtrasoap.cells(i,11).getValue();";
                            // $eventxle.="    if(trim(insertby)!==trim('".$_SESSION['nik']."')){";
                            // $eventxle.="        dgtrasoap.setRowTextStyle(i, \"color:grey;\");";
                            // $eventxle.="    }";
                            // $eventxle.="}";
                            $oncalendarshow="var dhxcalendar=myCal;";
                            $oncalendarshow.="";
                            $arrevent=array(                                                
                                                array("eventname"=>"onRowSelect","eventparam"=>"rId",
                                                            "statement"=>"$eventrowselect"
                                                ),
                                                array("eventname"=>"onCalendarShow","eventparam"=>"myCal,row_id,col_ind",
                                                            "statement"=>"$oncalendarshow"
                                                ),
                                                // array("eventname"=>"onXLE","eventparam"=>"grid_obj,rcount",
                                                //             "statement"=>"$eventxle"
                                                // )
                                        );
                            $this->FN_DHTMLXGRIDVIEWEVENT("dgchild",$arrevent);

                            // if($childspassenger){
                            //     echo "fn_grid_default();";
                            // }
                            
            echo "      }
                        
                        
                        function fn_grid_new(){
                            var idx=rowid;
                            var childspassenger='".$childspassenger."';
                            var rowcount=dgchild.getRowsNum()+1;
                            var idtiketpassenger=myForm.getItemValue('txt_idtiketpassenger');
                            var gridid=dgchild.uid();";                
            echo "          dgchild.addRow(rowcount,[gridid,0,'','".date("Y-m-d")."','','',idtiketpassenger]);                            
                            
                        }

                        function fn_grid_delrow(){
                            dhtmlx.confirm({
                                title:'Confirmation',text:'Are you sure for delete this row?',type:'confirm',
                                callback:function(result){
                                    if(result)
                                    {
                                        dgchild.deleteSelectedRows();
                                        if(rowid==0){
                                            dgchild.deleteSelectedRows();
                                        }
                                        else{
                                            var delchild=ajaxuridhtmlx('".$this->enc("obj_tra")."','obj=".$this->enc("tra_newticketc")."&task=".$this->enc("delchilds")."&id='+rowid).split('|');
                                            if(delchild[0]=='ok'){
                                                fn_dhtmlxmessage('','Data has been deleted. Thank you');
                                                // fn_grid_loaddata();
                                            }
                                            else{
                                                fn_dhtmlxmessage('error',delchild[1]);
                                            }
                                        }                    
                                    }
                                }
                            });
                            
                            
                        }
                        
                        function fn_grid_default(){
                            var idx=rowid;
                            var childspassenger='".$childspassenger."';
                            var rowcount=dgchild.getRowsNum()+1;
                            var idtiketpassenger=myForm.getItemValue('txt_idtiketpassenger');
                            if(childspassenger){
                                var sp=childspassenger.split(';');
                                for(i=0;i<sp.length;i++)
                                {
                                    var gridid=dgchild.uid();
                                    var spchild=sp[i].split('|');
                                    var idchild=spchild[0];
                                    var names=spchild[1];
                                    var birthofdate=spchild[2];
                                    var ages=spchild[3];
                                    var notes=spchild[4];
                                    var idtiketpassenger=spchild[5];
                                    
                                    dgchild.addRow((i+1),[gridid,idchild,names,birthofdate,ages,notes,idtiketpassenger]);                            
                                }
                            }
                        }

                        function fn_setchild(){
                            var rowcount=dgchild.getRowsNum();
                            // alert(rowcount);
                            if(rowcount<=0)
                            {
                                fn_dhtmlxmessage('error','Please add child first!');
                                return false;
                            }
                            var valchilds='';
                            for(i=1;i<=rowcount;i++)
                            {
                                var idchilds=dgchild.cells(i,1).getValue();
                                var names=dgchild.cells(i,2).getValue();
                                var birthofdate=dgchild.cells(i,3).getValue();
                                var ages=dgchild.cells(i,4).getValue();
                                var notes=dgchild.cells(i,5).getValue();
                                var idtiketpassenger=dgchild.cells(i,6).getValue();
                                
                                valchilds+=idchilds+'|'+names+'|'+birthofdate+'|'+ages+'|'+notes+'|'+idtiketpassenger+';';
                            }
                            valchilds=valchilds.substring(0,valchilds.length-1);
                            window.parent.fn_closechildren(valchilds);
                        }

                       function fn_grid_loaddata()
                       {
                            var idtiketpassenger=myForm.getItemValue('txt_idtiketpassenger');
                            url=fn_dhtmlxloadobject('".$this->enc("obj_tra")."','obj=".$this->enc("tra_newticketc")."&task=".$this->enc("grdrecord")."&idtiketpassenger='+idtiketpassenger);
                            dgchild.clearAndLoad(url);";
            echo "     }
                       
                   </script>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");

           echo "<table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\" style=\"\">  
                    <tr>
                        <td valign=\"top\">
                            <div id=\"divfrmchild\" style=\"width:560px;margin-top:-6px;\"></div>
                        </td>
                    </tr>   
                    <tr>
                        <td valign=\"top\">
                            ".$this->CREATE_LINK('window.location.reload();','REFRESH','javascript:void(0)','_self','','','','')."
                        </td>
                    </tr>               
                    <tr>
                        <td>
                            <div id=\"div_dgchild\" style=\"height:360px;width:99%;overflow:hidden\"></div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div id=\"div_dgchildpaging\" style=\"width:740px;margin-top:1px\"></div>
                        </td>
                    </tr>
                    <tr><td valign=\"top\"><div id=\"loadergrid\" style=\"z-index:100;position:absolute;margin-top:-220px;margin-left:50%;display:visibility;\">".$this->CREATE_IMAGE("loadergrid.gif")."</div></td></tr>
                    <tr>
                        <td valign=\"top\" align=\"left\">
                            <table>
                                <tr>
                                    <td style='color:red;font-weight:bold;'>Notes</td>
                                    <td>:</td>
                                    <td>- Double click row for edit</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>";

            echo $this->FN_CREATEFOOTER();

        }

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

            $qry="select * from ".DB_APORTIL.".tbl_passenger_childs
                  where idtiketpassenger='$idtiketpassenger' 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[".$i."]]></cell>");
                        print("<cell><![CDATA[".$row["id"]."]]></cell>");
                        print("<cell><![CDATA[".$row["names"]."</span>"."]]></cell>");
                        print("<cell><![CDATA[".date("Y-m-d H:i:s",strtotime($row["birthofdate"]))."]]></cell>");
                        print("<cell><![CDATA[".$row["ages"]."</span>"."]]></cell>");
                        print("<cell><![CDATA[".$row["notes"]."</span>"."]]></cell>");
                        print("<cell><![CDATA[".$row["id"]."</span>"."]]></cell>");
                  echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                //   echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
                  echo "error at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

        }

        public function fn_delchilds(){
            $id=$_REQUEST['id'];
            $qry="update ".DB_APORTIL.".tbl_passenger_childs set isdeleted=1,remarkdeleted='Cancel by user ',dby='".$_SESSION['nik']."',ddt=now()
                  where id='$id'";
            if($this->executequery($qry)){
                echo "ok|";
            }
            else{
                echo "failed|Failed remove child";
            }

        }
        
    }
?>