<?php
$this->load->view("main/admin/header");
$page = $this->input->get("page");
$numberofpage = floor($counter / $limit) ;
if($counter % $limit > 0){
	$numberofpage++;
}
?>
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/datepicker/bootstrap-datepicker.min.css" />
<style>
	.btn-success{
		margin-bottom:10px;
	}
	.left{
		float: left;
		margin-bottom:10px;
	}
	.right{
		float: right;
	}
	.icons{
		text-align: center;
	}
	.expire{
		color: #f00 !important;
	}
	.fewday{
		color: rgb(213,190,0) !important;
	}
	.demo{
		color: #00f !important;
	}
	.finance,.set_password{
	   width: 100%;
	   height: 100%;
	   position: fixed;
	   top: 0;
	   left: 0;
	   z-index: 3000;
	   background: rgba(0,0,0,0.5);
	   
}
.closer{
	float: left !important;
	line-height: 35px;
	color: #f00 !important;
	width: 30px;
}
.closer button{
	border: 0px solid #ccc;
	background:transparent;
	border-radius: 4px 0 0 0;
	width: 100%;
	color: #fff;
}
.closer button:hover{
	color: #000;
}
.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default{
		border: 1px solid #ccc !important;
		border-radius: 7px  !important;
	}
	.ui-state-default:hover, .ui-widget-content .ui-state-default:hover, .ui-widget-header .ui-state-default:hover{
		background:#08c !important;
		color: #fff;
	}
	.ui-state-active, .ui-widget-content .ui-state-active, .ui-widget-header .ui-state-active{
		background:#08c !important;
		
	}
	textarea{
		height: 100px !important;
		resize: none;
	}
	.jamehkol{
		background: #ccc;
	}
	table , th{
		text-align: center !important;
	}
	.panel{
		margin-top:10px;
	}
</style>
<div class="col-lg-12">
			  	<div class="finance" data_bazaryab_id="" data_id="" style="display:none;">
	   <div class="modal-dialog">
		<section class="panel">
                            <header class="panel-heading tab-bg-dark-navy-blue" style="background:rgb(200,107,248)">
                                <ul class="nav nav-tabs">
                                    <li>
                                        <a href="#home-2" data-toggle="tab">
                                            <i class="icon-calendar"></i>
											شارژ مجدد
                                        </a>
                                    </li>
                                    <li>
                                        <a href="#about-2" data-toggle="tab">
                                            <i class="icon-bar-chart"></i>
                                           درآمدها
                                      </a>
                                    </li>
                                    <li class="active">
                                        <a href="#contact-2" data-toggle="tab">
                                            <i class="icon-envelope"></i>
                                            فرستادن پیغام
                                      </a>
                                    </li>
									<li class="closer">
										<button class="" >
                                        <i class="icon-remove"></i>
                                    </button>
									</li>
                                </ul>
								
                            </header>
                            <div class="panel-body">
                                <div class="tab-content">
                                    <div class="tab-pane" id="home-2">
                                        <div class="form-group">
                                        <label for="exampleInputEmail1">شارژ مجدد</label>
                                        <input class="form-control" id="date" type="email" placeholder="تاریخ">
                                    </div>
                                  <div class="form-group">
                                        <label for="exampleInputEmail1">مبلغ پرداختی</label>
                                        <input class="form-control nicenumber" type="email" placeholder="تومان">
                                    </div>
								  <button class="btn btn-info">ذخیره</button>
                                    </div>
                                    <div class="tab-pane" id="about-2">
								<table class="table table-striped border-top dataTable" id="sample_1" aria-describedby="sample_1_info">
                                <thead>
                                    <tr role="row"><th class="sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Username: activate to sort column ascending" style="width: 210px;">تاریخ</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending" style="width: 396px;">نام بازاریاب</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending" style="width: 396px;">مبلغ فروش</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Points: activate to sort column ascending" style="width: 323px;">سهم بازار یاب</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Joined: activate to sort column ascending" style="width: 216px;">سهم شرکا</th>
									</tr>
                                </thead>
                                
                            <tbody role="alert" aria-live="polite" aria-relevant="all"><tr class="gradeX even">
                                       
                                       
                                        
                                    </tr></tbody></table>
									</div>
                                    <div class="tab-pane active" id="contact-2">
										 <label for="exampleInputEmail1">متن پیام خود را وارد کنید</label>
                                       <textarea class="form-control"></textarea>
									   <button class="btn btn-danger" style="margin-top:10px;">فرستادن پیام</button>
										
									</div>
                                </div>
                            </div>
                        </section>
                                    </div>
</div>
<div class="set_password" style="display: none;">
	   <div class="modal-dialog">
		<input type="hidden" id="ajaxupdater" value="" >
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                                                <h4 class="modal-title">تغییر رمز کاربر</h4>
                                            </div>
                                            <div class="modal-body">
												 <input type="hidden" id="hidden_path" name="path" value="" />
												
												
												
                                            
										   <div id="folder">
											<form action="<?php echo base_url(); ?>admin/update_user" method="post">
											<input type="hidden" id="id_content" name="id" value="" >
                                                    <div class="form-group">
                                                        <label for="change_password">سطح دسرسی</label>
                                                    <select type="text" class="form-control permision" name="permision" style="padding:0px;">
													<option value="4">پزشک</option>
													<option value="3">منشی</option>
													<option value="2">کاربر ربات</option>
													<option value="1">مدیریت مشتری</option>
												</select>
                                                    </div>
													<div class="form-group">
                                                        <label for="change_password">ربات تلگرام</label>
                                                        <select class="form-control whatbot" name="whatbot" style="padding:0px">
															<option class="pationt" value="-1" style="display: block;" selected="selected">بدون گروه ربات</option>
															<option class="pationt" value="1" style="display: block;">ربات پزشکان</option>
															<?php foreach($robats as $value){ ?>
															<option value="<?php echo $value->name_id; ?>" style="display:none" class="bot_group"><?php echo $value->name; ?></option>
															<?php } ?>
														</select>
                                                    </div>
													<div class="form-group">
                                                        <label for="change_token">توکن ربات تلگرام</label>
                                                        <input type="text" name="rotoken" class="form-control bottoken" id="change_token">
                                                    </div>
													<div class="form-group">
                                                        <label for="change_password">نوع آبونمان</label>
                                                        <select type="text" name="typeofpay" class="form-control tofpay" style="padding:0px;">
													<option value="0">شارژی</option>
													<option value="1">دائمی</option>
													<option value="2">دمو</option>
												</select>
                                                    </div>
													<div class="form-group expire_date">
                                                        <label for="change_password">تاریخ انقضآ</label>
                                                        <input type="text" name="expdate" class="form-control" id="exp_date">
                                                    </div>
													<div class="form-group">
                                                        <label for="change_password">رمز عبور</label>
                                                        <input type="text" name="password" class="form-control" id="change_password">
                                                    </div>
													
													
                                                   </div>
										  
                                                    <button type="submit" class="btn btn-info set_calender" id="button_creator">ثبت</button>
                                            </div>
										   
                                        </div>
											</form>
                                    </div>
</div>
	<a href="<?php echo base_url(); ?>admin/createuser"><button class="btn btn-success">ایجاد کاربر جدید <i class="icon-user"></i></button></a>

	<div class="col-lg-6 col-sm-6 left">
                    <div class="search_holder">
                                                <button class="col-lg-3 searchbtn"><i class="icon-search icon-flip-horizontal" aria-hidden="true"></i> جستجو</button>
                                                <div style="float: left;width: 60%;margin-bottom: 10px;">
												<input class="form-control searchinput" type="text" placeholder="جستجو بر اساس نام کاربر">
												</div>
                                            </div>  
                  </div>
<section class="panel">
	
        <header class="panel-heading">لیست کاربران</header>
		<div class="col-lg-12">
			<?php if($this->session->flashdata("type")== "succ"){ ?>
		<div class="alert alert-success fade in" style="margin-top:10px;">
                                    <button class="close close-sm" type="button" data-dismiss="alert">
                                        <i class="icon-remove"></i>
                                    </button>
                                    کاربر جدید با موفقیت ذخیره شد.
                             
                                </div>
		<?php } ?>
		<?php if($this->session->flashdata("type")== "succup"){ ?>
		<div class="alert alert-success fade in" style="margin-top:10px;">
                                    <button class="close close-sm" type="button" data-dismiss="alert">
                                        <i class="icon-remove"></i>
                                    </button>
                                 تغییرات با موفقیت اعمال شد.
                             
                                </div>
		<?php } ?>
		<?php if($this->session->flashdata("isbot")== "yes"){ ?>
		<div class="alert alert-warning fade in">
                                    <button class="close close-sm" type="button" data-dismiss="alert">
                                        <i class="icon-remove"></i>
                                    </button>
                                   <a href="<?php echo $this->session->flashdata("active_link") ?>" target="_blank">برای فعال شدن ربات خود لطفا کلیک کنید</a>
                             
                                </div>
		<?php } ?>
		</div>
                            <div class="panel-body">
								<div id="sample_1_wrapper" class="dataTables_wrapper form-inline" role="grid">
								<div class="row"><div class="col-sm-6"><div id="sample_1_length" class="dataTables_length"><label><select size="1" name="sample_1_length" aria-controls="sample_1" class="form-control limit"><option value="10" <?php $limit == 10 ? 'selected="selected"' : "" ?>>10</option><option value="25" <?php $limit == 25 ? 'selected="selected"' : "" ?>>25</option>
								<option value="50" <?php $limit == 50 ? 'selected="selected"' : "" ?>>50</option><option value="100" <?php $limit == 100 ? 'selected="selected"' : "" ?>>100</option></select> نمایش رکورد در صفحه</label></div></div>
								</div>
								<table class="table table-striped border-top dataTable" id="sample_1" aria-describedby="sample_1_info">
                                <thead>
                                    <tr role="row"><th class="sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Username: activate to sort column ascending" style="width: 100px;">ردیف</th><th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending" style="width: 200px;">نام کاربری</th><th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Points: activate to sort column ascending" style="width: 146px;">نام و نام خانوادگی</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label=": activate to sort column ascending" style="width: 150px;">نوع کاربر</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label=": activate to sort column ascending" style="width: 150px;">تاریخ ثبت</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label="Points: activate to sort column ascending" style="width: 146px;">تاریخ انقضآ</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label=": activate to sort column ascending" style="width: 150px;">نوع آبونمان</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label=": activate to sort column ascending" style="width: 150px;">بازاریاب</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label=": activate to sort column ascending" style="width: 150px;">شماره تلفن</th>
									<th class="hidden-phone sorting" role="columnheader" tabindex="0" aria-controls="sample_1" rowspan="1" colspan="1" aria-label=": activate to sort column ascending" style="width: 150px;"></th>
									</tr>
                                </thead>
                                
                            <tbody role="alert" aria-live="polite" aria-relevant="all">
								<?php $count =$offset+1; ?>
									<?php  foreach($result as $value){ ?>
									<?php
									if($value->id == 1)
									continue;
									?>
									<?php
									if($value->permision == 3){
										$expire = "-";
									}else{
										if($value->expire == 2000099999){
											$expire = "نامحدود";
										}else{
									$ex = (string)$value->expire;
									@$expire = $ex[0].$ex[1].$ex[2].$ex[3]."/".$ex[4].$ex[5]."/".$ex[6].$ex[7];
										}
									}
									?>
									<?php
									if($value->permision == 4){
										$typeofuser = "پزشک";
									}elseif($value->permision == 3){
										$typeofuser = "منشی";
									}elseif($value->permision == 2){
										$typeofuser = "ربات تلگرام";
									}elseif($value->permision == 1){
										$typeofuser = "مدیریت مشتری";
									}
									if($value->abuneman_type == 0 && $value->permision != 3){
										$typeofpey = "شارژِی";
									}elseif($value->abuneman_type == 1 && $value->permision != 3){
										$typeofpey = "دائمی";
									}elseif($value->abuneman_type == 2 && $value->permision != 3){
										$typeofpey = "دمو";
									}else{
										$typeofpey = "-";
									}
									
									?>
									<?php
									if($today > $value->expire){
										$isexpire = "expire";
									}elseif(($today+5) > $value->expire && ($today) <= $value->expire && $value->abuneman_type != 2){
										$isexpire = "fewday";
									}else if($value->abuneman_type == 2){
										$isexpire ="demo";
									}else{
										$isexpire ="";
									}
									?>
										<tr class="gradeX <?php echo $isexpire; ?> <?php echo $count %2 == 0 ? "even" : "odd" ?>" data_id="<?php echo $value->id; ?>">
                                        <td class="radife"><?php echo $count; ?></td>
                                        <td class="hidden-phone "><?php echo $value->username; ?></td>
										<td class="hidden-phone "><?php echo $value->full_name; ?></td>
                                        <td class="center hidden-phone "><?php echo $typeofuser; ?></td>
										<td class="center hidden-phone "><?php echo $value->sybmitdate; ?></td>
										 <td class="hidden-phone "><?php echo $expire; ?></td>
										<td class="center hidden-phone type"><?php echo $typeofpey; ?></td>
										<td class="center hidden-phone"><?php echo $value->bazaryab; ?></td>
										<td class="center hidden-phone"><?php echo $value->mo_number; ?></td>
										<td class="icons">
											<button class="btn btn-warning btn-xs" data_id="<?php echo $value->id ?>"><i class="icon-dollar"></i></button>
											<button class="btn btn-primary btn-xs" data_botgroup="<?php echo $value->botgroup; ?>"  data_token="<?php echo $value->tokenkey; ?>" data_id="<?php echo $value->id ?>"><i class="icon-edit"></i></button>
											<button class="btn btn-danger btn-xs" href="#myModal-1" data-toggle="modal"><i class="icon-trash "></i></button>
										</td>
									
                                    </tr>
									<?php $count++; } ?>						
											
																
							
							
							</tbody></table>
								<div class="row"><div class="col-sm-6"><div class="dataTables_info" id="sample_1_info">نمایش  <?php echo $offset+1; ?>  تا <?php echo ($offset)+$limit; ?> از <b><?php echo $counter; ?></b> رکرد</div></div>
							<div class="col-sm-6">
								<div class="dataTables_paginate paging_bootstrap pagination">
									<?php if(($page-1) <= 0){ ?>
									<ul><li class="prev disabled"><a href="javascript:void(0)"> → قبلی </a></li>	
									<?php }else{ ?>
									<ul><li class="prev"><a href="<?php echo base_url(); ?>/admin/userslist?limit=<?php echo $limit; ?>&page=<?php echo ($page-1); ?>"> → قبلی </a></li>	
									<?php } ?>
									<?php for($i=1;$i<=$numberofpage;$i++){  ?>
																		<li class="<?php echo $i == $page ? "active" : "" ?>"><a href="<?php echo base_url(); ?>/admin/userslist?limit=<?php echo $limit; ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
									<?php } ?>
									<?php if(($page) >= $numberofpage){ ?>
									<li class="next disabled"><a href="javascript:void(0)">بعدی ←</a></li></ul></div></div></div>
									<?php }else{ ?>
									<li class="next"><a href="<?php echo base_url(); ?>/admin/userslist?limit=<?php echo $limit; ?>&page=<?php echo ($page+1); ?>">بعدی ←</a></li></ul></div></div></div>
									<?php } ?>
							</div>
							</div>
</section>
</div>
<?php
$this->load->view("main/admin/footer");
?>
<script src="<?php echo base_url(); ?>assets/main/js/jquery.stepy.js"></script>
<script src="<?php echo base_url(); ?>assets/datepicker/bootstrap-datepicker.min.js"></script>
        <script src="<?php echo base_url(); ?>assets/datepicker/bootstrap-datepicker.fa.min.js"></script>
<script>
	$("#date").datepicker();
	$("#exp_date").datepicker();
	$(".btn-danger").click(function(){
		if(confirm("آیا از پاک کردن این اکانت اطمینان داری؟")){
		$id = $(this).parent().parent().attr("data_id");
		$value = $(this).parent().parent();
		$.post("<?php echo base_url() ?>admin/delete_user_control",{id:$id},function(e){
			if (e == "done") {
				$value.remove();
			}
			});
		}
		});
	$(".limit").change(function(){
		$value = $(this).val();
		window.location = "<?php echo base_url(); ?>/admin/userslist?limit="+ $value +"&page=1";
		});
</script>
<script>
	$(".close").click(function(){
		$(".set_password").css({"display":"none"});
		});
	$(".btn-primary").click(function(){
		$typeofkarbar = $(this).parent().parent().find("td").eq(3).text();
		$typeofabuneman = $(this).parent().parent().find("td").eq(6).text();
		$expdate =  $(this).parent().parent().find("td").eq(5).text();
		if ($expdate != "نامحدود") {
		$expdate = $expdate.split("/");
		$expdate = $expdate[2]+"/"+ $expdate[1]+"/"+$expdate[0];
		}

		//for karbar type
		if ($typeofkarbar == "پزشک") {
			$(".permision").val("4");
			$(".permision").change();
		}else if ($typeofkarbar == "ربات تلگرام") {
			$(".permision").val("2");
			$(".permision").change();
		}else if ($typeofkarbar == "مدیریت مشتری") {
			$(".permision").val("1");
			$(".permision").change();
		}
		//forabuneman type ;
		if ($typeofkarbar == "پزشک") {
			$(".permision").val("4");
			$(".permision").change();
		}else if ($typeofkarbar == "ربات تلگرام") {
			$(".permision").val("2");
			$(".permision").change();
		}else if ($typeofkarbar == "مدیریت مشتری") {
			$(".permision").val("1");
			$(".permision").change();
		}
		if ($typeofabuneman == "شارژی") {
			$(".tofpay").val("0");
			$(".tofpay").change();
		}else if ($typeofabuneman == "دائمی") {
			$(".tofpay").val("1");
			$(".tofpay").change();
		}else if ($typeofabuneman == "دمو") {
			$(".tofpay").val("2");
			$(".tofpay").change();
		}
		if($expdate != "نامحدود"){
			$("#exp_date").val($expdate);
		}
		$(".whatbot").val($(this).attr("data_botgroup"));
		$("#change_token").val($(this).attr("data_token"));
		$(".set_password").css({"display":"block"});
		$("#id_content").val($(this).attr("data_id"));
		});
	$(".closer").click(function(){
		$(".finance").css({"display":"none"});
		});
	$(".btn-warning").click(function(){
		$id = $(this).attr("data_id");
		if ($(this).parent().parent().find(".type").text() == "شارژِی") {
		$(".finance #home-2 input").removeAttr("disabled");
			$(".finance #home-2 button").removeAttr("disabled");
		}else{
			
			$(".finance #home-2 input").attr({"disabled":"disabled"});
			$(".finance #home-2 button").attr({"disabled":"disabled"});
		}
		$.post("<?php echo base_url(); ?>admin/send_finance",{"uid":$id},function(e){
		
		var $array = JSON.parse(e);
		var $html = "";
		var $kol_jam = 0;
		var $bazaryab_jam = 0;
		var	$partner_jam =0;
		if ($array.length > 0) {
		for($i=0;$i<$array.length;$i++){
			if (($i+1)%2 == 0) {
				$type = "even";
			}else{
				$type = "odd";
			}
			$html += '<tr class="gradeX'+ $type + '">';
            $html +=  '<td>'+$array[$i]["date"]+'</td>';
			$html += '<td class="hidden-phone ">'+ $array[$i]["bazaryab_name"] +'</td>';
            $html += '<td class="hidden-phone ">'+(Number($array[$i]["marketer_reward"])+Number($array[$i]["partners_profit"]))+'</td>';
            $html +=  '<td class="center hidden-phone ">'+$array[$i]["marketer_reward"]+'</td>';
			$html +=  '<td class="center hidden-phone ">'+$array[$i]["partners_profit"]+'</td>';
            $html +=  '</tr>';
			$bazaryab_jam += Number($array[$i]["marketer_reward"]);
			$partner_jam +=Number($array[$i]["partners_profit"]);
			$kol_jam += Number($array[$i]["marketer_reward"])+Number($array[$i]["partners_profit"])
		}
		}
		$html += '<tr>';
		$html += '<td colspan="2" style="background:#41cac0;text-align:center;color:#fff;">جمع کل</td>';
		$html += '<td style="background:#555;color:#fff;">'+$kol_jam+'</td>';
		$html += '<td style="background:#555;color:#fff;">'+$bazaryab_jam+'</td>';
		$html += '<td style="background:#555;color:#fff;">'+$partner_jam+'</td>';
		$html += '</tr>';
		$(".finance .tab-pane table tbody").html($html);
		$(".finance").attr({"data_id":$id}).css({"display":"block"});
		if ($array.length > 0) {
			$bazaryabcode = $array[0]["bazar_yab_code"];
		}else{
			$bazaryabcode = "null";
		}
		$(".finance").attr({"data_bazaryab_id":$bazaryabcode});
			});
		});
	$(".btn-info").click(function(){
		$uid = $(".finance").attr("data_id");
		$bid = $(".finance").attr("data_bazaryab_id");
		$date = $("#date").val();
		$price = $(".nicenumber").val();
		$.post("<?php echo base_url(); ?>admin/recharge_account",{"id":$uid,"date":$date,"price":$price,"bid":$bid},function(e){
			if (e == "done") {
				location.reload();
			}
			});
		});
</script>
<script>
	$(".searchbtn").click(function(){
		var $searchinput = $(".searchinput").val();
		window.location = "<?php echo base_url(); ?>admin/userslist?limit=10&page=1&search="+$searchinput;
		});
	 $(".searchinput").keypress(function(e){
		 var code = e.keyCode || e.which;
		if(code == 13) {
		var $searchinput = $(".searchinput").val();
		window.location = "<?php echo base_url(); ?>admin/userslist?limit=10&page=1&search="+$searchinput;
		}
		});
</script>
<script>
	$(".tofpay").change(function(){
		var $value = $(this).val();
		if ($value == 1 || $value == 2) {
			
			$(".expire_date").css({"display":"none"});
		}else{
			$(".expire_date").css({"display":"block"});
		}
		});
	$(".permision").change(function(){
		$value = $(this).val();
		if ($value == 4 || $value == 1) {
		$(".bot_group").css({"display":"none"});
		$(".pationt").css({"display":"block"});
		$(".pationt").eq(0).attr({"selected":"selected"});
		}else if ($value == 2) {
		$(".bot_group").css({"display":"block"});
		$(".pationt").css({"display":"none"});
		$(".bot_group").eq(0).attr({"selected":"selected"});
		}
		});
	
</script>