<?php
$this->load->view("main/drs/header");
?>
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/datepicker/bootstrap-datepicker.min.css" />
<style>
	.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;
		
	}
	table{
		margin-top:38px;
		float: right;
	}
	
</style>
<div class="col-lg-12">
<section class="panel">
        <header class="panel-heading">مدیریت مالی</header>
                            <div class="panel-body">
								<div class="col-lg-4">
									<label>نوع جستجو</label>
								<select class="form-control typeofserach" style="padding:0px">
									<option value="date">جستجو بر اساس تاریخ</option>
									<option value="moshtari">جستجو بر اساس نام مشتری</option>
								</select>	
								</div>
								<div class="moshtari_name col-lg-4" style="display: none;">
									<label style="visibility: hidden">s</label>
								<input type="text" class="form-control" id="moshtari_name" placeholder="نام مشتری">
								</div>
								<div class="date col-lg-3">
									<label style="visibility: hidden">s</label>
									<input type="text" id="sdate" class="form-control" placeholder="تاریخ شروع" >
								</div>
								<div class="date col-lg-3">
									<label style="visibility: hidden">s</label>
									<input type="text" id="edate" class="form-control" placeholder="تاریخ پایان">
								</div>
								<div class="col-lg-2">
									<label style="visibility: hidden;width:100%">s</label>
									<button class="btn btn-info">جستجو</button>
								</div>
								<table class="table table-striped table-advance table-hover">
                                <thead>
									
                                    <tr>
                                        <th>نام مشتری</th>
                                        <th class="hidden-phone">تاریخ پرداخت</th>
                                        <th>هزینه ی ویزیت</th>
                                        <th>هزینه ی جراحی</th>
                                        <th>سایر هزینه ها</th>
										<th>جمع هزینه ها</th>
										<th>پرداختی توسط بیمه</th>
										<th>پرداختی مشتری</th>
                                    </tr>
                                </thead>
                                <tbody>
									<?php
									$surgery_pay = 0;
									$other_pay = 0;
									$insure_pay = 0;
									$visit_pay = 0;
									$client_pay = 0;
									$kol_pay = 0;
									?>
									<?php foreach($results as $value){ ?>
									<?php
									$surgery_pay += $value->surgery_pay;
									$other_pay += $value->other_pay;
									$insure_pay += $value->insure_pay;
									$visit_pay += $value->visit_pay;
									$client_pay += $value->client_pay;
									$kol_pay += ($value->surgery_pay+$value->other_pay+$value->visit_pay);
									?>
									<tr>
                                        <td><?php echo $value->client_name; ?></td>
                                         <td><?php echo $value->visit_date; ?></td>
										<td class="hidden-phone"><?php echo $value->visit_pay; ?></td>
                                        <td><?php echo $value->surgery_pay; ?></td>
                                        <td><?php echo $value->other_pay; ?></td>
                                        <td>
                                            <?php echo ($value->surgery_pay+$value->other_pay+$value->visit_pay); ?>
                                        </td>
										<td>
											<?php echo $value->insure_pay; ?>
										</td>
										<td>
											<?php echo $value->client_pay; ?>
										</td>
                                    </tr>
									<?php } ?>
                                </tbody>
								<?php if(count($results) > 0){ ?>
								<tfoot>
								<tr class="results">
									<td colspan="2" style="text-align:center;background:#ccc;color: #fff">جمع کل</td>
									<td style="background:#ccc;color: #fff"><?php echo $visit_pay; ?></td>
									<td style="background:#ccc;color: #fff"><?php echo $surgery_pay; ?></td>
									<td style="background:#ccc;color: #fff"><?php echo $other_pay; ?></td>
									<td style="background:#ccc;color: #fff"><?php echo $kol_pay; ?></td>
									<td style="background:#ccc;color: #fff"><?php echo $insure_pay; ?></td>
									<td style="background:#ccc;color: #fff"><?php echo $client_pay; ?></td>
									
								</tr>
								</tfoot>
								<?php } ?>
								
                            </table>
							</div>
</section>
</div>
<?php
$this->load->view("main/drs/footer");
?>
<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>
	$("document").ready(function(){
		$("#sdate").datepicker();
		$("#edate").datepicker();
		});
	$(".typeofserach").change(function(){
		$value = $(this).val();
		if($value == "moshtari")
		{	
			$(".date").css({"display":"none"});
			$(".moshtari_name").css({"display":"block"});
		}else{
			$(".date").css({"display":"block"});
			$(".moshtari_name").css({"display":"none"});
		}
		});
	$(".btn-info").click(function(){
		$value = $(".typeofserach").val();
		
		if ($value == "date") {
		$sdate = $("#sdate").val();
		$sdate = $sdate.split("/");
		$sdate = $sdate[2]+$sdate[1]+$sdate[0];
		$edate = $("#edate").val();
		$edate = $edate.split("/");
		$edate = $edate[2]+$edate[1]+$edate[0];
		$type ="date";
		$data = $sdate + "," + $edate;
		}else{
		$type="search";
		$data = $("#moshtari_name").val();
		}
		$.post("<?php echo base_url(); ?>panel/finance_update",{"type":$type,"data":$data},function(e){
			var $array = JSON.parse(e);
			$length = $array.length;
			$html = "";
			$htmlf = "";
			$pay0 = 0;
			$pay1 = 0;
			$pay2 = 0;
			$pay3 = 0;
			$pay4 = 0;
			$pay5 = 0;
			for($i=0;$i<$length;$i++){
			$html += "<tr><td>"+$array[$i]["client_name"]+"</td>";
			$html +=  "<td>"+ $array[$i]["visit_date"] +"</td>";
			$html += "<td class=\"hidden-phone\">"+$array[$i]["visit_pay"]+"</td>";
			$html += "<td>"+$array[$i]["surgery_pay"]+"</td>";
			$html += "<td>"+$array[$i]["other_pay"]+"</td>";
			$html += "<td>"+(Number($array[$i]["visit_pay"])+Number($array[$i]["surgery_pay"])+Number($array[$i]["other_pay"]))+"</td>";
			$html += "<td>"+$array[$i]["insure_pay"]+"</td>";
			$html += "<td>"+$array[$i]["client_pay"]+"</td></tr>";
			$pay0 += Number($array[$i]["visit_pay"]);
			$pay1 += Number($array[$i]["surgery_pay"]);
			$pay2 += Number($array[$i]["other_pay"]);
			$pay3 += Number($array[$i]["visit_pay"])+Number($array[$i]["surgery_pay"])+Number($array[$i]["other_pay"]);
			$pay4 += Number($array[$i]["insure_pay"]);
			$pay5 += Number($array[$i]["client_pay"]);
			}
			if ($length > 0) {
			$htmlf += '<tr class="results">'
			$htmlf += '<td colspan="2" style="text-align:center;background:#ccc;color: #fff">جمع کل</td>'
			$htmlf +=  '<td style="background:#ccc;color: #fff">'+$pay0+'</td>';
			$htmlf +=   '<td style="background:#ccc;color: #fff">'+$pay1+'</td>'
			$htmlf +='<td style="background:#ccc;color: #fff">'+$pay2+'</td>';
			$htmlf +='<td style="background:#ccc;color: #fff">'+$pay3+'</td>';
			$htmlf +='<td style="background:#ccc;color: #fff">'+$pay4+'</td>';
			$htmlf += '<td style="background:#ccc;color: #fff">'+$pay5+'</td></tr>';	
			}
	$("table tbody").html($html);
	$("table tfoot").html($htmlf);
			});
		});
</script>