<style>
    .text-white{
        color: white;
    }
    a{
        color: #4c0870;
    }
</style>
<script src="<?php echo VIEWS_PATH; ?>crm/common.js"></script>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php echo $title; ?> </div>
        </div>
        <div class="panel-body">
            <div class="row form-group">
                <div class="col-md-3" >
                    <div class="input-group  ">
                        <span class="input-group-addon bg-complete-dark"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                    </div>
                </div> 
                <div class="col-md-3">
                    <select name="deal_state" id="deal_state" class="full-width " data-init-plugin="select2">
                        <option value=""><?php echo CommonLanguage::getMessage('All');?></option>
                        <option value="Incomplete"><?php echo CommonLanguage::getMessage('incomplete');?></option>
                        <option value="Won"><?php echo CommonLanguage::getMessage('won');?></option>
                        <option value="Lost"><?php echo CommonLanguage::getMessage('lost');?></option>
                    </select>
                </div>
                <?php  if ($permissions->hasPermission('add_deal')) {?>
                <div class="col-md-4 pull-right">
                    <a style="color:white !important;" class="btn bg-complete-dark pull-right" id="add_deal" data-origin="list" data-toggle="modal" data-target="#deal_modal">
                        <?php print CommonLanguage::getMessage('add_deal'); ?>
                    </a> 
                </div>
                <?php }?>
            </div>
            <div class="row  m-t-10">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered table-hover crm-table" id="tableWithSearch">
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%;" >#</th>
                                <th class="text-center"  ><?php print CommonLanguage::getMessage('title'); ?></th>
                                <th class="text-center" style="width:10%;"><?php print CommonLanguage::getMessage('stage'); ?></th>
                                <th class="text-center" style="width:10%;"><?php print CommonLanguage::getMessage('state'); ?></th>
                                <th class="text-center" style="width:10%;"><?php print CommonLanguage::getMessage('value'); ?></th>
                                <th class="text-center" style="width:10%;"><?php print CommonLanguage::getMessage('deal_prob'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('reason'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('customer'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('contact_date'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('created_by'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('service'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function(){ 
        var data_session = get_storage_element(page_link);
        if (data_session.deal_state != '') {
            $("#deal_state").val(data_session.deal_state).select2();
        }
        if (data_session.Date != '') {
            window.startDate = data_session.Date_From;
            window.endDate = data_session.Date_To;
            $("input[name=From_To_Date]").val(window.startDate+' - '+window.endDate)
        }
        ChangeTable()
        $("input[name=From_To_Date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                window.endDate = end.format("YYYY-MM-DD")
            ChangeTable();
        });
        $('#deal_state').on('change',function(){
            ChangeTable()
        });
    });
    function ChangeTable(){
        var Data = {
            Date:$("input[name=From_To_Date]").val(),
            Date_From: window.startDate ? window.startDate : today,
            Date_To: window.endDate ? window.endDate : today,
            deal_state:$('#deal_state').val()
        };
        set_storage_element(page_link, Data)
        RebuildDataTable($('.panel-heading .panel-title').html(),[0,1,2,3,4,5,6,7,8,9],'crmAjax/dealList',Data)
    }
</script>
<?php  if ($permissions->hasPermission('add_deal')) {
    include 'Views/crm/deal_modal.php';
}