<?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_tag", "no");
$ij=0;
$tagid=array();
        foreach($sales_status_res1 as $sales_status_row1){
            $tagid[$ij]=$sales_status_row1['id'];
            $ij++;
        }
       
if(isset($_POST['tag_id'])){
    $t_id=$_POST['tag_id'];
    $t_name=$_POST['tag_name'];
    $t_status_desc=$_POST['tag_desc'];

for($kk=0;$kk<count($tagid);$kk++){
    $check=1;
    $sc=$tagid[$kk];
    for($k=0;$k<count($t_id);$k++){
        if($t_id[$k]!=$sc){
            $check=1;
        }else{
            $check=0;
            break;
        }
}
//echo $check;
if($check==1){
     $objsales->salesdeletestatustagleadsource("sales_tag", $sc);
}
}
    for($i=0;$i<count($t_id);$i++){
        $id12=$t_id[$i];
        $name=$t_name[$i];
        $color="sd";
        $desc1=$t_status_desc[$i];
        if($id12==""){
            $rs=$objsales->salesinsertstatustagleadsource("sales_tag",$companyname, $adminemailid, $name, $desc1, $color, $adminemailid, "admin", "insert", $id12);
            print_r($rs);
        }else{
            $objsales->salesinsertstatustagleadsource("sales_tag",$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 5%;
         }
         @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 Tag</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="tagForm" method="post">
        <div class="field_wrapper_tag">
        <?php
        $sales_status_res=$objsales->salesselectall($companyname, $adminemailid, "sales_tag", "no");
        $cc=$sales_status_res->rowCount();
        $i=1;
        foreach($sales_status_res as $sales_status_row){
            $tag_name=$sales_status_row['tag_name'];
            $tag_desc=$sales_status_row['tag_desc'];
            $tag_id=$sales_status_row['id'];
        $sales_tag_res2=$objsales->salesselectstatustagleadsourceteamstream("tag", $tag_id);
        $cc1=$sales_tag_res2->rowCount();
        //echo $cc1;
            ?>
                <div class="row">
                    <div class="col-md-5">
                        <input type="text" name="tag_name[]" id="tag_name" class="form-control" value="<?php echo $tag_name;?>">
                    </div>
                    <div class="col-md-7">
                        <input type="hidden" name="tag_id[]"  value="<?php echo $tag_id;?>">
                    <?php if($cc1==0){?>
                    <div class="form-group">
                        <div class="input-group"><?php } ?>
                        <input type="text" name="tag_desc[]" id="tag_desc" class="form-control" value="<?php echo $tag_desc;?>">
                        <?php if($cc1==0){?>
                        <div class="input-group-addon" style="background-color:red;color:white;cursor:pointer;" id="remove_button_tag_1">x</div>
                        
                        </div> 
                    </div><?php } ?></div><br><br>
                </div>
            <?php
            $i++;
            }
            ?>
            </div>
            <span style="color:Blue;cursor:pointer;" id="add_button_tag" 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_tag'); //Add button selector
                                var wrapper__url_1 = $('.field_wrapper_tag'); //Input field wrapper
                                var inputurl1 = '<div class="row">';
                                inputurl1 +='<div class="col-md-5">';
                                inputurl1 +='<input type="text" name="tag_name[]" id="tag_name" class="form-control">';
                                inputurl1 +='</div>';
                                inputurl1 +='<div class="col-md-7"><input type="hidden" name="tag_id[]"><div class="form-group"><div class="input-group">';
                                inputurl1 +='<input type="text" name="tag_desc[]" id="tag_desc" class="form-control">';
                                inputurl1 +='<div class="input-group-addon" style="background-color:red;color:white;cursor:pointer;" id="remove_button_tag_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_tag_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() {
    $('#tagForm').formValidation({
        //message: 'Invalid',
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
        fields: {
            'tag_name[]': {
                validators: {
                    notEmpty: {
                        message: 'status Name is required and cannot be empty'
                    },
                    stringLength: {
                        min: 3,
                        max: 30,
                        message: 'The username must be more than 2 and less than 30 characters long'
                    }
                }
            }/*,
            'tag_desc[]': {
                validators: {
                    notEmpty: {
                        message: 'Status Description is required and cannot be empty'
                    },
                    stringLength: {
                        min: 3,
                        max: 300,
                        message: 'The username must be more than 2 and less than 300 characters long'
                    }
                }
            }*/
   
        }  
    })
        .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>";
}?>