<?php
session_start();
$companyname=$_SESSION['companyname'];
$adminemailid=$_SESSION['emailid'];
if(($adminemailid !="")&&($companyname!="")){
require_once('../Control/sales.php');
$objsales=new salesctrl();
?>
<?php
$sales_lead_res1=$objsales->salesselectall($companyname, $adminemailid, "sales_lead_source", "no");
$ij=0;
$leadid=array();
        foreach($sales_lead_res1 as $sales_lead_row1){
            $leadid[$ij]=$sales_lead_row1['id'];
            $ij++;
        }
       
if(isset($_POST['lead_id'])){
    $l_id=$_POST['lead_id'];
    $l_name=$_POST['lead_name'];

for($kk=0;$kk<count($leadid);$kk++){
    $check=1;
    $sc=$leadid[$kk];
    for($k=0;$k<count($l_id);$k++){
        if($l_id[$k]!=$sc){
            $check=1;
        }else{
            $check=0;
            break;
        }
}
//echo $check;
if($check==1){
     $objsales->salesdeletestatustagleadsource("sales_lead_source", $sc);
}
}
    for($i=0;$i<count($l_id);$i++){
        $id12=$l_id[$i];
        $name=$l_name[$i];
        $color="sd";
        $desc1="fg";
        if($id12==""){
            $rs=$objsales->salesinsertstatustagleadsource("sales_lead_source",$companyname, $adminemailid, $name, $desc1, $color, $adminemailid, "admin", "insert", $id12);
            //print_r($rs);
        }else{
            $objsales->salesinsertstatustagleadsource("sales_lead_source",$companyname, $adminemailid, $name, $desc1, $color, $adminemailid, "admin", "update", $id12);
        }
    }
    echo "<script> top.location.replace('dashboard.php') </script>";
}
?>
<html>
    <head>
        <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="../../Bootstrap/bootstrapAlert.js"></script>
        <link rel="stylesheet" href="Css/addcontact.css">    
        <!-- FormValidation CSS file -->
        <link rel="stylesheet" href="../../Formvalidation/formValidation.min.css">
        <!-- FormValidation plugin and the class supports validating Bootstrap form -->
        <script src="../../Formvalidation/formValidation.min.js"></script>
        <script src="../../Formvalidation/framework.min.js"></script>

     <style>
         form{
             padding:10px 20%;
         }
         @media only screen and (max-width: 500px) {
            form {
                padding: 30px;
            }
        }
     </style>   
    </head>
    
    <body>
        <span  class="view">
        <div style="margin:10px 10px 10px 10px;border-style: solid;">
        <div class="navbar-inverse" style="color:white;text-align:center;font-size:1.5em;">Update Lead</div>
        <br>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="leadForm" method="post">
        <div class="field_wrapper_lead">
        <?php
        $sales_lead_res=$objsales->salesselectall($companyname, $adminemailid, "sales_lead_source", "no");
        //echo 'salesselectall('.$companyname.', '.$adminemailid.', "sales_lead_source", "no")';
        $cc=$sales_lead_res->rowCount();
        //echo $cc;
        $i=1;
        foreach($sales_lead_res as $sales_lead_row){
            $lead_name=$sales_lead_row['lead_name'];
            $lead_id=$sales_lead_row['id'];
        $sales_lead_res2=$objsales->salesselectstatustagleadsourceteamstream("leadsource", $lead_id);
        $cc1=$sales_lead_res2->rowCount();
        //echo $cc1;
            ?>
                <div class="row">
                    <div class="col-md-12">
                        <input type="hidden" name="lead_id[]"  value="<?php echo $lead_id;?>">
                    <?php if($cc1==0){?>
                    <div class="form-group">
                        <div class="input-group"><?php } ?>
                        <input type="text" name="lead_name[]" id="lead_name" class="form-control" value="<?php echo $lead_name;?>">
                        <?php if($cc1==0){?>
                        <div class="input-group-addon" style="background-color:red;color:white;cursor:pointer;" id="remove_button_lead">x</div>
                        
                        </div> 
                    </div><?php } ?></div><br><br>
                </div>
            <?php
            $i++;
            }
            ?>
            </div>
            <span style="color:Blue;cursor:pointer;" id="add_button_lead" style="background-color:green">Add another status</span>
            
            <center><input type="submit" class="btn btn-success btn-lg" value="Save"></center>
            </form>
            </div>
            </span>
            
                        
            
                        <script type="text/javascript">
                            $(document).ready(function(){
                                var maxField = 10; //Input fields increment limitation
                                var addButton_url_1 = $('#add_button_lead'); //Add button selector
                                var wrapper__url_1 = $('.field_wrapper_lead'); //Input field wrapper
                                var inputurl1 = '<div class="row">';
                                inputurl1 +='<div class="col-md-12"><input type="hidden" name="lead_id[]"><div class="form-group"><div class="input-group">';
                                inputurl1 +='<input type="text" name="lead_name[]" id="lead_name" class="form-control">';
                                inputurl1 +='<div class="input-group-addon" style="background-color:red;color:white;cursor:pointer;" id="remove_button_lead">x</div>';
                                inputurl1 +='</div></div></div><br></div>'; //New input field html 
                                var xurl1 = 1; //Initial field counter is 1
                                $(addButton_url_1).click(function(){ //Once add button is clicked
                                    if(xurl1 < maxField){ //Check maximum number of input fields
                                        xurl1++; //Increment field counter
                                        $(wrapper__url_1).append(inputurl1); // Add field html
                                    }
                                });
                                $(wrapper__url_1).on('click', '#remove_button_lead', function(e){ //Once remove button is clicked
                                    e.preventDefault();
                                    $(this).parent('div').parent('div').parent('div').parent('div').remove(); //Remove field html
                                    xurl1--; //Decrement field counter
                                });
                            });
                        </script>
                        
<script>
    
$(document).ready(function() {
    $('#leadForm').formValidation({
        //message: 'Invalid',
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
        fields: {
            'lead_name[]': {
                validators: {
                    notEmpty: {
                        message: 'status Name is required and cannot be empty'
                    }
                }
            }
   
        }  
    })
        .on('success.field.fv', function(e, data) {
            if (data.fv.getInvalidFields().length > 0) {    // There is invalid field
                data.fv.disableSubmitButtons(true);
            }
        });
});
               
</script>

    </body>
</html>
<?php
}else{
echo "<script> location.replace('http://fieldcloud.in/Login.php') </script>";
}?>