<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$pag = $pag1 = $pag2 = $pag3 = $page = 1; $setLimit = 20;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
if(isset($_GET["pag"])) $pag = (int)$_GET["pag"];
$pageLimit2 = ($pag * $setLimit) - $setLimit; 
if(isset($_GET["pag1"])) $pag1 = (int)$_GET["pag1"];
$pageLimit3 = ($pag1 * $setLimit) - $setLimit; 
if(isset($_GET["pag2"])) $pag2 = (int)$_GET["pag2"];
$pageLimit4 = ($pag2 * $setLimit) - $setLimit; 
if(isset($_GET["pag3"])) $pag3 = (int)$_GET["pag3"];
$pageLimit5 = ($pag3 * $setLimit) - $setLimit; 

$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "", "s=s_q" => "", "s=s_c" => "", "s=s_f" => "", "s=s_p" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
//====End ==============
define('TITLE','Fields');

if(userRole()> 1) {
	header('location: index.php');	
}

$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
?>
<?php

 if(isset($_REQUEST['di'])) {
	$tdelete = mysqli_real_escape_string($server,$_REQUEST['di']);
	mysqli_query($server,"DELETE FROM `customfields` WHERE id = '$tdelete'") or die(mysqli_error($server));
	$_SESSION['message'] = $LANG['The item(s) has been deleted from the list'];
	header('location: customfields?err=0&i');
 }
?>
 <div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card blue">
            <div class="card-content white-text"><?=$LANG['custf_tip']?></div>
        </div>
    </div>
 </div>
 
<div class="row no-m-t no-m-b">
	<div class="col s12 m12 l12 responsive-tabs">
            
      <h5><?=$LANG['Custom Customer Fields']?></h5>
      <div class="card card-white <?php if(isset($_GET['i'])) echo ' responsive-tabs__panel--active'?>">
            <div class="card-content">
            	<div class="row no-m-t no-m-b">   
                
                     <div class="col s12 m6 l7">
                          <h5><?=$LANG['Custom Customer Fields']?></h5> 
                     <?php
                    $query="SELECT * FROM customfields WHERE customer_id = 0 ORDER BY id DESC limit $pageLimit2,$setLimit";
					$result = mysqli_query($server,$query) or die(mysqli_error($server));
					$x = mysqli_num_rows($result);
                    if($x < 1) {
                    ?><table>	   
                      <div class="col s12 m12 l12">
                            <div class="card blue lighten-5">
                                <div class="card-content">
                                     <h3 class="center-align"><i class="fa fa-exclamation-circle"></i></h3>
                                     <h5 class="center-align"><?=$LANG['You have not created any custom fields yet']?></h5>
                                </div>
                            </div>
                        </div>
                       </table> 
                      <?php } else { ?>  
                      <table class="responsive-table bordered">
                          <thead>
                            <tr>
                                <th data-field="date"><?=$LANG['Field Name']?></th>
                                <th data-field="status"><?=$LANG['Type']?></th>
                                <th date-field="requyored"><?=$LANG['Required']?></th>
                                <th data-field="act"></th>
                             </tr>
                          </thead>
                          <tbody>
                          <?php
                        while ($rows = mysqli_fetch_assoc($result)) { 
						?>
                             <tr>
                                 <td><?=$rows['name']?> </td>
                                 <td><?=$LANG[$rows['field_type']]?></td>
                                 <td><?=$LANG[$rows['required']]?></td>
                                  <td><a class="hoverable red-text" onclick="return confirm('<?=$LANG['delete_confirm']?>');" href="customfields?di=<?=$rows['id']?>"><?=$LANG['Delete']?></a></td>
                             </tr>
                       <?php } ?>   
                          </tbody>
                      </table>
                      <?php } ?>
                       <div class="col s12 m12 l12" style="margin-top: 5px;">
                          <?php displayPaginationN($setLimit,$page,$query,'i','');  ?>
                      </div>
                      <br><br>
                     </div>
                
                     <div class="col s12 m6 l5 yellow lighten-5" style="border:1px solid black">
                          <form action="" method="post" id="credit-form" onSubmit="return false;">
                          	<h5><?=$LANG['Add New Record']?></h5> 

                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="name" type="text" required class="validate" placeholder="" >
                                       <label for="name"><?=$LANG['Field Name']?></label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <select id="field_type" required class="validate">
                                        <option value="Text Box" ><?=$LANG['Text Box']?></option>
                                        <option value="Textarea" ><?=$LANG['Textarea']?></option>
                                   </select>
                                   <label><?=$LANG['Type']?></label>
                             </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <select id="required" required class="validate">
                                        <option value="Yes" ><?=$LANG['Yes']?></option>
                                        <option value="No" ><?=$LANG['No']?></option>
                                   </select>
                                   <label><?=$LANG['Required']?></label>
                             </div>
                           </div>
                            <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <select id="show_form" required class="validate">
                                        <option value="1" ><?=$LANG['Yes']?></option>
                                        <option value="0" ><?=$LANG['No']?></option>
                                   </select>
                                   <label><?=$LANG['Show on Sign-up Form']?></label>
                             </div>
                           </div>
                           <div class="card-action">
                           		<input id="customfield_id" type="hidden" value="<?="0"?>" >
                                <button id="customf-button" class="btn green darken-4" type="submit"><i class="fa fa-save"></i> <?=$LANG['Save Record']?> </button>
                                <span id="customf-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                           </div>
                        </form>
                     </div>
				</div>                
            </div>
      </div>     
        
</div>
