<?php
session_start();
$companyname=$_SESSION['companyname'];//;
$adminemailid=$_SESSION['emailid'];//;
if(($adminemailid !="")&&($companyname!="")){
include('../Model/configuration.php');
require_once('../Control/sales.php');
$objsales=new salesctrl();
require_once('../Control/employee.php');
$objemployee=new employeectrl();
require_once('../Control/pipeline.php');
$objpipeline=new pipelinectrl();
$emp_res=$objsales->salesselectall($companyname, $adminemailid, "signupdetails", "no");
//print_r($emp_res);
$j=0;
foreach($emp_res as $emp_row){
 $emp_emailid_a[$j++]=$emp_row['emailid'];   
}


?>
<!doctype html>
<html>
    <head>
    
        <link rel="icon" href="../../Images/cloud-icon-27.png">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../../Bootstrap/css/bootstrap.min.css">
        <script src="../../Bootstrap/jquery.min.js"></script>
        <script src="../../Bootstrap/js/bootstrap.min.js"></script>
        <script src="../../Bootbox/bootbox.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-sweetalert/1.0.1/sweetalert.min.js"></script>
                       
    </head>
    
    <body>
        <input type="hidden" id="condition_button" value="123"/>
                 <input type="hidden" id="startmonth" name="startmonth" value="<?php echo date('m');?>">
                    <input type="hidden" id="startmonth1" name="startmonth1" value="<?php echo date('Y');?>">
                    <input type="hidden" id="startmonth2" name="startmonth2" value="<?php echo date('Y');?>">
        <div class="row">
            
            <div class="col-md-3">
                
            </div>

            <div class="col-sm-9" id="monthlyshow">
                <!--<div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                    <button id="m11" class="btn btn-default pull-right" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php if(date('m')<7){ echo "Jan"; }else{ echo "Jul"; } ?>&nbsp;&nbsp;<?php echo $prevmonth = date('Y', strtotime('-1 months'));?></b></button>
                    <button class="btn btn-primary pull-left" onclick="previewsrecordmonth()" style="display:inline;padding:5px;"><i class="glyphicon glyphicon-triangle-left"></i></button>
                </div>
                <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                    <center><button id="m22" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php if(date('m')<7){ echo "Feb"; }else{ echo "Aug"; } ?>&nbsp;&nbsp;<?php echo $prevmonth = date('Y');?></b></button></center>
                </div>
                <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                    <center><button id="m33" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php if(date('m')<7){ echo "Mar"; }else{ echo "Sep"; } ?>&nbsp;&nbsp;<?php echo $prevmonth = date('Y', strtotime('1 months'));?></b></button></center>
                </div>
                <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                    <center><button id="m44" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php if(date('m')<7){ echo "Apr"; }else{ echo "Oct"; } ?>&nbsp;&nbsp;<?php echo $prevmonth = date('Y', strtotime('2 months'));?></b></button></center>
                </div>
                <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                    <center><button id="m55" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php if(date('m')<7){ echo "May"; }else{ echo "Nov"; } ?>&nbsp;&nbsp;<?php echo $prevmonth = date('Y', strtotime('3 months'));?></b></button></center>
                </div>
                <div class="col-sm-2" style="padding:5px 0px;">
                    <center><button id="m66" class="btn btn-default pull-left" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php if(date('m')<7){ echo "Jun"; }else{ echo "Dec"; } ?>&nbsp;&nbsp;<?php echo $prevmonth = date('Y', strtotime('4 months'));?></b></button></center>
                    <button  class="btn btn-primary pull-right" onclick="nextrecordmonth()" style="display:inline;padding:5px;"><i class="glyphicon glyphicon-triangle-right"></i></button>
                </div>-->
                 
                            <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                                <button id="m11" class="btn btn-default pull-right" next_month="<?php echo date('M',strtotime($month .'-2 month')); ?>" my_year="<?php echo date('M',strtotime($month .'-2 month'));?>"  style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php echo date('M',strtotime($months .'-2 month')); ?>&nbsp;&nbsp;<?php echo date('Y',strtotime($months .'-2 month'));?></b></button>
                                <button class="btn btn-primary pull-left" onclick="previewsrecordmonth()" style="display:inline;padding:5px;"><i class="glyphicon glyphicon-triangle-left"></i></button>
                            </div>
                            <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                                <center><button id="m22" class="btn btn-default"  style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php echo date('M', strtotime($month .'-1 month')); ?>&nbsp;&nbsp;<?php echo date('Y', strtotime($months .'-1 month')); ?></b></button></center>
                            </div>
                            <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                                <center><button id="m33" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php echo date('M'); ?>&nbsp;&nbsp;<?php echo date('Y'); ?></b></button></center>
                            </div>
                            <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                                <center><button id="m44" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php echo date('M', strtotime($month .'+1 month')); ?>&nbsp;&nbsp;<?php echo date('Y', strtotime($months .'+1 month')); ?></b></button></center>
                            </div>
                            <div class="col-sm-2" style="padding:5px 0px;border-right:1px solid #D5D5D5;">
                                <center><button id="m55" class="btn btn-default" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php echo date('M', strtotime($month .'+2 month')); ?>&nbsp;&nbsp;<?php echo date('Y', strtotime($months .'+2 month')); ?></b></button></center>
                            </div>
                            <div class="col-sm-2" style="padding:5px 0px;">
                                <center><button id="m66" class="btn btn-default pull-left" style="display:inline;padding:5px;border:none;background-color:#f6f6f6;"><b><?php echo date('M', strtotime($month .'+3 month')); ?>&nbsp;&nbsp;<?php echo date('Y', strtotime($months .'+3 month')); ?></b></button></center>
                                <button  class="btn btn-primary pull-right" onclick="nextrecordmonth()" style="display:inline;padding:5px;"><i class="glyphicon glyphicon-triangle-right"></i></button>
                            </div>
                            </div>
            
            
        </div>
            
            <?php
            for($lm=0;$lm<count($emp_emailid_a);$lm++){
                $email1=$emp_emailid_a[$lm];
                $employeename=$objemployee->salesemployeename($email1);
                $commom_res=$objpipeline->salescommontarget($companyname,$email1);
                $commomamount1=0;
                $string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $email1);
                foreach($commom_res as $commom_row){
                    $commomamount1=$commom_row['common_amt'];
                }
                if($commom_res->rowCount()>0){
                    $commomamount=$commomamount1;
                }else{
                    $commomamount=5000;
                }
            ?>
            <script>
                function showrefresh(hide1, show1){
                    $("#"+hide1).hide();
                    $("#"+show1).show();
                }
            </script>
            <div class="row" style="padding-top:5px;padding-bottom:5px;<?php if($lm%2==0){ ?>background-color:#f5f5f5; <?php }else{ ?>background-color:white;<?php } ?>">
            
                <div class="col-md-3">
                    <b><?php echo $employeename;?></b><br>
                    Default&nbsp; &nbsp; &#8360 &nbsp;<?php echo $commomamount;?>&nbsp;&nbsp; <span onclick="showrefresh('<?php echo "hide".$string;?>','<?php echo $string;?>')" id="<?php echo 'hide'.$string;?>"><i class="glyphicon glyphicon-pencil"></i></span>
                    <span style="display:none;" id="<?php echo $string;?>">
                        <input type="text" name="<?php echo $string.'common_amount_update';?>" value="<?php echo $commomamount;?>" id="<?php echo $string.'common_amount_update';?>">
                        <span  style="color:green;cursor:pointer;" onclick="commontargetfun('<?php echo $string.'common_amount_update';?>','<?php echo $email1;?>','<?php echo $commomamount;?>')"><i class="glyphicon glyphicon-refresh"></i></span>
                    </span>
                </div>
                
                
                <div class="col-md-9"  id="exe_result1">
                
                
                    <?php
                    for($hgf=-2;$hgf<4;$hgf++){
                        $i=date('m',strtotime($months.'+'.$hgf.' months'));
                        $year=date('Y',strtotime($months.'+'.$hgf.' months'));
                        
                        if(intval($i)==1){
                            $sqlans1='SELECT sum(t_1) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==2){
                            $sqlans1='SELECT sum(t_2) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==3){
                            $sqlans1='SELECT sum(t_3) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==4){
                            $sqlans1='SELECT sum(t_4) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==5){
                            $sqlans1='SELECT sum(t_5) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==6){
                            $sqlans1='SELECT sum(t_6) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==7){
                            $sqlans1='SELECT sum(t_7) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==8){
                            $sqlans1='SELECT sum(t_8) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==9){
                            $sqlans1='SELECT sum(t_9) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==10){
                            $sqlans1='SELECT sum(t_10) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else if(intval($i)==11){
                            $sqlans1='SELECT sum(t_11) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        }else{
                            $sqlans1='SELECT sum(t_12) as amount FROM `sales_employee_target1` WHERE `t_company`="'.$companyname.'" and `t_year`="'.$year.'" and `t_emp_id`="'.$email1.'"';
                        } 
                        $resultans1 = $pdo->query($sqlans1);
                        $traget_amount="";
                        foreach($resultans1 as $rowans1){
                            $traget_amount=$rowans1['amount'];
                        }
                        $traget_amount=round($traget_amount);
                        ?> 
                        <div class="col-md-2">
                            <?php 
                            if($traget_amount==0){ 
                            ?>
                                <input type="text" class="form-control" name="<?php echo $string.'@@@'.$i.'@@@'.$year;?>" onchange="getinputfunction('<?php echo $email1."@@@".$i."@@@".$year;?>','<?php echo $string."___".$i."___".$year;?>')" id="<?php echo $string.'___'.$i.'___'.$year;?>" readonly value="<?php echo $traget_amount;?>" >
                            <?php
                            }else{
                            ?>
                                <input type="text" class="form-control" name="<?php echo $string.'@@@'.$i.'@@@'.$year;?>" onchange="getinputfunction('<?php echo $email1."@@@".$i."@@@".$year;?>','<?php echo $string."___".$i."___".$year;?>')" id="<?php echo $string.'___'.$i.'___'.$year;?>" value="<?php echo $traget_amount;?>" >
                            <?php
                            }
                            ?>
                            
                        </div>
                    <?php
                    }
                    ?>
                </div>
            
            </div>
            <?php
            }
            ?>
            
            <textarea class="form-control" id="update_employee_target" style="display:none;"></textarea>
        
        
        <br>
        <center><input type="submit" onclick="savethetarget();" class="btn btn-primary"></center>
        
        <script>
            function getinputfunction(id,id1){
                var value=$("#"+id1).val();
                /*alert(value);
                alert(id);*/
                var text=$("#update_employee_target").val();
                var value1=text+""+id+"@@@"+value+"___";
                //alert(value1);
                $("#update_employee_target").val(value1);
                
            }
        </script>
        <script>
            function savethetarget(){
                var text=$("#update_employee_target").val();
                //alert(text);
                if(text!=""){
                $.ajax({
                   url:"pipeline_intermediate.php",
                   data:{text:text},
                   type:"post",
                   success:function(respond){
                       //alert(respond);
                      window.location.reload();
                   }
                });
                }
            }
        </script>

        <script>
            function commontargetfun(id, em, default1){
                var value=$("#"+id).val();
                //alert(value);
                if(default1!=value){
                    $.ajax({
                       url:"pipeline_intermediate.php",
                       data:{common:value, em:em},
                       type:"post",
                       success:function(respond){
                           //alert(respond);
                          window.location.reload();
                       }
                    });
                }
            }
        </script>
        
    </body>
    
</html>
<?php
}else{
echo "<script> location.replace('http://fieldcloud.in/Login.php') </script>";
}?>