<?php
$msg = '';

include("include/settings.php");
include("checksession.php");

$usermaster = $mysqli->prepare("select count(*) from cdi_usermaster where user_id=?");
$usermaster->bind_param("i", $_GET['userid']);
$usermaster->execute();
$usermaster->bind_result($membercount);
$usermaster->fetch();
$usermaster->close();

if ($membercount == 0) {
    header("location:index.php");
}

$memberdetailmaster = $mysqli->prepare("select * from cdi_usermaster where user_id=?");
$memberdetailmaster->bind_param("i", $_GET['userid']);
$memberdetailmaster->execute();
$memberdetailmaster = $memberdetailmaster->get_result();
$userrow = mysqli_fetch_object($memberdetailmaster);

if (isset($_POST['btnSaveMemberssss'])) {
    $_POST["txtType"] = 'sales_exec';
    $usermaster = $mysqli->prepare("update cdi_usermaster set target=? where user_id=?");
    $usermaster->bind_param("ii", $_POST['target'], $_GET['userid']);
    $usermaster->execute();
    $usermaster->close();
}

if (isset($_POST['btnSaveMember'])){
    
    $userid = $userrow->user_id;
    
  // $mysqli=new MySQLi("localhost","callcer1_rishta","Password@12","callcer1_rishta"); 
   $mysqli=new MySQLi("localhost","rishtadepot_new","Sailor@#321","rishtadepot_live");  
   
	date_default_timezone_set('Asia/Kolkata');
	$thisdate=date("Y-m-d H:i:s");
	

	
//	$usermaster=$mysqli->prepare("INSERT INTO rishta_target(user_id, month, year, target, created_at) VALUES (?,?,?,?,?)");
//				$usermaster->bind_param("issss",$userid,$_POST[month],$_POST[year],$_POST[target],$thisdate);
//				$usermaster->execute();
//				$usermaster->close();
				
$sql = "INSERT INTO rishta_target ( user_id, month,year,target,created_at)
VALUES ('$userid','$_POST[month]', '$_POST[year]','$_POST[target]','$thisdate')";

if ($mysqli->query($sql) === TRUE) {
//  echo "New record created successfully";
  $msg = "New record created successfully !";
 
} else {
  echo "Error: " . $sql . "<br>" . $mysqli->error;
}

$mysqli->close();


 // $query=mysqli_query($mysqli,"insert into rishta_target values ('','$userid','$_POST[month]','$_POST[year]','$_POST[target]','$thisdate')");
  
   //$msg = "New record created successfully !";
 
   
}

if (isset($_POST['btnSaveMembers'])) {

    if (isset($_POST['target']) && !empty($_POST['target'])) {

        $month = $_POST['month'];
        $year = $_POST['year'];
        $userid = $userrow->user_id;
        $target = $_POST['target'];

        $check_query = "SELECT * FROM rishta_target WHERE `user_id` = " . $userid . " AND `month` = '" . $month . "' AND `year` = '" . $year . "'  ";
        $result = mysqli_query($mysqli, $check_query);
        if (isset($result) && !empty($result)) {

            $row = mysqli_num_rows($result);

            if($row > 0) {

                $update_query = "UPDATE rishta_target SET `target` = '" . $target . "' WHERE `user_id` = " . $userid . " AND `month` = '" . $month . "' AND `year` = '" . $year . "' ";

                if (mysqli_query($mysqli, $update_query)) {
                    $msg = "Exist record successfully updated!";
                    header("refresh:2;url=set-target.php?userid=" . $userid);
                } else {
                    $msg = "Error: " . $sql . " " . mysqli_error($conn);
                    header("refresh:2;url=set-target.php?userid=" . $userid);
                }
            }else {

                $insert_query = "INSERT INTO rishta_target SET `user_id` = " . $userid . ", `month` = '" . $month . "', `year` = '" . $year . "', `target` = '" . $target . "'  ";

                if (mysqli_query($mysqli, $insert_query)) {
                    $msg = "New record created successfully !";
                    header("refresh:2;url=set-target.php?userid=" . $userid);
                } else {
                    $msg = "Error: " . $sql . " " . mysqli_error($conn);
                    header("refresh:2;url=set-target.php?userid=" . $userid);
                }
                mysqli_close($conn);
            }
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Set Targets - Rishta Depot </title>
  
		
<?php include('./include/meta.php'); ?>

</head>
	
<?php include('./include/header.php'); ?>
  
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
	  <div class="container-full">
		<!-- Content Header (Page header) -->
		<div class="content-header">
			<div class="d-flex align-items-center">
				<div class="me-auto">
					<h4 class="page-title">Reports</h4>
					<div class="d-inline-block align-items-center">
						<nav>
							<ol class="breadcrumb">
								<li class="breadcrumb-item"><a href="#"><i class="mdi mdi-home-outline"></i></a></li>
								<li class="breadcrumb-item" aria-current="page">Add Sale Target</li>
								<li class="breadcrumb-item active" aria-current="page">Reports</li>
							</ol>
						</nav>
					</div>
				</div>
				
			</div>
		</div>	  
     <?php
                    if (trim($msg) != null) {
                        ?>
                        <div class='row'>
                            <div class='col-md-12'>
                                <div class="alert alert-success errormessage" id="errormessage">
                                    <strong><?php echo $msg ?></strong>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                    ?>
		<!-- Main content -->
		<section class="content">
			<div class="row">			  
				<div class="col-12">
					<div class="box">
						<div class="box-header with-border">
						  <h3 class="box-title">Set Target</h3>
						</div>
						<!-- /.box-header -->
					    <form action="#" method="post" enctype="multipart/form-data">
							<div class="box-body">
								<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">Email Id</label>
									  
									     <input type="text" id="txtEmailId" name="txtEmailId" placeholder="Email Id" class="form-control" value="<?php echo $userrow->user_email_id ?>"/>
                            <input type="hidden" id="txtHfEmailId" name="txtHfEmailId" class="form-control" value="<?php echo $userrow->user_email_id ?>"/>
                       
									</div>
								  </div>
								  <div class="col-md-6">
									<div class="form-group">
								      <label class="form-label">Target</label>
								          <input type="text" maxlength="50" id="target" name="target" placeholder="target" class="form-control"/>
								    </div>
								  </div>
								</div>
								<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">Month</label><span class="required">*</span>
                            	 <select class="form-select" name="month" id="month">
                                <option value=''>--Select Month--</option>
                                <option selected value='1'>Janaury</option>
                                <option value='2'>February</option>
                                <option value='3'>March</option>
                                <option value='4'>April</option>
                                <option value='5'>May</option>
                                <option value='6'>June</option>
                                <option value='7'>July</option>
                                <option value='8'>August</option>
                                <option value='9'>September</option>
                                <option value='10'>October</option>
                                <option value='11'>November</option>
                                <option value='12'>December</option>
                                </select>
									</div>
								  </div>
								  <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">year</label>
									   <select class="form-select" name="year" id="year">
                              <option value=''>--Select Year--</option>
                                <option selected value='2021'>2021</option>
                                <option value='2022'>2022</option>
                                <option value='2023'>2023</option>
                                <option value='2024'>2024</option>

                            </select>
									</div>
								  </div>
								       <div class="row">
                        <!--div class="col-md-2">
                             <label>year <span class="required">*</span></label>
                        </div-->
                        <div class="col-md-4">
                            <input type="hidden" maxlength="50" id="lastid" name="<?php echo $userrow->user_id ?>" class="form-control-custom" value="<?php echo $userrow->user_id ?>"/>
                       
					   </div>
                    </div>
								</div>
								<div class="box-footer">
								     <input type="submit" id="btnSaveMember" name="btnSaveMember" value="Save User" class="btn btn-primary"/> &nbsp;&nbsp;
                            <input type="button" id="btnBack" name="btnBack" value="Go Back" class="btn btn-primary" onclick="return back()"/>
							</div>
							</div>
						</form>
                    </div>
                </div>
			</div>
        </section>
	<script>

    function saveuser() {
        var txtName = $('#txtName').val();
        var txtPassword = $('#txtPassword').val();
        var txtConfirmPassword = $('#txtConfirmPassword').val();
        var txtEmailId = $('#txtEmailId').val();
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if ($.trim(txtName).length == 0 || $.trim(txtEmailId).length == 0) {
            alert("Please fill all the required fields.");
            return false;
        }
        else if (emailReg.test(txtEmailId) == false) {
            alert("Enter the valid email");
            return false;
        }
        else if (txtPassword != txtConfirmPassword) {
            alert("Enter The Correct Passwords");
            return false;
        }
        else if (txtPassword.length != 0 && ((txtConfirmPassword.length < 8 || txtConfirmPassword.length > 20) || (!/\d/.test(txtConfirmPassword)) || (!/[a-z]/.test(txtConfirmPassword)) || (!/[A-Z]/.test(txtConfirmPassword)) || (!/[!@#$%\^&*(){}[\]<>?/|\-]/.test(txtConfirmPassword)))) {
            msg = "Password must be 8-20 characters<br/>";
            msg += "Password must contain 1 digit<br/>";
            msg += "Password must contain 1 lower case character<br/>";
            msg += "Password must contain 1 upper case character<br/>";
            msg += "Password must contain 1 special character<br/>";
            $('.errormessage').html(msg);
            return false;
        }
        else {
            return confirm('Are You Sure?');
        }
    }

    function back() {
        var c = confirm('Are You Sure');
        if (c == true) {
            window.location = "sale-target.php";
        }
    }

    function isDecimal(evt, element) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode != 46 || $(element).val().indexOf('.') != -1) &&      // “.” CHECK DOT, AND ONLY ONE.
            (charCode < 48 || charCode > 57))
            return false;
        return true;
    }

    function isNumber(evt, element) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode < 48 || charCode > 57))
            return false;
        return true;
    }

    $(document).ready(function () {

        $('body').on("keypress", '#txtExperience', function () {
            return isDecimal(event, this)
        });

        $('body').on("keypress", '#txtCredits', function () {
            return isNumber(event, this)
        });

        $('#txtName').keypress(function (e) {
            var regex = new RegExp("^[a-zA-Z ]+$");
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) {
                return true;
            }

            e.preventDefault();
            return false;
        });

        $('input').bind("cut copy paste", function (e) {
            e.preventDefault();
        });

        $('body').on("keypress", '#target', function () {
            return isNumber(event, this)
        });
    });
</script>
<?php include('./include/footer.php'); ?>