<div class="row grid-margin">
    <div class="card" style="width:100%">
        <div class="card-body">
            <div class="row">
                <div class="icheck-flat">
                    <input type="radio" id="is_day" name="date_type"  value="day" onclick="changeDateType('day')"
                    <?php 
                        if(isset($date_type) && $date_type =="day"){
                            echo "checked";
                        }
                    ?>
                    />
                    <label for="is_map">Before</label>
                </div>
                <div class="icheck-flat" style="margin-left: 20px">
                    <input type="radio" id="is_period" name="date_type" value="period" onclick="changeDateType('period')"
                    <?php 
                        if(isset($date_type) && $date_type =="period"){
                            echo "checked";
                        }
                    ?>/>
                    <label for="is_list">Period</label>
                </div>

            </div>
            <div class="row" style="padding:10px">
                <div class="col-md-2 col_date">
                    <div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd">
                        <input type="text" class="form-control" id="date">
                        <div class="input-group-addon">
                            <i class="icon-calendar"></i>
                        </div>
                    </div>
                </div>
                <div class="col-md-2" id="col_start">
                <div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd">
                        <input type="text" class="form-control" id="start">
                        <div class="input-group-addon">
                            <i class="icon-calendar"></i>
                        </div>
                    </div>
                </div>
                <div class="col-md-1 col_end" style="text-align:center; line-height: 30px">
                    ~
                </div>
                <div class="col-md-2 col_end">
                <div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd">
                        <input type="text" class="form-control" id="end">
                        <div class="input-group-addon">
                            <i class="icon-calendar"></i>
                        </div>
                    </div>
                </div>
                <div class="col-md-1">
                    <button class="btn btn-danger" onclick="purgeData()">Purge Data</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    <?php 
        if(isset($date_type)) {
            echo "changeDateType('{$date_type}');";
        }else{
            echo "changeDateType('');";
        }

        if(!isset($date)) {
            $date = date('Y-m-d', strtotime("-3 Months"));
        }

        if(!isset($start)) {
            $start = date('Y-m-d', strtotime("-3 Months"));
        }

        if(!isset($end)) {
            $end = date('Y-m-d', strtotime("-3 Months"));
        }
        echo "$('#date').val('{$date}');";
        echo "$('#start').val('{$start}');";
        echo "$('#end').val('{$end}');";

        if(isset($message)){
            echo "alert('$message');";
        }
    ?>

    function changeDateType(type){
        switch(type){
            case "day":
                $(".col_date").show();
                $("#col_start").hide();
                $(".col_end").hide();
                break;
            case "period":
                $(".col_date").hide();
                $("#col_start").show();
                $(".col_end").show();
                break;
            default:
                $(".col_date").hide();
                $("#col_start").hide();
                $(".col_end").hide();
        }
    }

    function purgeData()
    {
        if($("#is_day").is(":checked")){
            if($("#date").val() == "") {
                alert("Please select a date");
                return;
            }
            if(confirm("Do you really want to continue?")) {
                location.href = '<?=site_url("purge/purge_data/")?>' + $("#date").val();    
            }
        }else if($("#is_period").is(":checked")){
            if($("#start").val() == "" || $("#end").val() == "" || $("#end").val() < $("#start").val()) {
                alert("Please select valid period");
                return;
            }
            if(confirm("Do you really want to continue?")){
                location.href = '<?=site_url("purge/purge_data/")?>' + $("#end").val() + "/" + $("#start").val();    
            }
        }
		
    }

</script>
