<?php
error_reporting(0);
ini_set('display_errors', 0);
ob_start();
require 'setup/config.php';
session_start();
$LANG =  'cn';
// SET LANGUAGE
if (isset($_GET['lang'])){	
	$_SESSION['HTTP_ACCEPT_LANGUAGE'] = $_GET['lang'];
}
if (isset($_SESSION['HTTP_ACCEPT_LANGUAGE']) and file_exists('lang/' . $_SESSION['HTTP_ACCEPT_LANGUAGE'] . '.php')) {	
	if((@include 'lang/' . $_SESSION['HTTP_ACCEPT_LANGUAGE'] . '.php') === false){
		echo 'Caught exception: Translate file is wrong';
	}else{
		@include_once('lang/' . $_SESSION['HTTP_ACCEPT_LANGUAGE'] . '.php');
	}
} else {
	if((@include 'lang/' . $LANG . '.php') === false){
		echo 'Caught exception: Translate file is wrong';
	}else{
		@include_once('lang/' . $LANG . '.php');
	}
}

// INI VARs
$file = 'data/User.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){
    if($currentuser->gm != '1'){        
        header('location: index.php');
        exit;
    }
    //var_dump($login);
}else{
    header('location: login.php');
    exit;
}

// READ DATA
$jsonString = file_get_contents($file);
$data = json_decode($jsonString, true);
$tabs = json_decode(file_get_contents('data/Tabs.json'));
// GENERATE LIST FOR DATA-LIST IN NAME INPUT FIELD
foreach ($data as $key => $values) {
    //$list .= '<option value="' . $values['name'] . '">';
}

// SAVE NEW ENTRY IF FORM POSTED

if ($_SERVER['REQUEST_METHOD'] == 'POST' and 
    @$_POST['name'] != '' and 
   // @ $_POST['gmail'] != '' and 
    @ $_POST['password'] != '' and 
    @ $_POST['role'] != '' and 
    @ $_POST['location'] != '') {

    $data = json_decode( str_replace("\u00a0", '&nbsp;', file_get_contents( $file ) ), true );
    //var_dump($data);
    foreach ($data as $key => $entry) {
        if ($entry['id'] == $_GET['id']) {
            $data[$key]['name'] = $_POST['name'];
            //$data[$key]['gmail'] = $_POST['gmail'];
            $data[$key]['password'] = $_POST['password'];
            $data[$key]['role'] = $_POST['role'];
            $data[$key]['location'] = $_POST['location'];
			$data[$key]['account'] = $_POST['account'];
			$data[$key]['accountreadonly'] = $_POST['accountreadonly'];
			$data[$key]['changedcurrency'] = $_POST['changedcurrency'];			
			$data[$key]['modifiedname'] = $currentuser->name;
			$data[$key]['modifiedtime'] = time();
			$data[$key]['noti'] =  $_POST['noti'];
                        $data[$key]['companyinfo'] =  $_POST['companyinfo'];
			$data[$key]['chart'] =  $_POST['chart'];
			$data[$key]['condition'] =  $_POST['condition'];
			$data[$key]['daytoseedata'] =  $_POST['daytoseedata'];
			$data[$key]['total'] =  $_POST['total'];
			$data[$key]['totalconsignee'] = $_POST['totalconsignee'];
			$data[$key]['totalmonth'] = $_POST['totalmonth'];
			$data[$key]['customer'] =  $_POST['customer'];
			$data[$key]['cherry'] =  $_POST['cherry'];
			$data[$key]['reminder'] =  $_POST['reminder'];
			$data[$key]['end'] =  $_POST['end'];
			$data[$key]['borrow'] =  $_POST['borrow'];
			$data[$key]['locationreminder'] =  $_POST['locationreminder'];
			$data[$key]['filelocation'] =  $_POST['filelocation'];
                        $data[$key]['mod1'] = $_POST['mod1'];
                        $data[$key]['mod2'] = $_POST['mod2'];
                        $data[$key]['mod3'] = $_POST['mod3'];
                        $data[$key]['mod4'] = $_POST['mod4'];
                        $data[$key]['mod5'] = $_POST['mod5'];
                        $data[$key]['mod6'] = $_POST['mod6'];
                        $data[$key]['mod7'] = $_POST['mod7'];
                        $data[$key]['mod8'] = $_POST['mod8'];
                        $data[$key]['mod9'] = $_POST['mod9'];
                        $data[$key]['mod10'] = $_POST['mod10'];
                        $data[$key]['mod11'] = $_POST['mod11'];
                        $data[$key]['mod12'] = $_POST['mod12'];
                        $data[$key]['mod13'] = $_POST['mod13'];
                        $data[$key]['mod14'] = $_POST['mod14'];
                        $data[$key]['mod15'] = $_POST['mod15'];
                        $data[$key]['mod16'] = $_POST['mod16'];
                        $data[$key]['mod17'] = $_POST['mod17'];
                        $data[$key]['mod18'] = $_POST['mod18'];
                        $data[$key]['mod19'] = $_POST['mod19'];
                        $data[$key]['mod20'] = $_POST['mod20'];
                        $data[$key]['mod21'] = $_POST['mod21'];
                        $data[$key]['mod22'] = $_POST['mod22'];
                        $data[$key]['mod23'] = $_POST['mod23'];
                        $data[$key]['mod24'] = $_POST['mod24'];
                        $data[$key]['mod25'] = $_POST['mod25'];
                        $data[$key]['mod26'] = $_POST['mod26'];
                        $data[$key]['mod27'] = $_POST['mod27'];
                        $data[$key]['mod28'] = $_POST['mod28'];
                        $data[$key]['mod29'] = $_POST['mod29'];
        }
    }
    $newJsonString = json_encode($data);
    file_put_contents($file, $newJsonString);
    header('location: user.php?msg=' . $i18n['Entry successfully edited!']);
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $_GET['msg'] = $i18n['All fields are required!'];
}

// HEADER
ob_end_flush();
include 'tpl/header.php';

foreach ($data as $r){
	if($r['id'] == @$_GET['id']){
		$data = $r;
		break;	
	}
}

// FORM FOR EDIT ENTRY
?>
    <div class="badge grey lighten-3 hoverable create-user">
            <form action="" method="POST">
                <div class="row">
                    <div class="input-field col s1">
                        <input 
                            value="<?php echo $data['name']; ?>" 
                            id="name" 
                            name="name" 
                            type="text" 
                            class="validate" 
                            maxlength="32" 
                            data-length="32" 
                            required="required" autocomplete="off" />
                        <label for="name"><?php echo $i18n['Name']; ?></label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            <?php echo $i18n['MesName']; ?>
                        </span>
                    </div>
                    <div class="input-field col s1">
                        <input 
                            value="<?php echo $data['password']; ?>" 
                            id="password" 
                            name="password" 
                            type="password" 
                            class="validate" 
                            maxlength="32" 
                            data-length="32" 
                            required="required" autocomplete="off" />
                        <label for="password"><?php echo $i18n['Password']; ?></label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            <?php echo $i18n['MesPassword']; ?>
                        </span>
                    </div>
                    <div class="input-field col s1">
			<select name="role" id="role">
			<?php if ($currentuser->gm == '1' ) {  ?>    
                            <option <?php if($data['role'] == 'Manager'){
                                 echo 'selected'; 
                            } ?>  value="Manager">Manager</option>
							<option <?php if($data['role'] == 'Finance'){
                                 echo 'selected'; 
                            } ?>  value="Finance">Controller</option>
			<?php } ?>
                            <option <?php if($data['role'] == 'Operator'){
                                 echo 'selected'; 
                            }  ?>  value="Operator">Finance</option>
                        </select>

                        
                        <label for="nonumber"><?php echo $i18n['Role']; ?></label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            <?php echo $i18n['MesRole']; ?>
                        </span>
                    </div>
                    <div class="input-field col s1">
                         <?php if( $currentuser->gm == 1 ){ ?> 
				<select name="location" id="location">
                            		<option <?php if($data['location'] == '总公司'){
                                 echo 'selected'; 
                            } ?> value="总公司">总公司</option>
                            		<option <?php if($data['location'] == '越南'){
                                 echo 'selected'; 
                            } ?> value="越南">越南</option>
                            		<option <?php if($data['location'] == '中国'){
                                 echo 'selected'; 
                            } ?> value="中国">中国</option>
			    		<option <?php if($data['location'] == '泰国'){
                                 echo 'selected'; 
                            } ?> value="泰国">泰国</option>
                                        
                                        <option <?php if($data['location'] == '印尼'){
                                 echo 'selected'; 
                            } ?> value="印尼">印尼</option>
                                        
                                        <option <?php if($data['location'] == '马来西亚'){
                                 echo 'selected'; 
                            } ?> value="马来西亚">马来西亚</option>
                                        
                        	</select>
			<?php }else{ ?> 
				<select name="location" id="location">
                            		<option value="<?= $currentuser->location ?>"><?= $currentuser->location ?></option>                            		
                        	</select>
			<?php } ?>

                        <label for="location"><?php echo $i18n['Location']; ?></label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            <?php echo $i18n['MesLocation']; ?>
                        </span>
                    </div>
					
                    <div class="input-field col s3">
					
					
						<select name="account[]" id="account" multiple >
							<?php foreach ( $tabs as $r ){
								if( $currentuser->gm != 1 && $r->iduser != $currentuser->id ) { continue; } 
							?>
								 <option  <?php if(in_array($r->id,$data['account'])){ echo 'selected'; } ?> value="<?php echo $r->id ?>"><?php echo $r->name ?></option>';
							<?php }?>
						</select>
						<label for="location">Accounting</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            Add account for User
                        </span>
					</div>
					
					<div class="input-field col s3">					
						<select name="accountreadonly[]" id="accountreadonly" multiple >
							<?php foreach ( $tabs as $r ){
								if( $currentuser->gm != 1 && $r->iduser != $currentuser->id ) { continue; } 
							?>
								 <option  <?php if(in_array($r->id,$data['accountreadonly'])){ echo 'selected'; } ?> value="<?php echo $r->id ?>"><?php echo $r->name ?></option>';
							<?php }?>
						</select>
						<label for="location">Accounting Readonly</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            Add account for User
                        </span>
					</div>
					
					<div class="input-field col s1">
                        <button class="colortoptran btn waves-effect waves-light" type="submit" name="action">
                            <?php echo $i18n['Edit']; ?>
                        </button>
                    </div>
                    <div class="input-field col s1 ">
                        <a class="colortoptran btn waves-effect waves-light" href="./user.php">
                            <?php echo $i18n['Go Back']; ?>
                        </a>
                    </div>	
					<div class="input-field col s12" style="    height: 40px;    margin-top: -15px;" >
					<div class="input-field col s1" style="    margin: 0;padding:0px;">
						<select name="filelocation[]" id="filelocation" multiple >
                            <option >----</option> 	
                            		<option <?php if(in_array('越南',$data['filelocation'])){
                                 echo 'selected'; 
                            } ?> value="越南">越南</option>
                            		<option <?php if(in_array('中国',$data['filelocation'])){
                                 echo 'selected'; 
                            } ?> value="中国">中国</option>
									<option <?php if(in_array('泰国',$data['filelocation'])){
                                 echo 'selected'; 
                            } ?> value="泰国">泰国</option>
                        </select>
						<label for="location"  style="   top: -13px;    left: 1px;    font-size: 9px;" >File Manager</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                           
                        </span>
					</div>
                                            
					
					<?php 
					$daytoseedata = '';
					if($data['daytoseedata'] > 0){
						$daytoseedata = $data['daytoseedata'];
					}
                                        /*
                                        <select name="daytoseedata" id="daytoseedata">
						<option value="0">Modab02可看日记账天数</option>
						<?php for($d= 1; $d <= 100; $d++){
                                                    if($d != '1' && $d != '7' && $d != '15' && $d != '30'){continue;}
							?><option <?php if($d == $daytoseedata){ echo 'selected'; } ?> value="<?php echo $d; ?>"><?php echo $d; ?></option><?php
						} ?>
					</select>
                                         */
					?>		
					<div class="input-field col s2" style="    margin: 0;" >
					
					<input name="daytoseedata" id="daytoseedata" min="0"  type="number"  value="<?php echo $daytoseedata; ?>"/>
						<label for="daytoseedata">Modab02可看日记账天数</label>
					</div>
                                       
					<div class="input-field col s1" style="    width: 93px;     margin-top: -1px;    margin-left: 4px" >
						<select name="locationreminder" id="locationreminder">
									<option value="">----</option>
                            		<option <?php if($data['locationreminder'] == '越南'){ echo 'selected'; } ?> value="越南">越南</option>
                            		<option <?php if($data['locationreminder'] == '中国'){ echo 'selected'; } ?> value="中国">中国</option>
									<option <?php if($data['locationreminder'] == '泰国'){ echo 'selected'; } ?> value="泰国">泰国</option>
                        </select>	
					</div>		
					
					
					<div class="input-field col s2" style="    margin: 0; display:none;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="customer" <?php if($data['customer'] == 1){ echo "checked=''";} ?>  name="customer"  >
						  <span>Customer/客户付款方式?</span>
						</label>
					</div>
                                            
                                        <div class="input-field col s2" style="    margin: 0;" >
						<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="cherry"  <?php if($data['cherry'] == 1){ echo "checked=''";} ?> name="cherry"  >
						  <span>Modab01任意地址登录</span>
						</label>
					</div>
                                         <div class="input-field col s2" style="    margin: 0;" >    
                                            <label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="companyinfo" <?php if($data['companyinfo'] == 1){ echo "checked=''";} ?>  name="companyinfo"  >
						  <span>Modab03公司资料登记</span>
						</label>
					</div>	    
					<div class="input-field col s2" style="margin-top: 17px;width: 150px;" >
						<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="reminder"  <?php if($data['reminder'] == 1){ echo "checked=''";} ?>  name="reminder"  >
						  <span>MOD11授权</span>
						</label>
						<input
							 value="<?php if ($data['end'] != ''){ echo $data['end'];}else{ echo date($DATE); }; ?>"
							 id="end"
							 name="end"
							 type="text"
							 class="datepicker" style="    width: 85px;    margin-left: 27px;    margin-top: 0px;    height: 30px;"
							 maxlength="<?php strlen($PICK); ?>"
							 data-length="<?php strlen($PICK); ?>" required="required"/>
					</div>
					<div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="total" <?php if($data['total'] == 1){ echo "checked=''";} ?>  name="total"  >
						  <span>MOD1余额汇总</span>
						</label>
					</div>	
					<div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="totalconsignee" <?php if($data['totalconsignee'] == 1){ echo "checked=''";} ?> name="totalconsignee"  >
						  <span>MOD2财务报表</span>
						</label>
					</div>	
					<div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="totalmonth"    <?php if($data['totalmonth'] == 1){ echo "checked=''";} ?> name="totalmonth"  >
						  <span>MOD3账户收支/月</span>
						</label>
					</div>    
                    </div>				
					
                    
                     <div class="input-field col s12" style="    height: 40px;    margin-top: -15px;" >	
                                        
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod4" <?php if($data['mod4'] == 1){ echo "checked=''";} ?>  name="mod4"  >
						  <span>MOD4过账余额汇总</span>
						</label>
					</div>    
                                        <div class="input-field col s1" style="    margin: 0;">
						<label class="pure-material-checkbox">
							  <input type="checkbox"  value="1"  id="changedcurrency"  <?php if($data['changedcurrency'] == 1){ echo "checked=''";} ?>  name="changedcurrency"  >
							  <span>MOD5汇率</span>
						</label>
					</div>
					
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod6"  <?php if($data['mod6'] == 1){ echo "checked=''";} ?> name="mod6"  >
						  <span>MOD6Consignee</span>
						</label>
					</div> 
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod7" <?php if($data['mod7'] == 1){ echo "checked=''";} ?>  name="mod7"  >
						  <span>MOD7客户</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod8" <?php if($data['mod8'] == 1){ echo "checked=''";} ?>  name="mod8"  >
						  <span>MOD8往来单位</span>
						</label>
					</div>
                                       <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod9"  <?php if($data['mod9'] == 1){ echo "checked=''";} ?>  name="mod9"  >
						  <span>MOD9收/付款人</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod10" <?php if($data['mod10'] == 1){ echo "checked=''";} ?>  name="mod10"  >
						  <span>MOD10报销单</span>
						</label>
					</div>    
                                        
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod12" <?php if($data['mod12'] == 1){ echo "checked=''";} ?>  name="mod12"  >
						  <span>MOD12贷款人总数</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="borrow" <?php if($data['borrow'] == 1){ echo "checked=''";} ?>  name="borrow"  >
						  <span>MOD13贷/付</span>
						</label>
					</div>
                                            <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="condition" <?php if($data['condition'] == 1){ echo "checked=''";} ?>  name="condition"  >
						  <span>MOD14查询</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod15" <?php if($data['mod15'] == 1){ echo "checked=''";} ?>  name="mod15"  >
						  <span>MOD15文件管理器</span>
						</label>
					</div> 
                         
                                        </div>    
                                        <div class="input-field col s12" style="    height: 40px;    margin-top: -15px;">		
                                               
                                         
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod16" <?php if($data['mod16'] == 1){ echo "checked=''";} ?>  name="mod16"  >
						  <span>MOD16API</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod17" <?php if($data['mod17'] == 1){ echo "checked=''";} ?>  name="mod17"  >
						  <span>MOD17我的工作</span>
						</label>
					</div>
                                       <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="chart"  <?php if($data['chart'] == 1){ echo "checked=''";} ?>  name="chart"  >
						  <span>MOD18图表</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="noti" <?php if($data['noti'] == 1){ echo "checked=''";} ?>  name="noti"  >
						  <span>MOD19通知</span>
						</label>
					</div>
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod20"   <?php if($data['mod20'] == 1){ echo "checked=''";} ?>   name="mod20"  >
						  <span>MOD20收支汇总/日</span>
						</label>
					</div>	 
                                        <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod21"   <?php if($data['mod21'] == 1){ echo "checked=''";} ?>   name="mod21"  >
						  <span>Mod21新费用</span>
						</label>
					</div>	
                                         <div class="input-field col s1" style="    margin: 0;" >    
                                        <label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod25"   <?php if($data['mod25'] == 1){ echo "checked=''";} ?>   name="mod25"  >
						  <span>Mod22F</span>
						</label>
					</div>	
                                        <div class="input-field col s1" style="    margin: 0;" >    
                                        <label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod26"   <?php if($data['mod26'] == 1){ echo "checked=''";} ?>   name="mod26"  >
						  <span>Mod23建档</span>
						</label>
					</div>	  
                                        <div class="input-field col s1" style="    margin: 0;" >    
                                        <label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod27"   <?php if($data['mod27'] == 1){ echo "checked=''";} ?>   name="mod27"  >
						  <span>Mod24柜量统计</span>
						</label>
					</div>	
                                            <div class="input-field col s1" style="    margin: 0;" >    
                                        <label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod28"   <?php if($data['mod28'] == 1){ echo "checked=''";} ?>   name="mod28"  >
						  <span>Mod25工资</span>
						</label>
					</div>	 
                                            <div class="input-field col s1" style="    margin: 0;" >    
                                        <label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod29"   <?php if($data['mod29'] == 1){ echo "checked=''";} ?>   name="mod29"  >
						  <span>Mod26</span>
						</label>
					</div>	 
                                            <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod22"   <?php if($data['mod22'] == 1){ echo "checked=''";} ?>   name="mod22"  >
						  <span>Modaa设置</span>
						</label>
					</div>	     
                                            
                                            <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod23"   <?php if($data['mod23'] == 1){ echo "checked=''";} ?>   name="mod23"  >
						  <span>Modab用户</span>
						</label>
					</div>	 
                                            
                                            <div class="input-field col s1" style="    margin: 0;" >
					<label class="pure-material-checkbox">
						  <input type="checkbox"  value="1"  id="mod24"  <?php if($data['mod24'] == 1){ echo "checked=''";} ?>   name="mod24"  >
						  <span>Modac修订</span>
						</label>
					</div>	     
                                            
                                       </div>    
                </div>
            </form>
        </div>
<?php
// FOOTER
include 'tpl/footer.php';
