<?php
// DISPLAY ALL ERRORS
//error_reporting(E_ALL);
ini_set('display_errors', 0);

// FOR DEBUG ONLY
if (isset($_GET['php']) and $_GET['php'] == 'info') {
    die(phpinfo());
}

// SEARCH HELPER FUNCTION
function contains(array $array, $string) {
    $count = 0;
    foreach ($array as $value) {
        if (false !== stripos($string, $value)) {
            ++$count;
        }
    }
    return $count == count((array)$array);
}

// START & CONFIG
ob_start();
require 'setup/config.php';
include_once('lang/main.php');

// INI VARs
$found = 0;                     // MARKER HOW MANY HITS FOUND A SEARCH
$done = 0;                     // MARKER HOW MANY ENTRYs ARE DONE
$msg = '';                    // TOAST MESSAGE
$file = 'data/Setting.json'; // DATA FILE NAME
// LOGIN CHECK
session_start();
$login = check_current_user_login($ARR_USERS, $SALT);
$currentuser = $login['user'];
//var_dump($login);
if ($login['status'] == 200 && $currentuser->gm == '1' ) {
    //var_dump($login);
} else {
    header('location: login.php');
    exit;
}

$vi = file_get_contents('lang/vi.php');
$en = file_get_contents('lang/en.php');
$tha = file_get_contents('lang/tha.php');
$cn = file_get_contents('lang/cn.php');
$tabs = json_decode(file_get_contents('data/Tabs.json'));


if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['Container'] != ''    ) {
    $receivable = $amount = 0;

if($_POST['vi'] != ''){
 file_put_contents('lang/vi.php',$_POST['vi']);
}
if($_POST['en'] != ''){
 file_put_contents('lang/en.php',$_POST['en']);
}
if($_POST['tha'] != ''){ 
 file_put_contents('lang/tha.php',$_POST['tha']);
}
if($_POST['cn'] != ''){
 file_put_contents('lang/cn.php',$_POST['cn']);
}



        $formdata = array(
		'Container' => $_POST['Container'] ,
		'subContainer' => $_POST['subContainer'] ,		
		'ipvn' => $_POST['ipvn']     	,		
		'ipcn' => $_POST['ipcn']     	,
		'iptha' => $_POST['iptha']     	,
		'ipmain' => $_POST['ipmain']     	,
		'ipcontroller' => $_POST['ipcontroller']
	
    );
	
	foreach ( $_POST['tabs'] as $key => $id){
		foreach ( $tabs as $idt){
			if($id == $idt->id){
				$idt->tabsg = $_POST['tabsg'][$key];
				$newtab[] = $idt; break;
			}
		}
	}
	
	if(count((array)$newtab) > 0){
		file_put_contents('data/Tabs.json', json_encode($newtab) );
	}
    //$arr_data = array();
    //if (file_exists($file)) {
        //$jsondata = file_get_contents($file);
        //$arr_data = json_decode($jsondata, true);
    //}
    $arr_data = $formdata;
   
    $jsondata = json_encode($arr_data);
    if (file_put_contents($file, $jsondata)) {
        $msg = $i18n['Data successfully saved!'];
    } else {
        $msg = $i18n['ERROR &mdash; Data not saved!'];
    }
    header('location: setting.php?msg=' . $msg);
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $_GET['msg'] = $i18n['All fields are required!'];
}

// CREATE DATA FILE IF NOT EXISTS
if (!is_file($file)) {
    file_put_contents($file, '');
}

// READ DATA
$data = json_decode(file_get_contents($file), true);



// HEADER
ob_end_flush();
include 'tpl/header.php';
// FORM FOR NEW ENTRY
if(@$_GET['q'] == ''){
    ?>
        <div class="badge grey lighten-3 hoverable create-user">
            <form action="" method="POST">
                <div class="row">
                    <div class="input-field col s4">
                        <textarea id="Container"  name="Container"   style="padding-top:10px;    height: 100px;" ><?php echo @$data['Container']; ?></textarea>
                        <label for="name">费用归类</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            new options new line (value|text)
                        </span>
                    </div>
					
					<div class="input-field col s4">
                        <textarea id="sub-Container"  name="subContainer"  style="padding-top:10px;    height: 100px;" ><?php echo @$data['subContainer']; ?></textarea>
                        <label for="name">费用明细</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            sub for 费用归类 new options new line (费用归类|费用明细)
                        </span>
                    </div>

                    
                    <div class="input-field col s1" style="    text-align: right;">
                        <button class="colortoptran btn waves-effect waves-light" type="submit" name="action">
                          Save
                        </button>
                    </div>
                    <div class="input-field col s1 ">
                        <a style="    background: #4e4e4e;" class="btn  btn-default waves-effect waves-light" href="./index.php">
                            <?php echo $i18n['Go Back']; ?>
                        </a>
                    </div>
					<div class="input-field col s6">
                        <input id="ipvn"  name="ipvn"  style="  "  value="<?php echo @$data['ipvn']; ?>" />
                        <label for="ipvn" class="active" >Ip Login VN</label>
                    </div>
					<div class="input-field col s6">
                        <input id="ipcn"  name="ipcn"  style="   " value="<?php echo @$data['ipcn']; ?>" />
                        <label for="ipcn" class="active" >Ip Login CN</label>
                    </div>
					<div class="input-field col s6">
                        <input id="iptha"  name="iptha"  style="   " value="<?php echo @$data['iptha']; ?>" />
                        <label for="iptha" class="active" >Ip Login THA</label>
                    </div>
					<div class="input-field col s6">
                        <input id="ipmain"  name="ipmain"  style="    " value="<?php echo @$data['ipmain']; ?>"  />
                        <label for="ipmain" class="active" >Ip Login 总公司</label>
                    </div>
					
					<div class="input-field col s6">
                        <input id="ipcontroller"  name="ipcontroller"  style="    " value="<?php echo @$data['ipcontroller']; ?>"  />
                        <label for="ipcontroller" class="active" >Ip Login Controller/调节器</label>
                    </div>
					<div class="input-field col s12"></div>
					<div class="input-field col s6">
                        <textarea id="en"  name="en"  style=" padding-top:10px;    height: 1000px;"  ><?php echo $en; ?></textarea>
                        <label for="name">English</label>
                    </div>
					<div class="input-field col s6">
                        <textarea id="cn"  name="cn"  style=" padding-top:10px;    height: 1000px;"  ><?php echo $cn; ?></textarea>
                        <label for="name">中国</label>
                    </div>
					<div class="input-field col s6">
                        <textarea id="tha"  name="tha"  style=" padding-top:10px;    height: 1000px;"  ><?php echo $tha; ?></textarea>
                        <label for="name">ภาษาไทย</label>
                    </div>
					<div class="input-field col s6">
                        <textarea id="vi"  name="vi"  style=" padding-top:10px;    height: 1000px;"  ><?php echo $vi; ?></textarea>
                        <label for="name">VietNam</label>
                    </div>
					<div class="input-field col s6 sdfgsdkfgs">
						<h3>Account Order <button type="button" id="addgroup" >Add Group</button> 
                                                    <div style="width: 200px;float: right;">    
                                                 <select class="" onchange="  if($(this).val() == ''){ $('.nationfilter').removeClass('ishide'); }else if($(this).val() == '越南'){ $('.nationfilter').addClass('ishide'); $('.nationfilter越南').removeClass('ishide'); }else if($(this).val() == '中国'){ $('.nationfilter').addClass('ishide'); $('.nationfilter中国').removeClass('ishide'); }else if($(this).val() == '泰国'){ $('.nationfilter').addClass('ishide'); $('.nationfilter泰国').removeClass('ishide'); }else if($(this).val() == '印尼'){ $('.nationfilter').addClass('ishide'); $('.nationfilter印尼').removeClass('ishide'); }" id="filteraccountlocation">
                                     <option value="">全部</option>
                                    <option value="越南">越南</option>
                                    <option value="中国">中国</option>
                                    <option value="泰国">泰国</option>
                                    <option value="印尼">印尼</option>
                                    <option value="马来西亚">马来西亚</option>
                                </select>          
                                          </div>      
                                                </h3>
						
						<ol class="sortable list-unstyled example vertical " id="sortable" >
							<?php 
							foreach($tabs as $r){
								if($r->tabsg != ''){
									$tabsg[$r->tabsg] = $r->tabsg;
								}
							}
							
							foreach($tabsg as $r){
								 ?>
								 <li class="cvashkcvb <?php echo str_replace(' ','',$r); ?>" group="<?php echo $r; ?>" ><input placeholder="Enter Menu Lv1" class="dajsdh" style="padding: 0px;    height: 21px;" type="TEXT"  value="<?php echo $r; ?>" /> 
								 <ul class="sortable list-unstyled"> </ul></li>
								 <?php
							}
							
							foreach($tabs as $r){
								?>
								<li class="nationfilter nationfilter<?php echo $r->location; ?> block block-title" ><?php echo $r->name; ?>
									<input type="hidden" name="tabs[]"  value="<?php echo $r->id; ?>" />
									<input type="hidden" name="tabsg[]" group="<?php echo str_replace(' ','',$r->tabsg); ?>" class="dasjdjkas" value="<?php echo $r->tabsg; ?>" />
								</li>
							<?php 
							}
							
							
							
							?>
						</ol>
						
						
                    </div>

	
                </div>
            </form>
        </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sortable/0.9.13/jquery-sortable-min.js"></script>		
<script>
var adjustment;

$(function  () {
	
	$('li.block-title').each(function(){
		if($(this).find('.dasjdjkas').attr('group') != ''){
			$(this).appendTo(".cvashkcvb."+$(this).find('.dasjdjkas').attr('group')+' ul.list-unstyled'); 
		}
	});

	
	$('.sdfgsdkfgs').mouseup(function(){
		 setTimeout(function() {
			$('.dasjdjkas').val('');
			$('.cvashkcvb').each(function(){
				$(this).find('.dasjdjkas').val($(this).find('.dajsdh').val());
			}); 
		 }, 1000);
	});
	$('ol.sortable').sortable({ 	  
	   cancel: '.cvashkcvb'
	});
	$('ol.sortable').disableSelection();
	
	
	$("#addgroup").click(function(){
		$('ol.example').prepend('<li class="cvashkcvb" ><input placeholder="Enter Menu Lv1" class="dajsdh" style="padding: 0px;    height: 21px;" type="TEXT"  value="" /> <ul class="sortable list-unstyled"> </ul></li>');
		
		
	});
	
	

  
});
</script>
<style>
.cvashkcvb {
	background: none!important;
}

ol.vertical li {
    margin-left: 20px!important;

}
.sortable li.cvashkcvb {	
    margin-left: 0px!important;
}
.block {
	margin: 5px 0;
	border: 1px solid #f1e8e2;
	background: #fff;
}
.block-title {
    font-family: Arial;
	font-size: 12px;
	color: #4c4743;
	padding: 0 10px;
	height: 33px;
	line-height: 33px;
	position: relative;
}
.sortable {
    list-style: none;
    padding-left: 0;
}
.sortable ul {
    margin-left: 25px;
}
.ui-sortable-helper {
	box-shadow: rgba(0,0,0,0.15) 0 3px 5px 0;
	width: 300px!important;
	height: 33px!important;
}
.sortable-placeholder {
	height: 35px;
	background: #e3dcd7;
	margin-bottom: 5px;
	margin-top: 5px;
}


ol.vertical li {
    display: block;
    margin: 5px;
    padding: 5px;
    border: 1px solid #cccccc;
    color: #0088cc;
    background: #dadada;    cursor: all-scroll;
}
.dragged {
  position: absolute;
  opacity: 0.5;
  z-index: 2000;
}

ol.example li.placeholder {
  position: relative;
  /** More li styles **/
}
ol.example li.placeholder:before {
  position: absolute;
  /** Define arrowhead **/
}
.input-field>label {
	margin-left: 12px;
}
.input-field.col label {
    left: 0px;
    top: -5px;
}
.grey.lighten-3 {
    padding-top: 20px;
    background-color: #eee !important;
}
ol.vertical li.ishide {
    display: none;
   
}
.dropdown-content {
   
   }
</style>	
    <?php
}


// var_dump($sortdate); die;
// FOOTER
include 'tpl/footer.php';
?>
