<?php
include 'config.php';

## Read value
$draw = 1;//$_POST['draw'];
$row = "1";//$_POST['start'];
$rowperpage = "10";//$_POST['length']; // Rows display per page
$columnIndex = "";//$_POST['order'][0]['column']; // Column index
$columnName = "";//$_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = "";//$_POST['order'][0]['dir']; // asc or desc
$searchValue = "";//mysqli_real_escape_string($con,$_POST['search']['value']); // Search value

## Search 
$searchQuery = " ";
if($searchValue != ''){
	$searchQuery = " and (emp_name like '%".$searchValue."%' or 
        email like '%".$searchValue."%' or 
        city like'%".$searchValue."%' ) ";
}

## Total number of records without filtering
$sel = mysqli_query($connect,"select count(*) as allcount from employee");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($connect,"select count(*) as allcount from employee WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

## Fetch records
$empQuery = "select * from employee WHERE 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$empRecords = mysqli_query($connect, $empQuery);
$data = array();

/*while ($row = mysqli_fetch_assoc($empRecords)) {
    $data[] = array(
    		"emp_name"=>$row['emp_name'],
    		"email"=>$row['email'],
    		"gender"=>$row['gender'],
    		"salary"=>$row['salary'],
    		"city"=>$row['city']
    	);
} */

$paginationHtml = '';
			$page_id= base64_encode(base64_encode(base64_encode(2))); //THIS IS FOR PAGE IDENTIFICATION IN BL FILE for active inactive
			$bck_flg = base64_encode(base64_encode(base64_encode(1)));
			$flg1 = base64_encode(base64_encode(base64_encode(1)));

			$SLCT = "SELECT party_det_id,sel_date FROM design_sel_det GROUP BY party_det_id,sel_date ORDER BY design_sel_det_id DESC ";
			$QURY = mysqli_query($connect,$SLCT);
			$loop="1";//$startFrom;

			if(mysqli_num_rows($QURY)>0)
			{
			while($FTCH = mysqli_fetch_object($QURY))
			{
				$pa_id	=	base64_encode(base64_encode(base64_encode($FTCH->party_det_id)));
				$sel_date = base64_encode(base64_encode(base64_encode($FTCH->sel_date)));

				$SLCT_dd = "SELECT COUNT(design_sel_det_id) AS sel_cnt,IFNULL(SUM(master_pieces),0) AS mas_pic FROM design_sel_det,design_det WHERE  party_det_id = '$FTCH->party_det_id' and `sel_date` ='$FTCH->sel_date' AND design_det.design_det_id = design_sel_det.design_det_id GROUP BY sel_date";
				$QURY_dd = mysqli_query($connect,$SLCT_dd);
				$FTCH_dd = mysqli_fetch_object($QURY_dd);
				$pcode_where='';
				/*if($pcode!=''){
					$pcode_where .=' and party_code="'.$pcode.'"';
				}*/

				$SLCT_E = "SELECT cmp_name,party_code FROM party_det WHERE party_det_id = '$FTCH->party_det_id' ".$pcode_where." "; 
				$QURY_E = mysqli_query($connect,$SLCT_E);
				$FTCH_E = mysqli_fetch_object($QURY_E);
				$REST_E = mysqli_num_rows($QURY_E);

				$SLCT_Ec = "SELECT COUNT(design_sel_det_id) AS des_cnt,IFNULL(SUM(master_pieces),0) AS mas_pic FROM design_sel_det,design_det WHERE party_det_id = '$FTCH->party_det_id' AND design_det.design_det_id = design_sel_det.design_det_id AND ord_status = 'yes' and `sel_date` ='$FTCH->sel_date'";

				$QURY_Ec = mysqli_query($connect,$SLCT_Ec);
				$FTCH_Ec = mysqli_fetch_object($QURY_Ec);

				$SLCTre = "SELECT party_det_id,sel_date FROM design_sel_det WHERE party_det_id = '$FTCH->party_det_id' GROUP BY STR_TO_DATE(sel_date,'%d-%m-%Y') ORDER BY design_sel_det_id DESC";

				$QURYre = mysqli_query($connect,$SLCTre);
				$nu_rows = 0;
				$rowsre = mysqli_num_rows($QURYre);
				if($rowsre>0){
				while($FTCHre = mysqli_fetch_object($QURYre))
				{
					$SLCTre_sub = "SELECT * FROM design_sel_det WHERE party_det_id = '$FTCHre->party_det_id' and sel_date = '$FTCHre->sel_date' and ord_status='no'";
					//echo $SLCTre;
					$QURYre_sub = mysqli_query($connect,$SLCTre_sub);
					if(mysqli_num_rows($QURYre_sub)>0)
					{
						$nu_rows++;
					}
				}
				}

				$SLCT_DES = "SELECT COUNT(des_sel_cancel_id) AS canc_cnt FROM des_sel_cancel WHERE party_det_id = '$FTCH->party_det_id' AND sel_date = '$FTCH->sel_date'";
				$QURY_DES = mysqli_query($connect,$SLCT_DES);
				$FTCH_DES = mysqli_fetch_object($QURY_DES);

			if($REST_E>0){
			 $data[] = array("sno"=>$loop,"pcode"=>$FTCH_E->party_code,"cmpname"=>$FTCH_E->cmp_name,"sel_date"=>$FTCH->sel_date,"ss"=>$FTCH_dd->sel_cnt,"ssS"=>$FTCH_dd->mas_pic,"aa"=>"aaaa");
			}
			 }
			 }
print_r($data);
## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);
