<?php
include "../../pidbco/pidbjoin.php";
include "../ses_write.php";

   if(!isset($_SESSION))
   {
      session_start();
   }

    //fetching Client IP address
    function get_client_ip()
    {
        $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
    }
    $ip=get_client_ip();

?>






<?php
if(isset($_POST['btn_add_tele_data']))
{
    $phone    = strip_tags(trim($_POST['phone']));
    $per      = strip_tags(trim($_POST['per']));
    $comp     = strip_tags(trim($_POST['comp']));
    $phone2   = strip_tags(trim($_POST['phone2']));
    $country  = strip_tags(trim($_POST['country']));
    $state    = strip_tags(trim($_POST['state']));
    $dist     = strip_tags(trim($_POST['dist']));
    $loc = strip_tags(trim($_POST['loc']));
    $bdo = strip_tags(trim($_POST['bdo']));
    $email = strip_tags(trim($_POST['email']));
    $report = strip_tags(trim($_POST['report']));
    $status = strip_tags(trim($_POST['status']));
    $source = strip_tags(trim($_POST['source']));
    $today = date('Y-m-d');
    $tme = date('H:m:s');

    if(!empty($_SESSION['city']))
    {
        $loc2 = $_SESSION['city'];
    }
    else
    {
        $loc2 = "";
    }


    if(!empty($_POST['follow_up']))
    {
        $follow_up = strip_tags(trim($_POST['follow_up']));
    }
    else
    {
        $follow_up = $today;
    }
    /* check data exist */
    $stmt3 = $con->prepare("SELECT comp_name FROM tele_calls_det WHERE comp_name=?");
    $stmt3 -> bind_param("s",$comp);
    $stmt3 -> execute();
    $stmt3 -> store_result();
    $num = $stmt3 -> num_rows;


    $stmt4 = $con->prepare("Select phone FROM tele_calls_det WHERE phone = ?");
    $stmt4 -> bind_param("s",$phone);
    $stmt4 -> execute();
    $stmt4 -> store_result();
    $num1 = $stmt4 -> num_rows;

    if($phone2 != "")
    {
        $stmt5 = $con->prepare("Select phone2 FROM tele_calls_det WHERE phone2 = ?");
        $stmt5 -> bind_param("s",$phone2);
        $stmt5 -> execute();
        $stmt5 -> store_result();
        $num2 = $stmt5 -> num_rows;
    }
    if($num > 0 || $num1 >0 || $num2 >0)
    {
        if($num > 0)
        {
        	echo "Sorry! Company Name Already Exist";
			exit;
            
        }
        if($num1 > 0)
        {
            echo "Sorry! ! Phone number One Exist";
			exit;
        }
        if($num2 > 0)
        {
        	echo "Sorry! ! Phone number two Exist";
			exit;
        }

    }
    else
    {
        if(!empty($comp) && !empty($per) && !empty($phone) && !empty($status) && !empty($loc) && !empty($bdo) && !empty($report))
        {
          $stmt = $con->prepare("INSERT INTO tele_calls_det(entered_date, entered_time, entered_ip, entered_loc, comp_name, person_name, phone, phone2, country, state, district, location, bdo, email, status, follow_up,source) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
          $stmt->bind_param("ssssssssssssssiss",$today,$tme,$ip,$loc2,$comp,$per,$phone,$phone2,$country,$state,$dist,$loc,$bdo,$email,$status,$follow_up,$source);
          $stmt->execute();
          $stmt->store_result();
          $ok = $stmt->affected_rows;
          if($ok > 0)
          {

              $last_id = $stmt->insert_id;
              $stmt11 = $con->prepare("INSERT INTO tele_reports(telecall_id, tele_bdo_id, entered_date, report) VALUES (?,?,?,?)");
              $stmt11->bind_param("iiss",$last_id,$bdo,$today,$report);
			  $stmt11->execute();
              $stmt11->close();
			  echo "Registration Completed Successfully";			  
              ?>

          <?php
             }
             else
             {
              
             }

          $stmt->close();
        }
        else
        {
          
        }
    }
 exit();
}
/*if(isset($_POST['btn_show_tele_data']))
{

  ?>
     <div class="dataTables_wrapper">
          <table class="table table-striped table-bordered table-hover table-responsive" id="dataTables-example">
              <thead>
              <tr>
                  <th>ID</th>
                  <th>Company</th>
                  <th class="hidden-xs">Person</th>
                  <th>BDO</th>
                  <th class="hidden-xs">Phone</th>
                  <th class="hidden-xs">Last Follow Up</th>
                  <th>Status</th>
              </tr>
              </thead>
              <tbody>
            <?php
              $stmt = mysqli_query($con,"SELECT id,comp_name,person_name,bdo,phone,follow_up,status FROM tele_calls_det ORDER BY id DESC ");
              //$i=1;
              while($res = mysqli_fetch_object($stmt))
              {
                  $fw = $res->follow_up;
                  list($year,$month,$day) = explode('-', $fw);
                  $id_bdo = $res->bdo;
                  $stmt11 = mysqli_query($con,"SELECT id,emp_name FROM employee_registration WHERE id='$id_bdo'");
                  $stmt11_res = mysqli_fetch_object($stmt11);
              ?>
              <tr class="odd gradeX" data-toggle="modal"  style="cursor: pointer;">
                  <td>
                      <?php
                      if($res->status == 2)
                      {
                          ?>
                          <input type="checkbox" class="checkbox"  id="<?php echo $res->id; ?>" style="cursor: pointer;" />
                      <?php
                      }
                      else
                      {
                          ?>
                          <input disabled type="checkbox" class="checkbox"  id="<?php echo $res->id; ?>" style="cursor: pointer;" />
                      <?php
                      }
                      ?>
                  </td>
                  <td onclick="edt_tele_call(<?php echo $res->id; ?>);" ><?php echo $res->comp_name; ?></td>
                  <td onclick="edt_tele_call(<?php echo $res->id; ?>);" class="hidden-xs"><?php echo $res->person_name; ?></td>
                  <td onclick="edt_tele_call(<?php echo $res->id; ?>);" ><?php echo $stmt11_res->emp_name; ?></td>
                  <td onclick="edt_tele_call(<?php echo $res->id; ?>);" class="hidden-xs"><?php echo $res->phone; ?></td>
                  <td onclick="edt_tele_call(<?php echo $res->id; ?>);" class="hidden-xs"><?php echo $day.' - '.$month.' - '.$year; ?></td>
                  <td onclick="edt_tele_call(<?php echo $res->id; ?>);" class="center"><?php if($res->status == 1){ echo 'Interested'; }elseif($res->status == 2){ echo 'Not-Interested'; }elseif($res->status == 3){ echo 'Future'; }elseif($res->status == 4){ echo 'Visit Scheduled'; } ?></td>
              </tr>
              <?php
                 // $i++;
              }
              $stmt->close();
              ?>
              </tbody>
          </table>
      </div>
<?php

}*/
if(isset($_POST['btn_upd_tele_data']))
{
    $phone = strip_tags(trim($_POST['phone']));
    $per  = strip_tags(trim($_POST['per']));
    $comp = strip_tags(trim($_POST['comp']));
    $phone2 = strip_tags(trim($_POST['phone2']));
    $country = strip_tags(trim($_POST['country']));
    $state = strip_tags(trim($_POST['state']));
    $dist = strip_tags(trim($_POST['dist']));
    $loc = strip_tags(trim($_POST['loc']));
    $bdo = strip_tags(trim($_POST['bdo']));
    $email = strip_tags(trim($_POST['email']));
    $report = strip_tags(trim($_POST['report']));
    $status = strip_tags(trim($_POST['status']));
    $source = strip_tags(trim($_POST['source']));
    $today = date('Y-m-d');
    $tme = date('H:m:s');



    if(!empty($_POST['follow_up']))
    {
        $follow_up = strip_tags(trim($_POST['follow_up']));
    }
    else
    {
        $follow_up = $today;
    }
    $cid = strip_tags(base64_decode($_POST['cid']));

    /* check data exist */
    $stmt3 = $con->prepare("SELECT comp_name FROM tele_calls_det WHERE comp_name=? AND id != ?");
    $stmt3 -> bind_param("si",$comp,$cid);
    $stmt3 -> execute();
    $stmt3 -> store_result();
    $num = $stmt3 -> num_rows;


    $stmt4 = $con->prepare("Select phone FROM tele_calls_det WHERE phone = ? AND id != ?");
    $stmt4 -> bind_param("si",$phone,$cid);
    $stmt4 -> execute();
    $stmt4 -> store_result();
    $num1 = $stmt4 -> num_rows;

    if($phone2 != "")
    {
        $stmt5 = $con->prepare("Select phone2 FROM tele_calls_det WHERE phone2 = ? AND id != ?");
        $stmt5 -> bind_param("si",$phone2,$cid);
        $stmt5 -> execute();
        $stmt5 -> store_result();
        $num2 = $stmt5 -> num_rows;
    }
    if($num > 0 || $num1 >0 || $num2 >0)
    {
        if($num > 0)
        {
            ?><script>alert("Error! Company Name Exist");</script><?php
        }
        if($num1 > 0)
        {
            ?><script>alert("Error! Phone number One Exist");</script><?php
        }
        if($num2 > 0)
        {
            ?><script>alert("Error! Phone number Two Exist");</script><?php
        }

    }
    else
    {
        if(!empty($comp) && !empty($per) && !empty($phone) && !empty($status) && !empty($loc) && !empty($bdo) && !empty($report) && !empty($cid))
        {
            $stmt = $con->prepare("UPDATE tele_calls_det SET comp_name=?,person_name=?,phone=?,phone2=?,country=?,state=?,district=?,location=?,bdo=?,email=?,status=?,follow_up=?,source=? WHERE id=?");
            $stmt->bind_param("ssssssssssssii",$comp,$per,$phone,$phone2,$country,$state,$dist,$loc,$bdo,$email,$status,$follow_up,$source,$cid);
            if($stmt->execute())
            {
               
                    $stmt11 = $con->prepare("INSERT INTO tele_reports(telecall_id, tele_bdo_id, entered_date, report) VALUES (?,?,?,?)");
                    $stmt11->bind_param("iiss",$cid,$bdo,$today,$report);
                    $stmt11->execute();
                    $stmt11->close();
               
                ?>
                <script>
                   window.location.href="Marketing-Telecalls";
                </script>
            <?php
            }
            else
            {
                ?><script>alert("Error1!.");</script><?php
            }
            $stmt->close();
        }
    }
exit();
}
if(isset($_POST['btn_delete_tele_data']))
{
  $count = "";$i=0;
  $checked = $_POST['checked'];
  $count = count($checked);
  if($count > 0)
  {
      while($i<$count)
      {
          $del_id = $checked[$i];
          mysqli_query($con,"DELETE FROM tele_calls_det WHERE id = '$del_id'");
          $i++;
      }
	  echo "Tele Calling Data Deleted";
  }
  else
  {
   
   
  }

    exit();
}




if(isset($_POST['btn_daily_report'])){
	$bdo = $_POST['bdo'];
	$rep_date = $_POST['rep_date'];
	$reportt = $_POST['reportt'];
	$date=date('Y-m-d');
	
    $stmt = $con->prepare("INSERT INTO `daily_rep_det` (`entered_date` ,`bdo_id` ,`report_date` ,`report`) VALUES ('$date','$bdo','$rep_date','$reportt')");
    $stmt->execute();
	echo "Report Submitted Successfully..";
	
}


if(isset($_POST['updt_btn_daily_report'])){
    $bdo = $_POST['bdo'];
	$rep_date = $_POST['rep_date'];
	$reportt = $_POST['reportt'];
	$upval = $_POST['upval'];
	$stmt = $con->prepare("UPDATE `daily_rep_det` SET `bdo_id` = '$bdo',`report_date` = '$rep_date',`report` = '$reportt' WHERE `id` ='$upval'");
    $stmt->execute();
	echo "Report Updated Successfully..";
		
}

if(isset($_POST['delet_btn_daily_report'])){
	$idd = $_POST['idd'];
	$stmt = $con->prepare("DELETE FROM `daily_rep_det` WHERE `id` = '$idd'");
    $stmt->execute();
	echo "Report Deleted Successfully..";
}


if(isset($_POST['btn_edt_report'])){
$id= $_POST['id'];
$qry15 = mysqli_query($con,"SELECT * FROM `daily_rep_det` WHERE id='$id'");
$qry15_fet = mysqli_fetch_object($qry15);

?>	
	 <div class="row">
                                <div class="col-md-12">
                                    <div class="col-md-3">
                                        <select class="bdo form-control col-md-7 col-xs-12" id="bdo" required="required" name="bdo">
                                            <option value=""></option>
                                            <?php
                                            $emp = mysqli_query($con,"SELECT id,emp_name FROM employee_registration");
                                            while($emp_list = mysqli_fetch_object($emp))
                                            {
                                            	if($qry15_fet->bdo_id == $emp_list->id){ ?>
                                            <option value="<?php echo $emp_list->id; ?>" selected><?php echo $emp_list->emp_name; ?></option>		
												<?php } else {	
                                                ?>
                                                <option value="<?php echo $emp_list->id; ?>"><?php echo $emp_list->emp_name; ?></option>
                                            <?php
                                            } }
                                            ?>
                                        </select>
                                    </div>
                                    <div class="col-md-6">

                                    </div>
                                    <div class="col-md-3">
                                        <input id="rep_date" class="form-control col-md-7 col-xs-12" placeholder="Enter Date"  type="date" name="rep_date" value="<?php echo $qry15_fet->report_date; ?>" >
                                    </div>
                               </div>
                            </div>
                         <br>   
                <div class="row">
                                <div class="col-md-12">
                                	<div class="col-md-11">
                                        <textarea class="col-md-12 form-control" placeholder="Enter Report" name="reportt" id="reportt"><?php echo $qry15_fet->report; ?></textarea>
                                     </div>
                                       <div class="col-md-1">
                                       	<input type="hidden" name="upval" id="upval" value="<?php echo $id; ?>" />
                                       	<input type="button" class="btn btn-primary " value="Update" style="float: right; margin-top: 40px;" onclick="updatereport();">
                                       	</div>  
                                </div>
                            </div>             

<?php	
}
?>
