<?php
    
    if($_REQUEST['ajax']) {
             
            switch($_REQUEST['code']) {
                
                case "inventory":
                    echo GetGridUser('inventory');
                case "setting":
                    echo GetGridUser('setting');		
                    break;
                                     
                // Clear Database Transaksi 
                case "clear_db_transaksi":
                    ?>
                    var result = confirm('Anda Yakin Hapus Data Transaksi ? '); 
                    if (result){
                        <?php
                        $ssql=" SELECT table_schema,table_name
                                        FROM information_schema.tables
                                        WHERE table_schema=database() AND table_name LIKE 'tx%';";
                        $rs1=mysql_query($ssql);

                        while($r1=mysql_fetch_assoc($rs1)) {
                            $ssql2="DELETE FROM {$r1['table_name']}; ";
                            mysql_query($ssql2);
                        }

                        echo "alert('Data semua transaksi sudah berhasil dihapus...');";
                    ?>
                    }
                    <?php
                    break;
                    
                // Clear Database Master 
                case "clear_db_master":
                    // not included
                    // 'tlcustomer','tllocation','tlsupplier','tlsupplier_det','tlwarehouse','tlstock_det',
                    
                    // need reset
                    // tlbranch, tlbrand, tlcurrency, tlstock_hdr
                    // tlbuying_price, tlcustomer, tlsupplier, tllocation, tlwarehouse
                    ?>
                    var result = confirm('Anda Yakin Hapus Data Master ? '); 
                    if (result){
                        <?php         
                        $ssql=" SELECT table_schema,table_name
                                FROM information_schema.tables
                                WHERE table_schema=DATABASE() AND
                                table_name LIKE 'tl%'
                                AND  table_name NOT IN        
                                ('tlaplication','tlasset_category','tlbranch','tlbranch_group','tlbranch_type',
                                 'tlcoa','tlcoa_group','tlcoa_setting','tlcoa_subgroup','tlcurrency_hdr',
                                 'tlcustomer_type','tldepartment','tldocument_iso','tleducation',
                                 'tlgroup_penggajian','tlleave_type','tlrating','tlreligion',
                                 'tlmenu','tlother_cost','tlovertime','tlpph21','tlsalary','tlsalary_category','tlshift','tlday',
                                 'tlsecurity','tlsecurity_setting','tltype_form','tluom',
                                 'tlreport','tlstock_category','tlperiode',
                                 'tluser','tluser_access','tluser_access_setting','tluser_group','tluser_group_access',
                                 'tlstock_type','tlstock_type_bahan_baku','tlstock_type_setengah_jadi','tlweight');";
                        $rs1=mysql_query($ssql);

                        while($r1=mysql_fetch_assoc($rs1)) {
                            $ssql2="DELETE FROM {$r1['table_name']}; ";
                            mysql_query($ssql2);
                        }
                        
                        echo "alert('Data semua master sudah berhasil dihapus...');";
                        ?>
                    }
                    <?php    
                    break;    
                    
                // Clear Partial Database Master 
                case "clear_partial_db_master":
                    // not included
                    // 'tlcustomer','tllocation','tlsupplier','tlsupplier_det','tlwarehouse','tlstock_det',
                    ?>
                    var result = confirm('Anda Yakin Hapus Data Master ? '); 
                    if (result){
                        <?php         
                        $ssql=" SELECT table_schema,table_name
                                FROM information_schema.tables
                                WHERE table_schema=DATABASE() AND
                                table_name LIKE 'tl%'
                                AND  table_name NOT IN        
                                ('tlaplication','tlasset_category','tlbranch','tlbranch_group','tlbranch_type','tlreport','tlstock_category',
                                 'tlcoa','tlcoa_group','tlcoa_setting','tlcoa_subgroup','tlkategoriperkiraan','tlnoperkiraandetail','tlperiode',
                                 'tluser','tluser_access','tluser_access_setting',
                                 'tlstock_hdr',
                                 'tlcustomer_type','tldepartment','tlgroup_penggajian','tlleave_type','tlrating','tlreligion',
                                 'tlmenu','tlother_cost','tlovertime','tlpph21','tlsalary','tlsalary_category','tlshift','tlday',
                                 'tlsecurity','tlsecurity_setting','tltype_form','tluom',
                                 'tluser_group','tluser_group_access','tleducation','tlgroup_pengajian',
                                 'tlstock_type','tlstock_type_bahan_baku','tlstock_type_setengah_jadi','tlweight');";
                        $rs1=mysql_query($ssql);

                        while($r1=mysql_fetch_assoc($rs1)) {
                            $ssql2="DELETE FROM {$r1['table_name']}; ";
                            mysql_query($ssql2);
                        }
                        
                        echo "alert('Data semua transaksi sudah berhasil dihapus...');";
                        ?>
                    }
                    <?php    
                    break;        
            }
            die("");
        }

    // Script Ajax di form bersangkutan
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            
            
            
        }
    }
    
    
    function GetGridUser($n) {
        $x='';
        $ftusername='';

        $ssql="SELECT ftusername FROM tluser WHERE fnid='{$_REQUEST['id']}'";
        $rs=mysql_query($ssql) or die($ssql);
        $r=mysql_fetch_assoc($rs);
        $ftusername=$r['ftusername'];

        $ssql="SELECT a.fx, A.ftsecurity_code as fnsecurity_access,IFNULL(B.ftusername,'{$ftusername}') AS ftusername,a.ftmodule_code, A.ftparent_code as ftsecurity_description,a.ftsecurity_name as ftsecurity_subdescription,IFNULL(B.fbaccess,0) AS fnsecurity,
                a.ftmenu_code
                FROM (
                        SELECT 0 as fx,ftsecurity_code,ftmodule_code,ftparent_code,ftsecurity_name,ftmenu_code, fnlevel from tlsecurity_setting WHERE ftmodule_code='{$n}'
                ) A
                LEFT JOIN tluser_access_setting B ON A.ftsecurity_code=B.ftsecurity_code AND (B.ftusername ='{$ftusername}' OR B.ftusername IS NULL)
                WHERE ftmodule_code='{$n}' ";

        // echo ($ssql);
        $rs=mysql_query("select distinct ftsecurity_description, fx, ftmenu_code from ($ssql) m order by fx ASC, ftmenu_code asc, fnsecurity_access asc") or die($ssql) ;

        $x.= "<table class='ewTable' id='TransactionDetail{$n}' name='TransactionDetail{$n}' border='0' width='100%' cellspacing='0' celpadding='0'>
                <tr class='is_edit_header_row' width='100%'>
                    <th valign=top width=30 align=center>No <input type='hidden' name='ftusername' value='{$ftusername}'></th>
                    <th valign=top align=center width='31%'>Keterangan</th>
                    <th valign=top width=30 align=center>No</th>
                    <th valign=top align=center width='31%'>Keterangan</th>
                    <th valign=top width=30 align=center>No</th>
                    <th valign=top align=center width='31%'>Keterangan</th>
                </tr>";

        $i=0;
        $j=0;
        $row1=1;

        while($r = mysql_fetch_assoc($rs))
        {
            if ($i%2) {
                    $class1="ewTableRow";
                    $class2="ISGridTDeven";
            } else {
                    $class1="ISGridTRodd";
                    $class2="ISGridTDodd";
            }
            $j++;

            if($row1==1) {
                $x.= "<tr class='$class1'>";
            }
            
            $x.="<td class='$class2' valign=top>{$j}</td>
                 <td class='$class2' valign=top>
                    <b>".ucwords(str_replace("_"," ",$r['ftsecurity_description'])) ."</b>";

            $rs1=mysql_query($ssql." and A.ftparent_code='".$r['ftsecurity_description']."' order by fx ASC, ftmenu_code asc, fnsecurity_access asc" ) or die("m");

            $i=0;

            while($r1 = mysql_fetch_assoc($rs1)) {
                if($i==0) {
                     $namex="cAll_{$r1['fnsecurity_access']}";
                     $checkx= " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <input type=checkbox id='{$namex}' name='{$namex}' value='0' onClick=\"CheckSecurityAll('{$namex}')\">ALL";
                     $x.="$checkx<table class='ewTable'  border='0'  cellspacing='0' celpadding='0'  width='100%'>";

                }

                 $i++;
                    if($r1['fnsecurity']==0){
                            $c="";
                    }
                    else {
                            $c=" checked ";
                    }
                $x.="<tr  class='$class1'>";
                $x.="<td class='$class2'>";
                $x.="   <input type=hidden name=fnS[] value='{$r1['fnsecurity_access']}'>
                        <input type=checkbox id='{$namex}_{$i}' name='c{$r1['fnsecurity_access']}' value='1' $c ></td>
                     <td width='100%'>".ucwords(str_replace("_"," ",$r1['ftsecurity_subdescription'])) ." ";
                $x.="</td>";
                $x.="</tr>";
                            
             }

             $x.="</table>";
             $x.="<input type=hidden name='{$namex}_count'  id='{$namex}_count' value={$i}>";
                         $x.="</td>";
                             if((mysql_num_rows($rs)==$j)||($row1==3)) {
                                    if(mysql_num_rows($rs)==$j) {
                                            if($row1==1) {
                                                    $x.="<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
                                            }
                                            elseif($row1==2) {
                                                    $x.="<td>&nbsp;</td><td>&nbsp;</td>";
                                            }
                                    }
                                    $x.="</tr>
                                         <tr><td colspan=6>&nbsp;</td></tr>";
                             }
                             if($row1==3) $row1=0;
                             $row1++;
                 $i++;
        }
        $x.="</table>";

        return $x;

    }
    
    function GetUser() {

        $x.="<link type='text/css' href='jquery/themes/base/ui.all.css' rel='stylesheet' />
            <script type='text/javascript' src='jquery/jquery-1.3.2.js'></script>
            <script type='text/javascript' src='jquery/ui/ui.core.js'></script>
            <script type='text/javascript' src='jquery/ui/ui.tabs.js'></script>
            <script type='text/javascript'>
            $(function() {
                    $('#tabs').tabs();
            });
            </script> 

            <div class='demo'> 
                <div id='tabs'>
                        <ul>
                                <li><a href='#tabs-1'>Inventory</a></li>
                                <li><a href='#tabs-2'>Setting</a></li>
                        </ul>
                       

                        <div id='tabs-1'>
                        ".GetGridUser('inventory')."
                        </div>

                        <div id='tabs-2'>
                        ".GetGridUser('setting')."
                        </div>
                        
                </div>

                </div><!-- End demo --> ";
        return $x;

    }

    $objAxpDB->FormName("System Setting");
    $objAxpDB->FormSQL("SELECT ftusername,fnstatus FROM tluser");
    $objAxpDB->FormCaption("User2");
    $objAxpDB->FormPosition("TL");
    
    //$ajaxClearDB="OnClick=\"var result = confirm('Anda Yakin Hapus Data Transaksi ? '); if (result) { ajax_do('system.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=clear_db_transaksi'); } \"";
    $ajaxClearDB="OnClick=\"ajax_do('setting.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=clear_db_transaksi');\"";
    $ajaxClearDBMaster="OnClick=\"ajax_do('setting.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=clear_db_master');\"";
    $objAxpDB->FormLayout(0,"<input type = 'button' value='Clear Data Transaksi' {$ajaxClearDB}> <BR> <input type = 'button' value='Clear Data Master' {$ajaxClearDBMaster} > ");
    
    $objAxpDB->GridSQL(0, "SELECT fnid FROM tluser");
    $m=GetUser();
    #die($m);
    $objAxpDB->GridReplace(0, $m);

    $objAxpDB->SetStatus("id");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("tutup");

    }
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");

    }
    elseif ($objAxpDB->GetStatus("id") != 0) {
            $objAxpDB->ButtonShow("update, tutup");
            $objAxpDB->FormLocked("1, 0, 0");
    }
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();


