<?php 
$con=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()){
echo "Connection Fail".mysqli_connect_error();
}

$logged_id = Session::getUserID();
if(isset($_POST['submit_customer'])){
    $logged_id = Session::getUserID();
    $customers_ID = $_POST['customers_ID'];
    $ST_date = $_POST['ST_date'];
    $group = $_POST['group'];
    $daily = $_POST['daily'];
    $Employee_ID = $_POST['Employee_ID'];
    $end_date== date('Y-m-d', strtotime($ST_date. " + $daily days"));
    $have_conflict  = TRUE;
    $sales_tracker = mysqli_query($con, "SELECT DISTINCT tracker_group,daily,start_date from sales_tracker");
    $tracker_group = mysqli_query($con, "SELECT * from sales_tracker where tracker_group='$group'");
    
    $num_tracker_group =mysqli_num_rows($tracker_group);
    $start_date_group=mysqli_fetch_array($tracker_group);
    if($num_tracker_group <= 0){
        while($sales_tracker_record =  mysqli_fetch_array($sales_tracker)){   
            $start_date_db =$sales_tracker_record['start_date'];
            $daily_db =$sales_tracker_record['daily'];
            $end_date_db = date('Y-m-d', strtotime($start_date_db. " + $daily_db days"));
            
            if($start_date_db <= $ST_date && $end_date_db >= $ST_date || $start_date_db <= $end_date && $end_date_db >= $end_date){
                $have_conflict=FALSE;
                break;
            }
        }
        if($have_conflict){
            foreach($customers_ID as $ID){
                $query = mysqli_query($con, "insert into  sales_tracker(supplier_id,customers_ID,start_date,tracker_group,daily) value('$Employee_ID','$ID','$ST_date','$group','$daily')");
                

            }
            echo "<script>alert('succ');</script>";
        }else{
            echo "<script>alert('have conflict');</script>";
        }
    }else{
        $sales_tracker = mysqli_query($con, "SELECT DISTINCT tracker_group,daily,start_date from sales_tracker where tracker_group='$group'");
        $sales_tracker_record =  mysqli_fetch_array($sales_tracker);
        $test_daily=$sales_tracker_record['daily'];
        $test_start_date=$sales_tracker_record['start_date'];
        $test= $start_date_group['start_date'];
        if($sales_tracker_record['daily'] == $daily && $sales_tracker_record['start_date'] == $start_date_group['start_date'])
        {
            foreach($customers_ID as $ID){
                $query = mysqli_query($con, "insert into  sales_tracker(supplier_id,customers_ID,start_date,tracker_group,daily) value('$Employee_ID','$ID','$ST_date','$group','$daily')");
                

            }
        }
       else echo "<script>alert('if you want to add more sales man into exist group must delay , start date and salesman same with group info  ');</script>";

    }
    

}
?>
<style>
    .nav-tabs-fillup>li>a:after {
        background: none repeat scroll 0 0 #4c0870 !important;
        border: 1px solid #4c0870 !important;
    }
    .AddButtons .fa-plus,.AddButtons .fa-minus{
        cursor:pointer;
    }
    .AddButtons .fa-minus{
        color:#ed9421;
    }
    .AddButtons .fa-plus{
        color:#4c0870; 
    }
    .required-fields{
        border: red solid 1px;
    }
</style>
<div class="modal fade" id="customer_modal" data-backdrop="static" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                
                <h5 class="add-label h-labels bold hidden"> <?php echo CommonLanguage::getMessage('new_customer'); ?></h5>
                <h5 class="edit-label h-labels bold hidden"> <?php echo CommonLanguage::getMessage('edit_customer'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('customer_alert'); ?></div>
                <form method="POST" id="customer_form" >
                    <input type="hidden" name="Customer_ID" id="Customer_ID"/>
                    <div class="panel panel-transparent ">
                    <ul class="nav nav-tabs nav-tabs-fillup">
                            <li class="active">
                                <a data-toggle="tab" href="#slide1"><span><?php print CommonLanguage::getMessage('customer_info'); ?></span></a>
                            </li>
                            
                        </ul>

                        <div class="tab-content">
                            <div class="tab-pane slide-left active" id="slide1">
                                
                                <hr class="b-dashed">
                                <div class="row form-group">
                                    
                                    <div class="col-md-3">
                                    <?php 
                                    $tbl_salesman_customers = mysqli_query($con, "SELECT SC.customer_id FROM tbl_salesman_customers as SC WHERE SC.employee_id='$logged_id' and NOT EXISTS (SELECT ST.customers_ID FROM  sales_tracker as ST WHERE  SC.customer_id = ST.customers_ID )
                                    ");

                                    while($tbl_salesman_customers_record =  mysqli_fetch_array($tbl_salesman_customers)){   
                                        $customer_id =$tbl_salesman_customers_record['customer_id'];
                                        $tbl_customers = mysqli_query($con, "SELECT * FROM tbl_customers WHERE Customer_ID='$customer_id'");
                                        $tbl_customers_record =  mysqli_fetch_array($tbl_customers);
                                        $Customer_Name =$tbl_customers_record['Customer_Name']; 
                                    
                                    ?>
                                    <input type="checkbox" name="customers_ID[]" id="customers_ID[]"  value="<?php echo  $customer_id?>">
                                    <label > <?PHP echo $Customer_Name; ?></label><br>
                                    <?PHP } ?>
                                    </div>
                                    <div class="col-md-3">
									 <label>Sales Man</label> 
                                         <select id="type1" name="Employee_ID" class="full-width" >
                                        <?php   
                                        $tbl_hrm_employee = mysqli_query($con, "SELECT * FROM tbl_hrm_employee ");
                                        
                                        while($sales_supplier_record =  mysqli_fetch_array($tbl_hrm_employee)){
                                            $Employee_ID = $sales_supplier_record['Employee_ID'];
                                            ?>
                                            <option value="<?php echo $Employee_ID;?>"><?php echo $sales_supplier_record['Employee_FirstName']." ".$sales_supplier_record['Employee_MiddleName']." ".$sales_supplier_record['Employee_LastName']; ?></option>
                                            <?php } ?>
                                        </select>
                                      </div>
                                    <div class="col-md-3" >
                                    <label >Start Date</label> <input type="date"class="form-control" id="ST_date" name="ST_date" placeholder="Start Date" value="" required="true"> </div>
                                    <div class="col-md-3">
									 <label>Next Time</label> <input type="text" class="form-control" id="daily" name="daily" placeholder="Next time" value="" required="true"> 
                                    </div>
                                    <div class="col-md-3">
									 <label>Group</label> <input type="text" class="form-control" id="group" name="group" placeholder="Group" value="" required="true"> 
                                    </div>

                                </div>
                                
                            </div>
                            
                        </div>
                    </div>
                    <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default btn-cons">
                    <?php print CommonLanguage::getMessage('Close'); ?>
                </button>
                <button class="btn btn-primary btn-cons"  name="submit_customer" id="submit_customer" type="submit">
                    <?php print CommonLanguage::getMessage('Save'); ?>
                </button>
            </div>
                </form>
            </div>
            
            <?php echo Forms::ModalLoader('customer_loader'); ?>
        </div>
    </div>
</div>