<?php
session_start();
$companyname=$_SESSION['companyname'];
$adminemailid=$_SESSION['emailid'];
include('../Model/configuration.php');

if(($adminemailid !="")&&($companyname!="")){
$sql='SELECT `id`, `pipe_name`, `pipe_percentage`, (case when (select count(sales_deal.id) from sales_deal where sales_deal.companyname="'.$companyname.'" and sales_deal.d_delete="no" and sales_deal.d_stage =sales_pipeline_type.id)>0 THEN "yes" else "no" end) as used, `pipe_created_by`, `pipe_created_type`, `pipe_datetime` FROM `sales_pipeline_type` WHERE `companyname`="'.$companyname.'" and pipe_deleted="no"';
$result=$pdo->query($sql);
?>
<!--<!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>-->
        
       <!-- <div class="container">-->
            
            <style>
                @media (max-width: 765px) {
                  img{
                      height:100%;
                      width:auto;
                      text-align: center;
                  }
                  
                }
            </style>
            
            <div class="col-sm-3" style="text-align:center;">
                <img src="Image/funnel_stages.jpg" width="100%" height="auto">
                <br>
                
                <div class="edit-stages-info" style="color:#999;">
                    <p>if remove a deal stage, it must be not used in deal. if it's used then you can rename but you cant delete the values in the % column.</p>
                </div>
                
            </div>
            <div class="col-sm-9">
                
                <div class="row">
                    
                    <div class="col-sm-4">
                        <span style="color:back;"><center><b>%</b></center></span>
                    </div>
                    
                    <div class="col-sm-8">
                        <span style="color:back;"><center><b>Stage name</b></center></span>
                    </div>
                    
                </div>
                <form>
                <?php
                $i=0;
                foreach($result as $row){
                    $id=$row['id'];
                    $pipe_name=$row['pipe_name'];
                    $pipe_percentage=$row['pipe_percentage'];
                    $used=$row['used'];
                    $pipeid="vel_id_".$i;
                    $pipename="vel_name_".$i;
                    $pipestage="vel_stage_".$i;
                    $pipetoprow="vel_common_".$i;
                    $pipe_array=explode("%", $pipe_percentage);
                ?>
                <span  id="<?php echo $pipetoprow;?>">
                    <div class="row">
                        
                        <div class="col-sm-4">
                            <span style="color:#999;">
                                <center>
                                    <input type="hidden" name="pipeid" id="<?php echo $pipeid;?>" value="<?php echo $id;?>" class="form-control" >  
                                    <input type="number" name="pipestage" min="1" max="2" id="<?php echo $pipestage;?>" value="<?php echo $pipe_array[0];?>" class="form-control" >  
                                </center>  
                            </span>
                        </div>
                        
                        <div class="col-sm-8">
                            <span style="color:#999;">
                                <center>
                                    <?php
                                    if($used!="yes"){
                                    ?>
                                    <div class="from-group">
                                        <div class="input-group">
                                            <input type="text" name="pipename" id="<?php echo $pipename;?>" value="<?php echo $pipe_name; ?>" class="form-control" >
                                            <div class="input-group-addon btn-warning" onclick=removestage("<?php echo $id;?>","<?php echo $pipetoprow;?>") style="background-color:orange;cursor:pointer;">-</div>
                                        </div>
                                    </div>
                                    <?php
                                    }else{
                                    ?>
                                    <input type="text" name="pipename" id="<?php echo $pipename;?>" value="<?php echo $pipe_name; ?>" class="form-control" >
                                    <?php
                                    }
                                    ?>
                                </center>
                            </span>
                        </div>
                        
                    </div>
                    <br>
                </span>
                <?php
                $i++;
                }
                ?>
                <input type="hidden" value="<?php echo $i;?>" id="total_i_value">
                <input type="hidden" value="" id="delete_value">
                
                <div id="pipeline_new"></div>
                
            </div>
            <br><br>
            <center>
                <input type="button" class="btn btn-primary" onclick="newsatge()" value="New Stage">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="submit" id="savepipestage" class="btn btn-success" onclick="submitpipelineedit()" value="Save">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </center>
            </form>
            <script>
                function removestage(id, hide){
                    $("#"+hide).html();
                    $("#"+hide).remove();
                    var delete_value=$("#delete_value").val();
                    if(delete_value==""){
                        delete_value=id;
                    }else{
                        delete_value=delete_value+"#@"+id;
                    }
                    $("#delete_value").val(delete_value);
                }
            </script>
<script>
var x=0;
    function newsatge(){
        
        var insert1="<span id='vel_"+x+"'>";
        insert1+='<div class="row" id="">';
            
            insert1+='<div class="col-sm-4">';
                insert1+='<span style="color:#999;">';
                    insert1+='<center>';
                        insert1+='<input type="hidden" name="pipe_id[]" class="form-control" >';
                        insert1+='<input type="number" name="pipe_stage[]" min="1" max="2" maxlength="2" class="form-control" >'; 
                    insert1+='</center>';
                insert1+='</span>';
            insert1+='</div>';
            
            insert1+='<div class="col-sm-8">';
                insert1+='<span style="color:#999;">';
                    insert1+='<center>';
                        insert1+='<div class="from-group">';
                            insert1+='<div class="input-group">';
                                insert1+='<input type="text" name="pipe_name[]" class="form-control" >';
                                insert1+='<div class="input-group-addon btn-warning" onclick=removeappend("vel_'+x+'","'+x+'") style="background-color:orange;cursor:pointer;">-</div>';
                            insert1+='</div>';
                        insert1+='</div>';
                    insert1+='</center>';
                insert1+='</span>';
            insert1+='</div>';
            
        insert1+='</div>';
        insert1+='<br>';
        $("#pipeline_new").append(insert1);
        x++;
    }
</script>
<script>
    function removeappend(id1,x){
        $("#"+id1).html();
        $("#"+id1).remove();
    }
</script>
<script>
    function submitpipelineedit(){
        var total_i_value=$("#total_i_value").val();
        var delete_value=$("#delete_value").val();
        var delete_value_array=delete_value.split("#@");
        for(i=0;i<total_i_value;i++){
            var pipe_id="vel_id_"+i;
            var pipename="vel_name_"+i;
            var pipestage="vel_stage_"+i;
            pid=$("#"+pipe_id).val();
            pname=$("#"+pipename).val();
            percentage=$("#"+pipestage).val();
            if(pid==""){
                $("#"+pipe_id).focus();
                 $("#"+pipe_id).css("border-color","red");
                return;
            }else{
                 $("#"+pipe_id).css("border-color","green");
            }
            if(pname==""){
                $("#"+pipename).focus();
                 $("#"+pipename).css("border-color","red");
                return;
            }else{
                 $("#"+pipename).css("border-color","green");
            }
            if(percentage==""){
                $("#"+pipestage).focus();
                $("#"+pipestage).css("border-color","red");
                return;
            }else{
                 $("#"+pipestage).css("border-color","green");
            }
        }
        for(i=0;i<total_i_value;i++){
            var pipe_id="vel_id_"+i;
            var pipename="vel_name_"+i;
            var pipestage="vel_stage_"+i;
            pid=$("#"+pipe_id).val();
            pname=$("#"+pipename).val();
            percentage=$("#"+pipestage).val();
            percentage=percentage+"%";
            $.post("pipeline_edit_stages.php",{idno:pid,percentage:percentage,name:pname},function myresult(data){
            });
        }
        var check=false;
        var pipe_stage_val = $("input[name='pipe_stage[]']").map(function(){
            var checkval=$(this).val();
            if(checkval==""){
                $(this).focus();
                $(this).css("border-color", "red");
                check=true;
                swal("","stage must be less then 100","warning")
            }else if(checkval>99){
                $(this).val("");
                $(this).focus();
                $(this).css("border-color", "red");
                check=true;
            }
            return $(this).val();}).get();
        var pipe_name_val = $("input[name='pipe_name[]']").map(function(){
            var checkval=$(this).val();
            if(checkval==""){
                $(this).focus();
                $(this).css("border-color", "red");
                check=true;
            }else if(checkval.length<3){
                $(this).val("");
                $(this).focus();
                $(this).css("border-color", "red");
                check=true;
            }
            return $(this).val();}).get();
            if(check==true){
                return;
            }
            if(pipe_name_val.length>0){
                $.post("pipeline_edit_stages.php",{percentage_insert:pipe_stage_val,name_insert:pipe_name_val},function myresult(data){
                });
            }
            for(i=0;i<delete_value_array.length;i++){
                deleteno=delete_value_array[i];
                $.post("pipeline_edit_stages.php",{deleteno:deleteno},function myresult(data){
                });
            }
       setInterval(myTimer, 1000);
    }
    function myTimer() {
        window.location="pipeline_view.php";
    }
    function isNumber(n){
        return typeof n == 'number' && !isNaN(n - n);
    }
</script>
        <!--</div>-->
  <!--      
    </body>
    
</html>-->
<?php
}else{
echo "<script> location.replace('http://fieldcloud.in/Login.php') </script>";
}?>