<?php
session_start();
require("php_con.php");
require("check_user.php");
require("function.php");

if(isset($_POST['save'])){
	setting_update("bill_pint",$_POST['bill_pint']);
	setting_update("bill_title",$_POST['bill_title']);
	setting_update("bill_mobile",$_POST['bill_mobile']);
	setting_update("bill_footer_text",$_POST['bill_footer_text']);
	setting_update("product_suggestions",$_POST['product_suggestions']);
	setting_update("bill_address",$_POST['bill_address']);
	setting_update("bill_lan",$_POST['bill_lan']);
	setting_update("unit_display",$_POST['unit_display']);
	setting_update("ui_type",$_POST['ui_type']);
	setting_update("bill_font_size",$_POST['bill_font_size']);
	setting_update("logo_width",$_POST['logo_width']);
	setting_update("inventory_check",$_POST['inventory_check']);
	setting_update("bill_email",$_POST['bill_email']);
	setting_update("auto_add_item_list",$_POST['auto_add_item_list']);
	setting_update("summery_send_phone",$_POST['summery_send_phone']);
	setting_update("bill_discount_issue",$_POST['bill_discount_issue']);
	setting_update("pos_product_edit",$_POST['pos_product_edit']);
	
	if(empty($_POST['bill_title_display'])){$bill_title_display=0;}else{$bill_title_display=1;}
	if(empty($_POST['bill_address_display'])){$bill_address_display=0;}else{$bill_address_display=1;}
	if(empty($_POST['bill_mobile_display'])){$bill_mobile_display=0;}else{$bill_mobile_display=1;}
	if(empty($_POST['bill_logo_display'])){$bill_logo_display=0;}else{$bill_logo_display=1;}
	if(empty($_POST['bill_email_display'])){$bill_email_display=0;}else{$bill_email_display=1;}
	
	
	setting_update("bill_title_display",$bill_title_display);
	setting_update("bill_address_display",$bill_address_display);
	setting_update("bill_mobile_display",$bill_mobile_display);
	setting_update("bill_logo_display",$bill_logo_display);
	setting_update("bill_email_display",$bill_email_display);
}

if(isset($_POST['save_notification'])){
	setting_update("low_stock_popup",$_POST['low_stock_popup']);
}

if(isset($_POST['save_backup_setting'])){
	setting_update("backup_notification",$_POST['backup_notification']);
}

if(isset($_POST['auto_backup_setting'])){
	setting_update("auto_backup_status",$_POST['auto_backup_status']);
	setting_update("auto_backup_from",$_POST['auto_backup_from']);
	setting_update("auto_backup_to",$_POST['auto_backup_to']);
}

function Export_Database($host,$user,$pass,$name,  $tables=false, $backup_name=false ){
        $mysqli = new mysqli($host,$user,$pass,$name); 
        $mysqli->select_db($name); 
        $mysqli->query("SET NAMES 'utf8'");

        $queryTables    = $mysqli->query('SHOW TABLES'); 
        while($row = $queryTables->fetch_row()) 
        { 
            $target_tables[] = $row[0]; 
        }   
        if($tables !== false) 
        { 
            $target_tables = array_intersect( $target_tables, $tables); 
        }
        foreach($target_tables as $table)
        {
            $result         =   $mysqli->query('SELECT * FROM '.$table);  
            $fields_amount  =   $result->field_count;  
            $rows_num=$mysqli->affected_rows;     
            $res            =   $mysqli->query('SHOW CREATE TABLE '.$table); 
            $TableMLine     =   $res->fetch_row();
            $content        = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";

            for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0) 
            {
                while($row = $result->fetch_row())  
                { //when started (and every after 100 command cycle):
                    if ($st_counter%100 == 0 || $st_counter == 0 )  
                    {
                            $content .= "\nINSERT INTO ".$table." VALUES";
                    }
                    $content .= "\n(";
                    for($j=0; $j<$fields_amount; $j++)  
                    { 
                        $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) ); 
                        if (isset($row[$j]))
                        {
                            $content .= '"'.$row[$j].'"' ; 
                        }
                        else 
                        {   
                            $content .= '""';
                        }     
                        if ($j<($fields_amount-1))
                        {
                                $content.= ',';
                        }      
                    }
                    $content .=")";
                    //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
                    if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
                    {   
                        $content .= ";";
                    } 
                    else 
                    {
                        $content .= ",";
                    } 
                    $st_counter=$st_counter+1;
                }
            } $content .="\n\n\n";
        }
        $backup_name = $backup_name ? $backup_name : "Backup_database_".date("Ymd").".sql";
        //$backup_name = $backup_name ? $backup_name : $name.".sql";
        header('Content-Type: application/octet-stream');   
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"".$backup_name."\"");  
        echo $content; exit;
    }

if(isset($_POST['backup'])){
	
setting_update("next_backup_date",date("Y-m-d",strtotime("+7 days")));

    $mysqlUserName      = $UserName;
    $mysqlPassword      = $Password;
    $mysqlHostName      = $HostName;
    $DbName             = $databaseName;
    $backup_name        = "mybackup.sql";
    $tables             = "*";

Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );	
	
}
?>
<!doctype html>

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>

</head>

<body>
	
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="container mb-4">
<div class="row">

<form method="post" class="col-lg-6 col-sm-12 m-auto card pb-2">
	
<table class="table-sm">
<tbody>
<tr>
  <td colspan="2" align="left" valign="middle"><strong>POS &amp; Bill Setting</strong></td>
  </tr>
<tr><td align="left" valign="middle">Print</td><td align="left" valign="middle">
<select name="bill_pint" required="required" class="form-control form-control-sm" id="bill_pint">
  <option <?php if(setting_view("bill_pint")==1){echo "selected";} ?> value="1">On</option>
  <option <?php if(setting_view("bill_pint")==0){echo "selected";} ?> value="0">Off</option>
</select>	
</td></tr>
	
<tr><td align="left" valign="middle">Product Suggestions</td><td align="left" valign="middle">
  <select name="product_suggestions" required="required" class="form-control form-control-sm" id="product_suggestions">
    <option <?php if(setting_view("product_suggestions")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("product_suggestions")==0){echo "selected";} ?> value="0">Off</option>
  </select>	
</td></tr>
	
<tr>
  <td align="left" valign="middle">Automatically add item list</td>
  <td align="left" valign="middle"><select name="auto_add_item_list" required="required" class="form-control form-control-sm" id="auto_add_item_list">
    <option <?php if(setting_view("auto_add_item_list")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("auto_add_item_list")==0){echo "selected";} ?> value="0">Off</option>
  </select></td>
</tr>
<tr><td align="left" valign="middle">Language</td><td align="left" valign="middle">
  <select name="bill_lan" required="required" class="form-control form-control-sm" id="bill_pint">
    <option <?php if(setting_view("bill_lan")==1){echo "selected";} ?> value="1">Sinhala</option>
    <option <?php if(setting_view("bill_lan")==0){echo "selected";} ?> value="0">English</option>
    </select>	
</td></tr>
<tr>
  <td align="left" valign="middle">Unit Display</td>
  <td align="left" valign="middle"><select name="unit_display" required="required" class="form-control form-control-sm" id="bill_lan">
    <option <?php if(setting_view("unit_display")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("unit_display")==0){echo "selected";} ?> value="0">Off</option>
    </select></td>
</tr>
<tr>
  <td align="left" valign="middle">Inventory inspection before sale</td>
  <td align="left" valign="middle"><select name="inventory_check" required="required" class="form-control form-control-sm" id="inventory_check">
    <option <?php if(setting_view("inventory_check")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("inventory_check")==0){echo "selected";} ?> value="0">Off</option>
  </select></td>
</tr>
<tr>
  <td align="left" valign="middle">Bill Discount Issue</td>
  <td align="left" valign="middle"><select name="bill_discount_issue" required="required" class="form-control form-control-sm" id="bill_discount_issue">
    <option <?php if(setting_view("bill_discount_issue")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("bill_discount_issue")==0){echo "selected";} ?> value="0">Off</option>
  </select></td>
</tr>
<tr>
  <td align="left" valign="middle">POS Product Edit</td>
  <td align="left" valign="middle"><select name="pos_product_edit" required="required" class="form-control form-control-sm" id="pos_product_edit">
    <option <?php if(setting_view("pos_product_edit")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("pos_product_edit")==0){echo "selected";} ?> value="0">Off</option>
  </select></td>
</tr>
<tr>
  <td align="left" valign="middle">Font Size (px)</td>
  <td align="left" valign="middle">
	<input name="bill_font_size" type="text" class="form-control form-control-sm" id="bill_font_size" value="<?php echo setting_view("bill_font_size"); ?>">
	</td>
</tr>
<tr hidden="yes">
  <td align="left" valign="middle">User Interface</td>
  <td align="left" valign="middle"><select name="ui_type" required="required" class="form-control form-control-sm" id="ui_type">
    <option <?php if(setting_view("ui_type")==1){echo "selected";} ?> value="1">GUI</option>
    <option <?php if(setting_view("ui_type")==0){echo "selected";} ?> value="0">Normal</option>
  </select></td>
</tr>
<tr>
  <td align="left" valign="middle"><label>Logo Image Width (%)</label></td>
  <td align="left" valign="middle"><input name="logo_width" type="number" class="form-control form-control-sm" id="logo_width" max="100" min="0" value="<?php echo setting_view("logo_width"); ?>"></td>
</tr>
<tr>
  <td align="left" valign="middle"><label><input name="bill_logo_display" type="checkbox" id="bill_logo_display" <?php if(setting_view("bill_logo_display")==1){echo "checked";} ?>> Bill Logo Image</label></td>
  <td align="left" valign="middle"><img src="image/logo.png" style="width: 80px; height: 80px; object-fit: contain; border: 1px solid #CCCCCC; padding: 5px;"></td>
</tr>
<tr>
  <td align="left" valign="middle"><label><input name="bill_title_display" type="checkbox" id="bill_title_display" <?php if(setting_view("bill_title_display")==1){echo "checked";} ?>> Bill Title</label></td>
  <td align="left" valign="middle"><input name="bill_title" type="text" required="required" class="form-control form-control-sm" id="bill_title" value="<?php echo setting_view("bill_title"); ?>"></td>
</tr>
<tr>
  <td align="left" valign="middle"><label><input name="bill_address_display" type="checkbox" id="bill_address_display" <?php if(setting_view("bill_address_display")==1){echo "checked";} ?>> Bill Address</label></td>
  <td align="left" valign="middle"><textarea name="bill_address" rows="4" class="form-control form-control-sm" id="bill_address"><?php echo setting_view("bill_address"); ?></textarea></td>
</tr>
<tr>
  <td align="left" valign="middle"><label><input name="bill_mobile_display" type="checkbox" id="bill_mobile_display" <?php if(setting_view("bill_mobile_display")==1){echo "checked";} ?>> Bill Mobile</label></td>
  <td align="left" valign="middle"><input name="bill_mobile" type="text" class="form-control form-control-sm" id="bill_mobile" value="<?php echo setting_view("bill_mobile"); ?>"></td>
</tr>
<tr>
  <td align="left" valign="middle"><label><input name="bill_email_display" type="checkbox" id="bill_email_display" <?php if(setting_view("bill_email_display")==1){echo "checked";} ?>> Bill Email</label></td>
  <td align="left" valign="middle"><input name="bill_email" type="email" class="form-control form-control-sm" id="bill_email" value="<?php echo setting_view("bill_email"); ?>"></td>
</tr>
<tr>
  <td align="left" valign="middle">Report Send Number</td>
  <td align="left" valign="middle"><input name="summery_send_phone" <?php if(!$sms_send==1){echo "readonly";} ?> type="tel" class="form-control form-control-sm" id="summery_send_phone" value="<?php echo setting_view("summery_send_phone"); ?>"></td>
</tr>
<tr>
  <td align="left" valign="middle">Bill Footer Text</td>
  <td align="left" valign="middle"><textarea name="bill_footer_text" rows="4" required="required" class="form-control form-control-sm" id="bill_footer_text"><?php echo setting_view("bill_footer_text"); ?></textarea></td>
</tr>
<tr>
  <td align="left" valign="middle">&nbsp;</td>
  <td align="right" valign="middle"><button name="save" type="submit" class="btn btn-sm btn-info mt-2">Save Setting</button></td>
</tr>
</tbody>
</table>

</form>
	
</div>
	
<div class="row mt-4">

<form method="post" class="col-lg-6 col-sm-12 m-auto card pb-2">
	
<table class="table-sm">
<tbody>
<tr>
  <td colspan="2" align="left" valign="middle"><strong>Notification setting</strong></td>
</tr>
	
<tr>
  <td align="left" valign="middle">Low Stock Popup</td>
  <td align="left" valign="middle"><select name="low_stock_popup" required="required" class="form-control form-control-sm" id="low_stock_popup">
    <option <?php if(setting_view("low_stock_popup")==1){echo "selected";} ?> value="1">On</option>
    <option <?php if(setting_view("low_stock_popup")==0){echo "selected";} ?> value="0">Off</option>
  </select></td>
</tr>
	
<tr>
  <td align="left" valign="middle">&nbsp;</td>
  <td align="right" valign="middle"><button name="save_notification" type="submit" class="btn btn-sm btn-info mt-2">Save Setting</button></td>
</tr>
	
</tbody>
</form>
	
</table>
	
</div>
	
<div class="row mt-4">

<form method="post" class="col-lg-6 col-sm-12 m-auto card pb-2">
	
<table class="table-sm">
<tbody>
<tr>
  <td colspan="2" align="left" valign="middle"><strong>System Backup</strong></td>
</tr>
	
<tr><td align="left" valign="middle">Backup notification every 7 days</td><td align="left" valign="middle">
<select name="backup_notification" required="required" class="form-control form-control-sm" id="backup_notification">
  <option <?php if(setting_view("backup_notification")==1){echo "selected";} ?> value="1">On</option>
  <option <?php if(setting_view("backup_notification")==0){echo "selected";} ?> value="0">Off</option>
</select>	
</td></tr>
	
<tr>
  <td align="left" valign="middle">&nbsp;</td>
  <td align="right" valign="middle"><button name="save_backup_setting" type="submit" class="btn btn-sm btn-info mt-2">Save Setting</button></td>
</tr>
	
</tbody>
</form>
	
<form method="post">
<tbody>
<tr>
  <td align="left" valign="middle">&nbsp;</td>
  <td align="right" valign="middle"><button name="backup" type="submit" class="btn btn-sm btn-success mt-2">Backup Data</button></td>
</tr>
</tbody>
</form>
	
</table>
	
</div>
	
<div class="row mt-4">

<form method="post" class="col-lg-6 col-sm-12 m-auto card pb-2" autocomplete="off">
	
<table class="table-sm">
<tbody>
<tr>
  <td colspan="2" align="left" valign="middle"><strong>Auto System Backup</strong><br><i>(• last '/' character remove • User location separate '/' characters)</i></td>
</tr>
	
<tr><td align="left" valign="middle" style="width: 40%;">Backup every 1 days</td><td align="left" valign="middle">
<select name="auto_backup_status" required="required" class="form-control form-control-sm" id="auto_backup_status">
  <option <?php if(setting_view("auto_backup_status")==1){echo "selected";} ?> value="1">On</option>
  <option <?php if(setting_view("auto_backup_status")==0){echo "selected";} ?> value="0">Off</option>
</select>	
</td></tr>
	
<tr><td align="left" valign="middle">From Location</td><td align="left" valign="middle">
<input name="auto_backup_from" type="url" required="required" class="form-control form-control-sm" id="auto_backup_from" value="<?php echo setting_view("auto_backup_from"); ?>">
</td></tr>
	
<tr><td align="left" valign="middle">To Location</td><td align="left" valign="middle">
<input name="auto_backup_to" type="url" required="required" class="form-control form-control-sm" id="auto_backup_to" value="<?php echo setting_view("auto_backup_to"); ?>">
</td></tr>
	
<tr>
  <td align="left" valign="middle">&nbsp;</td>
  <td align="right" valign="middle"><button name="auto_backup_setting" type="submit" class="btn btn-sm btn-info mt-2">Save Setting</button></td>
</tr>
	
</tbody>
</form>
	
</table>
	
</div>
	
</div>



</body>
</html>
<?php @mysqli_close($connation); ?>