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

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

$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
$found = 0;                     // MARKER HOW MANY HITS FOUND A SEARCH
$done = 0;                     // MARKER HOW MANY ENTRYs ARE DONE
$msg = '';                    // TOAST MESSAGE
$file = 'data/mywork.json'; // DATA FILE NAME
// LOGIN CHECK

$login = check_current_user_login($ARR_USERS, $SALT);
$currentuser = $login['user'];
//var_dump($login);
if ($login['status'] == 200) {
    
} else {
    header('location: login.php');
    exit;
}
/* $sessionname = md5($USER . $SALT);
  if (!@$_SESSION[$sessionname]) {
  header('location: login.php');
  exit;
  } */
// DELETE ENTRY IF DEL LINK IS CLICKED
if ($_SERVER['REQUEST_METHOD'] == 'POST' and $_POST['sendtoaproval'] == '1' ) {
	$jsonString = file_get_contents($file);
    $data = json_decode($jsonString, true);
	$_POST['iddoc'] = explode(',',$_POST['iddoc']);
    foreach ($data as $key => $entry) {
		if (in_array($entry['id'],$_POST['iddoc'])) {
			
			 if($_POST['feedback'] != 1){	
				$data[$key]['status'] = 'complete';
				$data[$key]['datecomplete'] = time();
			 }
			 
			 $_POST['note'] = str_replace('Note/注意:','',$_POST['note']);
			 if($_POST['note'] != ''){
				$data[$key]['remark'] .= '<br><b>'.$currentuser->name.'</b>: '.$_POST['note'];
			 }
			
		}
	}
	
	if (file_put_contents($file, json_encode($data))) {
        $msg = $i18n['Data successfully saved!'];
    } else {
        $msg = $i18n['ERROR &mdash; Data not saved!'];
    }
    header('location: mywork.php?msg=' . $msg);
}
// DELETE ENTRY IF DEL LINK IS CLICKED
if (@$_GET['delete_id'] != '' && $currentuser->gm == 1 ) {
    $data = json_decode(file_get_contents($file), true);
    foreach ( $data as $key => $r  ){
        if($r['id'] == @$_GET['delete_id']){
			if ($currentuser->role == 'Manager' ) {
				unset($data[$key]);
			}
        }
    }
	$convert_array_to_json = array_values($data);
    $write_json = json_encode($convert_array_to_json);
    file_put_contents($file, $write_json);
    header('location: mywork.php?msg=' . $i18n['Entry successfully deleted.']);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['content'] != '' && $_POST['user'] != '' && $currentuser->role  == 'Manager'  )  { 
	if($_POST['id'] == ''){		
		if(count((array)$_FILES['fileupload']['name']) > 0){
			 foreach ($_FILES['fileupload']['name'] as $filekey => $fileu){
				if($_FILES['fileupload']['error'][$filekey] != 0 || strpos($_FILES['fileupload']['filetype'][$filekey],'application/octet-stream') !== false || strpos($fileu,'.php') !== false || strpos($fileu,'.html') !== false ){ continue; }
				
				 $filename_text = time().'_'.$fileu;
				 $filename[] = $fileu;
				 $filetype[] = $_FILES['fileupload']['type'][$filekey];
				 $filesize[] =  $_FILES['fileupload']['size'][$filekey];
				 $filetimeupload[] = time();
				 $filecreatebyid[] = $currentuser->id;
				 $filecreatebyname[] = $currentuser->name;
				 $filepath[] = $filename_text;
				 copy( $_FILES['fileupload']['tmp_name'][$filekey] , __DIR__.'/uploads/'.$filename_text );
			 }
			// print_r($_FILES['fileupload']);;die;
		}
		$receivable = $amount = 0;
        $formdata = array(
        'id' => time()
        , 'Content' => $_POST['content']
        , 'remark' => $_POST['remark'] 
		, 'who' => $_POST['user']   
		, 'status' => 'new' 	
		, 'createby' => $currentuser->name
		, 'filename' => $filename
		, 'filetype' => $filetype
		, 'filesize' => $filesize
		, 'filetimeupload' => $filetimeupload
		, 'filecreatebyid' => $filecreatebyid
		, 'filecreatebyname' => $filecreatebyname
		, 'filepath' => $filepath
		);
		$arr_data = array();
		if (file_exists($file)) {
			$jsondata = file_get_contents($file);
			$arr_data = json_decode($jsondata, true);
		}
		$arr_data[] = $formdata;
	}else{
		
		$arr_data = array();
			if (file_exists($file)) {
        		$jsondata = file_get_contents($file);
        		$arr_data = json_decode($jsondata, true);
    		}
			foreach ($arr_data as $key => $r){
				if($r['id'] == $_POST['id']){		
					if(count((array)$_FILES['fileupload']['name']) > 0){
						 foreach ($_FILES['fileupload']['name'] as $filekey => $fileu){
							if($_FILES['fileupload']['error'][$filekey] != 0 || strpos($_FILES['fileupload']['filetype'][$filekey],'application/octet-stream') !== false || strpos($fileu,'.php') !== false || strpos($fileu,'.html') !== false ){ continue; }
							
							 $filename_text = time().'_'.$fileu;
							 $arr_data[$key]['filename'][] = $fileu;
							 $arr_data[$key]['filetype'][] = $_FILES['fileupload']['type'][$filekey];
							 $arr_data[$key]['filesize'][] =  $_FILES['fileupload']['size'][$filekey];
							 $arr_data[$key]['filetimeupload'][] = time();
							 $arr_data[$key]['filecreatebyid'][] = $currentuser->id;
							 $arr_data[$key]['filecreatebyname'][] = $currentuser->name;
							 $arr_data[$key]['filepath'][] = $filename_text;
							 copy( $_FILES['fileupload']['tmp_name'][$filekey] , __DIR__.'/uploads/'.$filename_text );
						 }
						// print_r($_FILES['fileupload']);;die;
					}
					$arr_data[$key]['who'] = $_POST['user'];
					$arr_data[$key]['Content'] = $_POST['content'];
					$arr_data[$key]['remark'] = $_POST['remark'];				
					$arr_data[$key]['modifiedname'] = $currentuser->name;
					$arr_data[$key]['modifiedtime'] = time();
					if($_POST['goback'] == 1){
						$arr_data[$key]['status'] = 'new';
						$arr_data[$key]['goback'] = '1';
					}
					

		
			}			
		}
		
	}
    
	$arr_data = array_values($arr_data);
    $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: mywork.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));
$user = json_decode(file_get_contents('data/User.json'));

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

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
	if($_SESSION['HTTP_ACCEPT_LANGUAGE'] == 'en'){
		$string = array(
			'y' => 'year',
			'm' => 'month',
			'w' => 'week',
			'd' => 'day',
			'h' => 'hour',
			'i' => 'minute',
			's' => 'second',
		);
	}else{
		
		$string = array(
			'y' => '年',
			'm' => '月',
			'w' => '周',
			'd' => '天',
			'h' => '小时',
			'i' => '分钟',
			's' => '秒',
		);
	}
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
			if($_SESSION['HTTP_ACCEPT_LANGUAGE'] == 'en'){
				$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
			}else{
				$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? '' : '');
			}
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
	if($_SESSION['HTTP_ACCEPT_LANGUAGE'] == 'en'){
		return $string ? implode(', ', $string) . ' ago' : 'just now';
	}else{
		return $string ? implode(', ', $string) . ' 前' : '现在';
	}
}


// FORM FOR NEW ENTRY
if( $currentuser->role  == 'Manager'){
    ?>
        <div class="badge grey lighten-3 hoverable create-user">
            <form action="" method="POST"  enctype="multipart/form-data" >
			<input value=""  id="id" name="id" type="hidden"  />
			<input value=""  id="goback" name="goback" type="hidden"  />
                <div class="row ">
                    <div class="input-field col s5">
                        <textarea 
                            id="content" 
                            name="content" 
                            type="text" 
                            class="validate" 
                            required="required"  autocomplete="off" style="   background: #fff; margin-top: 10px;height:95px;" > </textarea>
                        <label for="name" class="active" >Content/内容</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                           
                        </span>
                    </div>
					
					<div class="input-field col s2  "  >
					<div class="file-field">
						<div class="btn btn-primary btn-sm float-left" style="padding: 3px;" >
							  <span><?php echo $i18n['Choose file']; ?></span>
							  <input type="file"  name="fileupload[]"  id="fileupload"  multiple="multiple" >
						</div>
						<div class="file-path-wrapper">
							  <input class="file-path validate" type="text" placeholder="<?php echo $i18n['Upload your file']; ?>">
						</div>
					</div>
				</div>	
                   
                    <div class="input-field col s3">
                        <input 
                        value="" 
                        id="remark" 
                        name="remark" 
                        type="text" 
                        class="validate"     
						placeholder=""				
						/>
						<label for="name" class="active" >Remark/备注</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                           
                        </span>
                    </div>
					
                    <div class="input-field col s1 saddnln" style="    text-align: right;">
                        <button class="colortoptran btn waves-effect waves-light" type="submit" name="action">
                          Save
                        </button>
                    </div>
					<div class="input-field col s2 uhdsai" style="    text-align: right; display:none;" >
                        <button class="colortoptran btn waves-effect waves-light pulse" style="  background-color: #f77f00!important;" type="submit" name="action">
                          Approved/批准
                        </button>
                    </div>
                    <div class="input-field col s1 ">
                        <a style="    background: #4e4e4e;" class="btn  btn-default waves-effect waves-light" href="./mywork.php">
                            <?php echo $i18n['Go Back']; ?>
                        </a>
                    </div>
					
					 <div class="input-field col s3" style="margin-top: -13px;" >
						<select name="user[]" id="user" multiple >
							
							<?php if($currentuser->gm == 1) {  ?>
							<optgroup label="总公司">
							<?php foreach($user as $r){ 
								//	if( $r->location != '总公司' ) { continue; }
									//if($currentuser->gm != 1) { continue; } 
									if($r->role == 'Finance'){ $r->role= 'Controller'; }
									if($r->role == 'Operator'){ $r->role= 'Finance'; }
								?>
								<option value="<?php echo $r->id; ?>" data-icon="https://viewsgain.com/wp-content/uploads/2018/08/Buy-Youtube-Subscribers.png" ><?php echo $r->name.'/'.$r->role; ?></option>
							<?php } ?>
							</optgroup>
							
							<?php }else{ ?>
								<?php foreach($user as $r){ 
									if($currentuser->location != $r->location || $r->id == $currentuser->id ) { continue; }
									if($r->role == 'Finance'){ $r->role= 'Controller'; }
									if($r->role == 'Operator'){ $r->role= 'Finance'; }									
								?>
								<option value="<?php echo $r->id; ?>" data-icon="https://viewsgain.com/wp-content/uploads/2018/08/Buy-Youtube-Subscribers.png" ><?php echo $r->name.'/'.$r->role; ?></option>
								<?php } ?>
							<?php } ?>
                        </select>
                        <label for="date">Assignee/代理人</label>
                        <span class="helper-text" data-error="Error" data-success="OK">
                            
                        </span>
                    </div>
                    
					
                </div>
            </form>
        </div>
    <?php
}
// DATA TABLE
// LOOP EACH ENTRY
    ?>
	
<ul class="collapsible">
    <li>
      <div class="collapsible-header" style="background: #999;color: #fff;" ><i class="material-icons">assignment</i>My Task/我的任务</div>
      <div class="collapsible-body" style="display:block;"  >
		<table class="striped responsive-table tablesorter" id="tab-table">
			<thead>
					<tr>
						<th  >Creator/创造者</th>
						<th style="width: 55%" >Content/内容</th>
						<th >Remark/备注</th>
						<th >File</th>
						<th style="    width: 124px;">Date/约会受让人</th>
						<th style="width: 280px;" ></th>
					</tr>
			</thead>
			<tbody>
					<?php foreach ($data as $r){ 
					
						if(!in_array($currentuser->id,$r->who)) { continue; } 
						if($r->status != 'new') { continue; }  
						$toast = 'M.toast({html: "<span>Are You Sure/你确定吗?</span>' .
						'<a href=?delete_id=' . $r->id  . ' class=toast-action>' .
						$i18n['DELETE'] . '</a> &nbsp; &nbsp;"});';	
						$file = ''; 
								$values = (array)$r;
								
								foreach ($values['filename'] as $keyfile => $dataf ){
										$fnjf++;
										if ( strpos($values['filetype'][$keyfile] , 'pdf')  !== false ) {
												
												
												if( $currentuser->location == '中国' ){
													$file .=  '<a ref="https://htlservice.net/expense/uploads/'.rawurlencode($values['filepath'][$keyfile]).'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/pdf.png" style="width:24px;height:auto;" /></a>';
												}else{
													$file .=  '<a ref="https://drive.google.com/viewerng/viewer?url=https://htlservice.net/expense/uploads/'.urlencode($values['filepath'][$keyfile]).'?pid=explorer&efh=false&a=v&chrome=false&embedded=true" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/pdf.png" style="width:24px;height:auto;" /></a>';
												}
										} elseif ( strpos($values['filetype'][$keyfile] , 'sheet')  !== false || strpos($values['filepath'][$keyfile] , '.xls')  !== false ) { 
												$file .=  '<a ref="https://view.officeapps.live.com/op/embed.aspx?src=https://htlservice.net/expense/uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/excel.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'zip')  !== false ) { 
												$file .=  '<a download href="uploads/'.$values['filepath'][$keyfile].'" ><img src="https://htlservice.net/Docs/tpl/img/zip.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'doc')  !== false ) { 
												$file .=  '<a ref="https://view.officeapps.live.com/op/embed.aspx?src=https://htlservice.net/expense/uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'"  href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/word.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'image')  !== false ) { 
												$file .=  '<a ref="uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));"  ><img src="https://htlservice.net/Docs/tpl/img/png.png" style="width:24px;height:auto;" /></a>';
										} else{
												$file .=  '<a download href="uploads/'.$values['filepath'][$keyfile].'" ><img src="https://htlservice.net/Docs/tpl/img/file.png" style="width:24px;height:auto;" /></a>';
										}
								}						
					?>
					<tr <?php if($r->goback == 1){ echo 'style="background: #ffd4d4;"'; } ?> >
						<td><?php echo $r->createby;  ?></td>
						<td><textarea readonly style="border: none;    width: 100%;    min-height: 50px;" ><?php echo $r->Content;  ?></textarea></td>
						<td><?php echo $r->remark;  ?></td>
						<td><?php echo $file;  ?></td>
						<td><b><?php echo time_elapsed_string('@'.$r->id); echo '<br><i style="font-weight:normal;    font-size: 11px;" >('.date('y.m.d H:i',$r->id).')</i>';  ?><b></td>
						<td>
						
						<button style="background: #01a000;color: #fff;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light sendtoaproval ">Done/完成</button>
						<?php if( $currentuser->gm == 1){  ?>
							<button onclick='editc("<?php echo addslashes(json_encode($r)); ?>")' style="  background: #a0a0a0;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light modifile ">Edit/编辑</button>
							<button onclick='<?php echo $toast; ?>' style="  background: red;color:#fff;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light deleted "><i style="    color: #fff!important;    font-size: 24px;" class="red-text text-darken-4 small material-icons">delete_forever</i></button>
						<?php } ?>
						<?php if( $currentuser->gm != 1){  ?>
						<button style="background: #666;color: #fff;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light sendtoaproval2 ">Remark/备注</button>
						<?php } ?>
						</td>
					</tr>
					<?php } ?>
			</tbody>
		</table>	
	  </div>
    </li>
	<?php if( $currentuser->role  == 'Manager'){ ?>
    <li>
      <div class="collapsible-header" style="background: #f77f00;color: #fff;" ><i class="material-icons"  >assignment_ind</i>Assignee/受让人的任务</div>
      <div class="collapsible-body" style="display:block;">
	  
			<table class="striped responsive-table tablesorter" id="tab-table">
			<thead>
					<tr>
						<th >Assignee/代理人</th>
						<th >Creator/创造者</th>
						<th style="width: 50%" >Content/内容</th>
						<th >Remark/备注</th>
						<th >File</th>
						<th style="    width: 124px;">Date/约会受让人</th>
						<th  style="width: 167px;"   ></th>
					</tr>
			</thead>
			<tbody>
					<?php foreach ($data as $r){ 	$uinfo = '';					
						if($r->status != 'new') { continue; }  	
						//if($r->who == $currentuser->id  ) { continue; } 
						if(in_array($currentuser->id,$r->who)) { continue; } 
						foreach($user as $u){ 
							if(in_array($u->id,$r->who)){
								$uinfo .= $u->name.', ';
							}
						}
						if($currentuser->gm != 1 && $currentuser->location != $uinfo->location ) { continue; } 	
						
						$toast = 'M.toast({html: "<span>Are You Sure/你确定吗?</span>' .
						'<a href=?delete_id=' . $r->id  . ' class=toast-action>' .
						$i18n['DELETE'] . '</a> &nbsp; &nbsp;"});';	
						$file = ''; 
								$values = (array)$r;
								
								foreach ($values['filename'] as $keyfile => $dataf ){
										$fnjf++;
										if ( strpos($values['filetype'][$keyfile] , 'pdf')  !== false ) {
												
												
												if( $currentuser->location == '中国' ){
													$file .=  '<a ref="https://htlservice.net/expense/uploads/'.rawurlencode($values['filepath'][$keyfile]).'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/pdf.png" style="width:24px;height:auto;" /></a>';
												}else{
													$file .=  '<a ref="https://drive.google.com/viewerng/viewer?url=https://htlservice.net/expense/uploads/'.urlencode($values['filepath'][$keyfile]).'?pid=explorer&efh=false&a=v&chrome=false&embedded=true" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/pdf.png" style="width:24px;height:auto;" /></a>';
												}
										} elseif ( strpos($values['filetype'][$keyfile] , 'sheet')  !== false || strpos($values['filepath'][$keyfile] , '.xls')  !== false ) { 
												$file .=  '<a ref="https://view.officeapps.live.com/op/embed.aspx?src=https://htlservice.net/expense/uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/excel.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'zip')  !== false ) { 
												$file .=  '<a download href="uploads/'.$values['filepath'][$keyfile].'" ><img src="https://htlservice.net/Docs/tpl/img/zip.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'doc')  !== false ) { 
												$file .=  '<a ref="https://view.officeapps.live.com/op/embed.aspx?src=https://htlservice.net/expense/uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'"  href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/word.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'image')  !== false ) { 
												$file .=  '<a ref="uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));"  ><img src="https://htlservice.net/Docs/tpl/img/png.png" style="width:24px;height:auto;" /></a>';
										} else{
												$file .=  '<a download href="uploads/'.$values['filepath'][$keyfile].'" ><img src="https://htlservice.net/Docs/tpl/img/file.png" style="width:24px;height:auto;" /></a>';
										}
								}							
					?>
					<tr>
						<td><?php echo $uinfo;  ?></td>
						<td><?php echo $r->createby;  ?></td>
						<td><textarea readonly style="border: none;    width: 100%;    min-height: 50px;" ><?php echo $r->Content;  ?></textarea></td>
						<td><?php echo $r->remark;  ?></td>
						<td><?php echo $file;  ?></td>
						<td><b><?php echo time_elapsed_string('@'.$r->id); echo '<br><i style="font-weight:normal;    font-size: 11px;" >('.date('y.m.d H:i',$r->id).')</i>';  ?><b></td>
						<td>
							<button onclick='editc("<?php echo addslashes(json_encode($r)); ?>")' style="  background: #a0a0a0;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light modifile ">Edit/编辑</button>
							<?php if( $currentuser->gm == 1){  ?>
								<button onclick='<?php echo $toast; ?>' style="  background: red;color:#fff;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light modifile "><i style="    color: #fff!important;    font-size: 24px;" class="red-text text-darken-4 small material-icons">delete_forever</i></button>
							<?php } ?>
						</td>
					</tr>
					<?php } ?>
			</tbody>
		</table>
	  
	  </div>
    </li>
    <li>
      <div class="collapsible-header" style="background: #01a000;color: #fff;"  ><i class="material-icons">assignment_turned_in</i>Completed/完成任务</div>
      <div class="collapsible-body" style="display:block;">
	  
	  <table class="striped responsive-table tablesorter" id="tab-table">
			<thead>
					<tr>
						<th >Assignee/代理人</th>
						<th >Creator/创造者</th>
						<th style="width: 45%" >Content/内容</th>
						<th >Remark/备注</th>
						<th >File</th>
						<th style="    width: 124px;" >Date/约会受让人</th>
						<th >Date Complete/完成</th>
						<th  style="width: 240px;"   ></th>
					</tr>
			</thead>
			<tbody>
					<?php foreach ($data as $r){ 		$uinfo = '';						
						if($r->status != 'complete') { continue; }  							
						foreach($user as $u){ 
							if(in_array($u->id,$r->who)){
								$uinfo .= $u->name.', ';
							}
						}
						if($currentuser->gm != 1 && $currentuser->location != $uinfo->location ) { continue; } 	
						
						$toast = 'M.toast({html: "<span>Are You Sure/你确定吗?</span>' .
						'<a href=?delete_id=' . $r->id  . ' class=toast-action>' .
						$i18n['DELETE'] . '</a> &nbsp; &nbsp;"});';	
						$file = ''; 
								$values = (array)$r;
								
								foreach ($values['filename'] as $keyfile => $dataf ){
										$fnjf++;
										if ( strpos($values['filetype'][$keyfile] , 'pdf')  !== false ) {
												
												
												if( $currentuser->location == '中国' ){
													$file .=  '<a ref="https://htlservice.net/expense/uploads/'.rawurlencode($values['filepath'][$keyfile]).'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/pdf.png" style="width:24px;height:auto;" /></a>';
												}else{
													$file .=  '<a ref="https://drive.google.com/viewerng/viewer?url=https://htlservice.net/expense/uploads/'.urlencode($values['filepath'][$keyfile]).'?pid=explorer&efh=false&a=v&chrome=false&embedded=true" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/pdf.png" style="width:24px;height:auto;" /></a>';
												}
										} elseif ( strpos($values['filetype'][$keyfile] , 'sheet')  !== false || strpos($values['filepath'][$keyfile] , '.xls')  !== false ) { 
												$file .=  '<a ref="https://view.officeapps.live.com/op/embed.aspx?src=https://htlservice.net/expense/uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/excel.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'zip')  !== false ) { 
												$file .=  '<a download href="uploads/'.$values['filepath'][$keyfile].'" ><img src="https://htlservice.net/Docs/tpl/img/zip.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'doc')  !== false ) { 
												$file .=  '<a ref="https://view.officeapps.live.com/op/embed.aspx?src=https://htlservice.net/expense/uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'"  href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));" ><img src="https://htlservice.net/Docs/tpl/img/word.png" style="width:24px;height:auto;" /></a>';
										} elseif ( strpos($values['filetype'][$keyfile] , 'image')  !== false ) { 
												$file .=  '<a ref="uploads/'.$values['filepath'][$keyfile].'" data-target="readfile" class="modal-trigger isreadder'.$fnjf.'" isreadder="'.$fnjf.'" href="javascript:void(0);" onclick="$(\'.isleft,.isright\').attr(\'nextread\',\''.$fnjf.'\');$(\'.iframereadfile\').attr(\'src\',$(this).attr(\'ref\'));"  ><img src="https://htlservice.net/Docs/tpl/img/png.png" style="width:24px;height:auto;" /></a>';
										} else{
												$file .=  '<a download href="uploads/'.$values['filepath'][$keyfile].'" ><img src="https://htlservice.net/Docs/tpl/img/file.png" style="width:24px;height:auto;" /></a>';
										}
								}							
					?>
					<tr>
						<td><?php echo $uinfo;  ?></td>
						<td><?php echo $r->createby;  ?></td>
						<td><textarea readonly style="border: none;    width: 100%;    min-height: 50px;" ><?php echo $r->Content;  ?></textarea></td>
						<td><?php echo $r->remark;  ?></td>
						<td><?php echo $file;  ?></td>
						<td><b><?php echo time_elapsed_string('@'.$r->id); echo '<br><i style="font-weight:normal;    font-size: 11px;" >('.date('y.m.d H:i',$r->id).')</i>';  ?><b></td>
						<td><b>完成而:<?php echo number_format(round(($r->datecomplete-$r->id)/3600)); ?> hours/小时<b><br><i style="font-weight:normal;    font-size: 11px;" >(<?php echo date('Y.m.d H:i',$r->datecomplete);  ?>)</i></td>
						<td>
							<button onclick='editc("<?php echo addslashes(json_encode($r)); ?>","1")' style="  background: #f77f00;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light modifile ">Unfinished/未完成</button>
							<?php if( $currentuser->gm == 1){  ?>
							<button onclick='<?php echo $toast; ?>' style="  background: red;color:#fff;" id="<?php echo $r->id ?>"  class="btn btn-danger waves-effect waves-light modifile "><i style="    color: #fff!important;    font-size: 24px;" class="red-text text-darken-4 small material-icons">delete_forever</i></button>
							<?php } ?>
						</td>
					</tr>
					<?php } ?>
			</tbody>
		</table>
		
	  </div>
    </li>
	<?php } ?>
</ul>
<form method="post" style="display:none;" id="fromsendtoaproval" >
   <input class="note" type="hidden"  name="note" value="" >   
   <input class="feedback" type="hidden"  name="feedback" value="" >
   <input class="iddoc" type="hidden"  name="iddoc" value="" >
   <input id="task" type="hidden"  name="sendtoaproval" value="1" >
</form>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<script>
<?php if( $currentuser->role  == 'Manager'){ ?>
function editc(id,back) {
//$(".dhkasbdbas").hide();	
var obj = jQuery.parseJSON(id);
$('#content').next().addClass('active');
$('#id').val(obj.id);
$('#content').val(obj.Content);
$('#remark').val(obj.remark);
$('#user').val(obj.who);

$('select:not(.exclude):not(#status)').formSelect();
if(back == 1){
	$('#goback').val('1');
	$('.saddnln').hide();$('.uhdsai').fadeIn();
}else{
	$('.saddnln').fadeIn();$('.uhdsai').hide();
}
var scroll_pos=(0);          
$('html, body').animate({scrollTop:(scroll_pos)}, '1000');
}
<?php } ?>
$(document).ready(function(){
	
	$("textarea").each(function(textarea) {
		$(this).height( $(this)[0].scrollHeight );
	});
	$('.isleft').click(function(){
		if($('.isreadder'+$(this).attr('nextread')).prev().hasClass('modal-trigger')){
			$('.isreadder'+$(this).attr('nextread')).prev().trigger('click');
		}
	});
	$('.isright').click(function(){
		if($('.isreadder'+$(this).attr('nextread')).next().hasClass('modal-trigger')){
			$('.isreadder'+$(this).attr('nextread')).next().trigger('click');
		}
	});
	
	$('.sendtoaproval2').click(function(){ 
			var newnote = window.prompt("FeedBack/反馈 ?", "Note/注意:");
			if ( newnote != '' ) {
				$("#fromsendtoaproval .note").val(newnote);
			}			
			$("#fromsendtoaproval .iddoc").val($(this).attr('id'));
			$("#fromsendtoaproval .feedback").val(1);
			$("#fromsendtoaproval").submit();
	});
	$('.sendtoaproval').click(function(){ 
			var newnote = window.prompt("Are you finished/你完成了吗 ?", "Note/注意:");
			if ( newnote != '' ) {
				$("#fromsendtoaproval .note").val(newnote);
			}			
			$("#fromsendtoaproval .iddoc").val($(this).attr('id'));
			$("#fromsendtoaproval").submit();
	});
	
    $('.collapsible').collapsible();
});
</script>			
<style>
table.tablesorter thead tr th {
    background-color: #ddd;
}
.isleft{ width: 50px; height: 50px; position: fixed; left: 15px; top: 45%; z-index: 1000000; font-size: 54px; opacity: 0.2;cursor: pointer; } .isright{ width: 50px; height: 50px; position: fixed; right: 15px; top: 45%; z-index: 1000000; font-size: 54px; opacity: 0.2;cursor: pointer; } .isleft:hover , .isright:hover{ opacity: 0.5; } .isleft i , .isright i { } 
</style>           
<div id="readfile" class="modal" style="    width: 80%!important;" >
			<a onclick="$('.modal-overlay').trigger('click');" style="       background: transparent;    color: red;    font-weight: bold;    font-size: 35px;	    font-family: cursive;    width: 30px;    height: 30px;    top: 0px;    right: 20px;    position: absolute;    left: unset;    bottom: unset;    display: block;    cursor: pointer;" >x</a>
			<iframe class="iframereadfile" src="" width="100%" style="height:auto;    min-height: 550px;" ></iframe>
			<div class="isleft" ><i class="fas fa-chevron-left"></i></div>
			<div class="isright" ><i class="fas fa-chevron-right"></i></div>		
</div>	
<?php
// var_dump($sortdate); die;
// FOOTER
include 'tpl/footer.php';
?>
