<?php
if(!isset($_SESSION)) {  session_start(); } 
ob_start();
if (!isset($_SESSION["user_id"])) {
    header("location:../../logout.php");
    }
if (isset($_REQUEST["ct"])) {
    $output = array();
    $contract_id = $_REQUEST["ct"];
    include "../../actions/dbPDO.php";
    // include_once('../../op/aksat_wanted.php');
    $sql = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) tot_payed 
					,count(wanted) as tot_w , count(payed) as tot_in 
		FROM aksat_in WHERE contract_id = ?");
	$sql->bindParam(1,$contract_id);
	$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
				$tot_payed    = $row["tot_payed"];
				$tot_wanted	  = $row["tot_wanted"];
				$tot_w   	  = $row["tot_w"];
				$tot_in		  = $row["tot_in"];
                $output["remain"]      = $tot_w - $tot_in ;
                $output["tot_dept"]    = $tot_wanted - $tot_payed ;
  }

  $last_kest = $db->prepare("SELECT min(ak_id) as ak_id from aksat_in where contract_id = ? AND date_payed IS NULL");
  $last_kest->bindParam(1,$contract_id);
  $last_kest->execute();
  if($last_kest->rowCount()>0){$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
      $next_ak_id = $ak['ak_id']; }

$last_kest = $db->prepare("SELECT ak_id , wanted , start,ak_no from aksat_in where ak_id = ?");
$last_kest->bindParam(1,$next_ak_id);
$last_kest->execute();
if($last_kest->rowCount()>0){$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
    $next_ak_id = $ak['ak_id']; 
    $output["next_wanted"] = $ak['wanted'];
    $output["next_start"]  = $ak['start'];
    $start  = $ak['start']; 
}

    $cont = $db->prepare("SELECT * FROM aksat_contract where contract_id = ?");
    $cont->bindParam(1, $contract_id);
    $cont->execute();
if ($cont->rowCount() > 0) { $c_row       = $cont->fetch(PDO::FETCH_ASSOC);
        $carid       = $c_row["carid"];
        $cust_id     = $c_row["cust_id"];
        $cust_id2    = $c_row["cust_id2"];
        $father      = $c_row["father"];
        $branch      = $c_row["branch"];
        $closer      = $c_row["closer"];

        $output["contract_id"]      = $c_row["contract_id"];
        $output["used_area"]        = $c_row["used_area"];
        $output["rent_days"]        = $c_row["rent_days"];
        $output["caid"]             = $c_row["carid"];
        $output["cust_id"]          = $c_row["cust_id"];
        $output["cust_id2"]         = $c_row["cust_id2"];
        $output["start"]            = $c_row["start"];
        $output["end"]              = $c_row["end"];
        $output["day_cost"]         = $c_row["day_cost"];
        $output["km_out"]           = $c_row["km_out"];
        $output["km_in"]            = $c_row["km_in"];
        $output["time_out"]         = $c_row["time_out"];
        $output["time_in"]          = $c_row["time_in"];
        $output["free_kilo"]        = $c_row["free_kilo"];
        $output["kilo_cost"]        = $c_row["kilo_cost"];

        $output["tamm_expire"]      = $c_row["tamm_expire"];
        $output["addtion_cost"]     = $c_row["addtion_cost"];
        $output["addtion_desc"]     = $c_row["addtion_desc"];
        
        $output["status"]           = $c_row["status"];
        $output["comm"]             = $c_row["comm"];
    }

    // car data 
    $car = $db->prepare("SELECT carid ,brand, kind , model , concat(letters,' ', numbers) as PL,
                color , freekilo   FROM cars  WHERE carid = ? LIMIT 1");
    $car->bindParam(1, $carid);
    $car->execute();
    if ($car->rowCount() > 0) {
        $row = $car->fetch(PDO::FETCH_ASSOC);
        $output["brand"]        = $row["brand"];
        $output["kind"]         = $row["kind"];
        $output["model"]        = $row["model"];
        $output["PL"]           = $row["PL"];
        $output["color"]        = $row["color"];
        $output["freekm"]       = $row["freekilo"];
    }
        $naql = $db->prepare("SELECT insurance_no, insurance_kind , insurance_expire , insurance_money
                            FROM naql WHERE carid = ? LIMIT 1");
        $naql->bindParam(1, $carid);
        $naql->execute();
    if ($naql->rowCount() > 0) {
                $seg = $naql->fetch(PDO::FETCH_ASSOC);
            $output["insurance_no"] = $seg["insurance_no"];
            $ins_kind               = $seg["insurance_kind"];
            $insurance_money        = $seg["insurance_money"];
            $output["insurance_expire"]   = $seg["insurance_expire"];

    switch ( $ins_kind) {
            case '1':      $kind = "المسئولية المدنية تجاه الغير";   break;
            case '2':      $kind = "تغطية ضد الغير - وافي";           break;
            case '3':      $kind = "تأمين شامل";                       break;
            default :      $kind = "المسئولية المدنية تجاه الغير";
        }
       
        $output["insurance_kind"]     = $kind ;
        $output["insurance_money"]    = "قبول التغطية التأمينية الإضافية المقدمة من المؤجر-- 
        الوسام المعتمد وسداد قسط تحمل عن كل حادث يشمله التأمين ".$insurance_money." ريال ";
    }else{
        $output["insurance_kind"]     = "المسئولية المدنية تجاه الغير";
        $output["insurance_money"]    = "قبول التغطية التأمينية الإضافية المقدمة من المؤجر-- 
        الوسام المعتمد وسداد قسط تحمل عن كل حادث يشمله التأمين 3500 ريال ";
        $output["insurance_no"] = 0;
    }
    // custmoer hero data
    $c1 = $db->prepare("SELECT * FROM customers WHERE cust_id = ? LIMIT 1");
	$c1->bindParam(1,$cust_id);
	$c1->execute();
  if($c1->rowCount() > 0 ){
                $rc = $c1->fetch(PDO::FETCH_ASSOC);
        $output["cust_name"]       = $rc["cust_name"];
        $output["idSA"]            = $rc["idSA"];
        $output["nation"]          = $rc["nation"];
        $output["license"]         = $rc["license"];
        $output["lic_expire"]      = $rc["lic_expire"];
        $output["address"]         = $rc["address"];
        $output["phone"]           = $rc["phone"];
        $output["phone2"]          = $rc["phone2"];

        $output["working_woner"]   = $rc["working_woner"];
        $output["w_phone"]         = $rc["w_phone"];
        $output["w_phone2"]        = $rc["w_phone2"];
        $output["working_address"] = $rc["working_address"];
    }
    // second customer data
    if(!empty($cust_id2)){
        $c2 = $db->prepare("SELECT * FROM customers WHERE cust_id = ? LIMIT 1");
	    $c2->bindParam(1,$cust_id2);
	    $c2->execute();
    if($c2->rowCount()>0){  $rc2  = $c2->fetch(PDO::FETCH_ASSOC);
        $output["cust_name3"]     = $rc2["cust_name"];
        $output["idSA3"] 		  = $rc2["idSA"];
        $output["nation3"]        = $rc2["nation"];
        $output["license3"]       = $rc2["license"];
        $output["lic_expire3"]    = $rc2["lic_expire"];
        $output["address3"]       = $rc2["address"];
        $output["phone3"]         = $rc2["phone"];
        $output["phone23"]        = $rc2["phone2"];
        
    	$output["working_woner3"] = $rc2["working_woner"];
    	$output["w_phone3"]       = $rc2["w_phone"];
		$output["w_phone23"]      = $rc2["w_phone2"];
		$output["working_address3"] = $rc2["working_address"];
         }
    }else{
        $output["cust_id2"] = 0 ;
    }
    // branch and emp data 
        $emp = $db->prepare("SELECT branches.b_name , users.realName
                    FROM branches , users WHERE branchID = ? AND user_id = ? LIMIT 1");
        $emp->bindParam(1,$branch);
        $emp->bindParam(2,$father);
	    $emp->execute();
    if($emp->rowCount()>0){$mass = $emp->fetch(PDO::FETCH_ASSOC);
        $output["realName"]      = $mass["realName"];
        $output["branch"] 	     = $mass["b_name"];
        $output["user_id"] 	     = $father;
         }
         if(!empty($closer)){
         $emp2 = $db->prepare("SELECT branches.b_name , users.realName
                    FROM branches , users WHERE branchID = ? AND user_id = ? LIMIT 1");
        $emp2->bindParam(1,$branch);
        $emp2->bindParam(2,$closer);
	    $emp2->execute();
    if($emp2->rowCount()>0){$m2 = $emp2->fetch(PDO::FETCH_ASSOC);
        $output["closer"]      = $m2["realName"];
        $output["branch2"] 	   = $m2["b_name"];
         }}
         $output["last_start"]   = date('Y-m-d', strtotime($start. ' - 1  month'));
         echo json_encode($output);
        // echo "ffffff";
        //  echo '<pre>'; print_r($output) ; echo '</pre>';
}