<?php
session_start();
$companyname=$_SESSION['companyname'];
$adminemailid=$_SESSION['emailid'];
if(($adminemailid !="")&&($companyname!="")){
require_once('../Control/sales.php');
$objsales=new salesctrl();
?>
<?php
$sales_status_res1=$objsales->salesselectall($companyname, $adminemailid, "sales_status", "no");
$ij=0;
$statusid=array();
        foreach($sales_status_res1 as $sales_status_row1){
            $statusid[$ij]=$sales_status_row1['id'];
            $ij++;
        }
if(isset($_POST['status_id'])){
    $s_id=$_POST['status_id'];
    $s_color=$_POST['status_color'];
    $s_name=$_POST['status_name'];
    $s_status_desc=$_POST['status_desc'];

for($kk=0;$kk<count($statusid);$kk++){
    $check=1;
    $sc=$statusid[$kk];
    for($k=0;$k<count($s_id);$k++){
        if($s_id[$k]!=$sc){
            $check=1;
        }else{
            $check=0;
            break;
        }
}
//echo $check;
if($check==1){
     $objsales->salesdeletestatustagleadsource("sales_status", $sc);
}
}
    for($i=0;$i<count($s_id);$i++){
        $id12=$s_id[$i];
        $name=$s_name[$i];
        $color=$s_color[$i];
        $desc1=$s_status_desc[$i];
        if($id12==""){
            $objsales->salesinsertstatustagleadsource("sales_status",$companyname, $adminemailid, $name, $desc1, $color, $adminemailid, "admin", "insert", $id12);
        }else{
            $objsales->salesinsertstatustagleadsource("sales_status",$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 2%;
         }
         @media only screen and (max-width: 500px) {
            form {
                padding: 30px;
            }
        }
     </style> 
    </head>
    
    <body>
        <span  class="view">
        <div style="margin:80px 10px 10px 10px;border-style: solid;">
        <div class="navbar-inverse">
        
        <span style="color:white;padding-top: 5%;"><i style="align:center;font-size:2em;">Update Contact</i><span id="backtopage" style="cursor:pointer;"><img src="../../Images/backicon.jpg" class="img-circle pull-right" style="align:right;" width="35px" height="35px"></span><br></span>
        </div>
        <br>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="statusForm" method="post">
        <div class="field_wrapper_status">
        <?php
        $sales_status_res=$objsales->salesselectall($companyname, $adminemailid, "sales_status", "no");
        $cc=$sales_status_res->rowCount();
        $i=1;
        foreach($sales_status_res as $sales_status_row){
            $status_name=$sales_status_row['status_name'];
            $status_desc=$sales_status_row['status_desc'];
            $status_color=$sales_status_row['status_color'];
            $status_id=$sales_status_row['id'];
        $sales_status_res2=$objsales->salesselectstatustagleadsourceteamstream("status", $status_id);
        $cc1=$sales_status_res2->rowCount();
        //echo $cc1;
            ?>
                <div class="row">
                    <div class="col-md-2">
                        
                        <select id="status_color" name="status_color[]" class="form-control">
                            <option value="<?php echo $status_color;?>" selected><?php echo $status_color;?></option>
                            <?php
                            if($status_color!="Black"){
                                echo "<option value='Black'>Black</option>";
                            }if($status_color!="Blue"){
                                echo "<option value='Blue'>Blue</option>";
                            }if($status_color!="Grey"){
                                echo "<option value='Grey'>Grey</option>";
                            }if($status_color!="Red"){
                                echo "<option value='Red'>Red</option>";
                            }if($status_color!="Orange"){
                                echo "<option value='Orange'>Orange</option>";
                            }if($status_color!="Green"){
                                echo "<option value='Green'>Green</option>";
                            }if($status_color!="Magenta"){
                                echo "<option value='Magenta'>Magenta</option>";
                            } 
                            ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <input type="text" name="status_name[]" id="status_name" class="form-control" value="<?php echo $status_name;?>">
                    </div>
                    <div class="col-md-7">
                        <input type="hidden" name="status_id[]"  value="<?php echo $status_id;?>">
                    <?php if($cc1==0){?>
                    <div class="form-group">
                        <div class="input-group"><?php } ?>
                        <input type="text" name="status_desc[]" id="status_desc" class="form-control" value="<?php echo $status_desc;?>">
                        <?php if($cc1==0){?>
                        <div class="input-group-addon" style="background-color:red;color:white;cursor:pointer;" id="remove_button_url_1">x</div>
                        
                        </div> 
                    </div><?php } ?></div><br><br>
                </div>
            <?php
            $i++;
            }
            ?>
            </div>
            <span style="color:Blue;cursor:pointer;" id="add_button_status" 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_status'); //Add button selector
                                var wrapper__url_1 = $('.field_wrapper_status'); //Input field wrapper
                                var inputurl1 = '<div class="row">';
                                inputurl1 +='<div class="col-md-2">'; 
                                inputurl1 += '<select id="status_color" name="status_color[]" class="form-control">';
                                inputurl1 +='<option value="Black">Black</option>';
                                inputurl1 +='<option value="Blue">Blue</option>';
                                inputurl1 +='<option value="Grey">Grey</option>';
                                inputurl1 +='<option value="Red">Red</option>';
                                inputurl1 +='<option value="Orange">Orange</option>';
                                inputurl1 +='<option value="Green">Green</option>';
                                inputurl1 +='<option value="Magenta">Magenta</option>';
                                inputurl1 +='</select>';
                                inputurl1 +='</div>';
                                inputurl1 +='<div class="col-md-3">';
                                inputurl1 +='<input type="text" name="status_name[]" id="status_name" class="form-control">';
                                inputurl1 +='</div>';
                                inputurl1 +='<div class="col-md-7"><input type="hidden" name="status_id[]"><div class="form-group"><div class="input-group">';
                                inputurl1 +='<input type="text" name="status_desc[]" id="status_desc" class="form-control">';
                                inputurl1 +='<div class="input-group-addon" style="background-color:red;color:white;cursor:pointer;" id="remove_button_url_1">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_url_1', 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() {
    $('#statusForm').formValidation({
        //message: 'Invalid',
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
        fields: {
            'status_name[]': {
                validators: {
                    notEmpty: {
                        message: 'status Name is required and cannot be empty'
                    }
                }
            },
            /*'status_desc[]': {
                validators: {
                    notEmpty: {
                        message: 'Status Description is required and cannot be empty'
                    }
                }
            },*/
            'status_color[]': {
                validators: {
                    notEmpty: {
                        message: 'Status Color 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>";
}?>