<?php
session_start();
$companyname=$_SESSION['companyname'];
$adminemailid=$_SESSION['emailid'];
$companytype=$_SESSION['companytype'];
if(($adminemailid !="")&&($companyname!="")){
require_once('../Control/address.php');
$objaddress=new addressctrl();
require_once('../Control/sales.php');
$objsales=new salesctrl();
require_once('../Control/contact.php');
$objcontact=new contactctrl();

?>
<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>
    
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="/resources/demos/style.css">
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    

        <script>
            $(function(){
                var newdate = new Date();
            
                newdate.setDate(newdate.getYear() - 10); // minus the date
                
                var date1=newdate.getFullYear() + "-" +(newdate.getMonth()+1) + "-" +newdate.getDate();
                //alert(date1);
            
                $('#date1').datepicker({
                    dateFormat: 'yy-mm-dd',
                    changeMonth: true,
                    changeYear: true,
                    setDate: date1,
                    numberOfMonths: 1,
                    showButtonPanel: true,
                    closeText: 'Clear',
                    onClose: function (dateText, obj) {
                        if ($(window.event.srcElement).hasClass('ui-datepicker-close'))
                            $("#cont_birthday").val('');
                    }
            }); 
            });
        
         
        </script>    
    </head>
    <style>
        form{
            padding:2% 20%;
        }
        @media screen and (max-width: 699px) and (min-width: 420px) {
            form {
                padding:50px 5%;
            }
        }
        @media screen and (max-width:420px) {
            form {
                padding:50px 1%;
            }
        }
    </style>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <br><br><br>
            <div class="col-md-12">
                <select class="form-control" name="filtertype" id="filtertype" onchange="functionshow();">
                    <option value="Status" >Status</option>
                    <option value="Tag" >Tag</option>
                    <option value="Owner" >Owner</option>
                    <!--<option value="Organization" >Organization</option>-->
                    <option value="Lead Source" >Lead Source</option>
                    <option value="Date Created" >Contact Date Created</option>
                    <option value="Call Date" >Call Date</option>
                    <option value="Deal create Date" >Deal create Date</option>
                    <option value="Deal Close Date" >Deal Close Date</option>
                    
                </select>
            </div>       
            <script>
                function functionshow(){
                    var type=$("#filtertype").val();
                    if((type=="Date Created")||(type=="Call Date")||(type=="Deal create Date")||(type=="Deal Close Date")){
                        $("#dateform").show();
                        $("#statusform").hide();
                        $("#tagform").hide();
                        $("#leadform").hide();
                        $("#ownerform").hide();
                    }else if(type=="Status"){
                        $("#dateform").hide();
                        $("#statusform").show();
                        $("#tagform").hide();
                        $("#leadform").hide();
                        $("#ownerform").hide();
                    }else if(type=="Tag"){
                        $("#dateform").hide();
                        $("#statusform").hide();
                        $("#tagform").show();
                        $("#leadform").hide();
                        $("#ownerform").hide();
                    }else if(type=="Owner"){
                        $("#dateform").hide();
                        $("#statusform").hide();
                        $("#tagform").hide();
                        $("#leadform").hide();
                        $("#ownerform").show();
                    }else if(type=="Lead Source"){
                        $("#dateform").hide();
                        $("#statusform").hide();
                        $("#tagform").hide();
                        $("#leadform").show();
                        $("#ownerform").hide();
                    }
                }
            </script>
            <div class="form-group col-md-12" style="padding-top:20px;"> 
                <select class=" form-control" id="filter_is" title="select the Country"  name="filter_is">
                    <option value="1">is</option>
                    <option value="0">is not</option>
                </select>
            </div>    <br>   
            
            <div class="form-group col-md-12" style="display:none;padding-top:20px;" id="dateform">
                <input type="text" name="date1" id="date1" class="form-control">
            </div> 
<?php
$res_status=$objsales->salesselectall($companyname, $adminemailid, "sales_status", "no");
//print_r($res_sales);
?>            
            <div class="col-md-12" id="statusform"> <br>
                <select class="form-control" id="filter_status" title="select the status"  name="filter_status">
                    <option value="" ></option>
                    <?php 
                        foreach($res_status as $row_status){
                        $s_name=$row_status['status_name'];
                        $s_id=$row_status['id'];
                        echo "<option value='".$s_id."'>".$s_name."</option>";
                        }
                    ?>
                </select>
            </div>
<?php
$res_tag=$objsales->salesselectall($companyname, $adminemailid, "sales_tag", "no");
//print_r($res_sales);
?>             
            <div class="col-md-12" style="display:none;padding-top:20px;" id="tagform"> 
                <select class="form-control" name="filter_tag" id="filter_tag">
                    <option></option>
                    <?php 
                    foreach($res_tag as $row_tag){
                    $t_name=$row_tag['tag_name'];
                    $t_id=$row_tag['id'];
                    echo "<option value='".$t_id."'>".$t_name."</option>";
                    }
                    ?>
                </select>
            </div>
<?php
$res_lead=$objsales->salesselectall($companyname, $adminemailid, "sales_lead_source", "no");
//print_r($res_sales);
?>              
            <div class="col-md-12" style="display:none;padding-top:20px;" id="leadform">
                <select class="form-control" name="filter_lead" id="filter_lead">
                    <option></option>
                    <?php 
                    foreach($res_lead as $row_lead){
                    $l_name=$row_lead['lead_name'];
                    $l_id=$row_lead['id'];
                    echo "<option value='".$l_id."'>".$l_name."</option>";
                    }
                    ?>
                </select>
            </div> 
<?php
$res_emp=$objsales->salesselectall($companyname, $adminemailid, "signupdetails", "no");
//print_r($res_sales);
?>              
            <div class="col-md-12" style="display:none;padding-top:20px;" id="ownerform"> 
                <select class="form-control" name="filter_owner" id="filter_owner">
                    <option value="<?php echo $adminemailid;?>" selected>Me</option>
                    <?php 
                    foreach($res_emp as $row_emp){
                    $s_employeename=$row_emp['employeename'];
                    $s_emailid=$row_emp['emailid'];
                    if($adminemailid!=$s_emailid){
                    echo "<option value='".$s_emailid."'>".$s_employeename."(".$s_emailid.")</option>";
                    }
                    }
                    ?>
                </select>
            </div> 
            
            <div class="col-md-12" style="display:none">
                <div class="input-group">
                    <div class="input-group-addon">Country</div>
                    <select class=" form-control cont_country" data-live-search="true" data-size="5" id="cont_country" title="select the Country"  name="cont_country" required width="70%">
                        <option value="" >country</option>
                        <?php 
                        foreach($res_country as $row_country){
                        $c_name=$row_country['name'];
                        $c_id=$row_country['id'];
                        echo "<option value='".$c_id."'>".$c_name."</option>";
                        }
                        ?>
                    </select>
                </div>
            </div>        
            
            
        
        <div style="padding-top:20px;"> 
            
            <div class="col-sm-4">
                <span class="pull-right">Save Filter?</span>
            </div>
            
            <div class="col-sm-8">
                <input type="text" class="form-control" max="100" name="filter_name" id="filter_name">
            </div>
        </div>
        <br>
        <span style="padding-top:20px;"><center><input type="submit" value="Run" class="btn btn-primary" onclick="functionfiltersubmit()"></center></span>
        </form>
<script>
    function functionfiltersubmit(){
        //alert("hai");
        var type=$("#filtertype").val();
        var type4=$("#filter_name").val();
        if(type=="Date Created"){
            var type1="Date Created";
            var type2=$("#filter_is").val();
            var type3=$("#date1").val();
            if(type3==""){
                $("#date1").focus();
            }
        }else if(type=="Call Date"){
            var type1="Call Date";
            var type2=$("#filter_is").val();
            var type3=$("#date1").val();
            if(type3==""){
                $("#date1").focus();
            }
        }else if(type=="Deal create Date"){
            var type1="Deal create Date";
            var type2=$("#filter_is").val();
            var type3=$("#date1").val();
            if(type3==""){
                $("#date1").focus();
            }
        }else if(type=="Deal Close Date"){
            var type1="Deal Close Date";
            var type2=$("#filter_is").val();
            var type3=$("#date1").val();
            if(type3==""){
                $("#date1").focus();
            }
        }else if(type=="Status"){
            var type1="Status";
            var type2=$("#filter_is").val();
            var type3=$("#filter_status").val();
            if(type3==""){
                $("#filter_status").focus();
            }
        }else if(type=="Tag"){
            var type1="Tag";
            var type2=$("#filter_is").val();
            var type3=$("#filter_tag").val();
            if(type3==""){
                $("#filter_tag").focus();
            }
        }else if(type=="Owner"){
            var type1="Owner";
            var type2=$("#filter_is").val();
            var type3=$("#filter_owner").val();
            if(type3==""){
                $("#filter_owner").focus();
            }
        }else if(type=="Lead Source"){
            var type1="Lead Source";
            var type2=$("#filter_is").val();
            var type3=$("#filter_lead").val();
            if(type3==""){
                $("#filter_lead").focus();
            }
        }
        
        if(type3!=""){
            if(type4!=""){
                //alert("dff");
                $.ajax({
                   url: "filter_intermediate.php",
                   method: "post",
                   data:{type1:type1, type2:type2, type3:type3, type4:type4},
                   success:function(response){
                        window.top.location.replace("dashboard.php");
                   }
                });
            }else{
                $("#filter_name").focus();
                swal("","fill the filtername", "warning");
            }
        }else{
            swal("","fill all the field", "warning");
        }
    }
</script>            
        
    </body>
    
</html>

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