@using System.Collections.Generic
@using Spartane.Web.Areas.Frontal.Models
@using Spartane.Web.Helpers
@model Spartane.Web.Areas.Frontal.Models.MS_Caracteristicas_PlatilloAdvanceSearchModel
@{
    List<SelectListItem> varCaracteristicas_platillo_Caracteristicas = null;
    if (ViewBag.Caracteristicas_platillos_Caracteristicas != null)
    { varCaracteristicas_platillo_Caracteristicas = (List<SelectListItem>)ViewBag.Caracteristicas_platillos_Caracteristicas; }

    var filters = (List<SelectListItem>)ViewBag.Filter;
}
<style>
    .input-validation-error {
        background: rgb(251, 227, 228);
        border: 1px solid #fbc2c4;
        color: #8a1f11;
    }

    .MarginBottom {
        margin-bottom: 1%;
    }

    #divAdvanceSearch input {
        margin-right: 2%;
    }

    #divAdvanceSearch button {
        margin-right: 2%;
    }

    #divAdvanceSearch select {
        margin-right: 2%;
    }

    #divAdvanceSearch h3 {
        margin-top: 2%;
    }

</style>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins" id="divAdvanceSearch">
        <div class="ibox-content">
            @using (Html.BeginForm("ShowAdvanceFilter", "MS_Caracteristicas_Platillo", FormMethod.Post, new { @id = "frmAdvanceSearch", @role = "form" }))
            {
				<input type="hidden" value="-1" name="idFilter" id="idFilter" />
                <div class="MarginBottom ">
                    <h3>@Resources.MS_Caracteristicas_PlatilloResources.Folio</h3>
                </div>
                <div class="MarginBottom form-inline">
                    <label class="">@Resources.Resources.From:</label>
                    @Html.TextBoxFor(m => m.FromFolio, new { @class = "form-control greaterThanZero", @type = "text", @onkeypress = "return isNumberKey(this, event);", @id = "txtAdvanceFromFolio" })
                    @Html.ValidationMessageFor(m => m.FromFolio)
                    <label class="">@Resources.Resources.To: </label>
                    @Html.TextBoxFor(m => m.ToFolio, new { @class = "form-control greaterThan greaterThanZero", @type = "text", @onkeypress = "return isNumberKey(this, event);", @id = "txtAdvanceToFolio", @data_min = "txtAdvanceFromFolio" })
                    @Html.ValidationMessageFor(m => m.ToFolio)
                </div>
                <div class="MarginBottom">
                    <h3>@Resources.MS_Caracteristicas_PlatilloResources.Caracteristicas</h3>
                </div>
                <div class="MarginBottom form-inline">
                    <label class="">@Resources.Resources.Filter:</label>
                    @Html.DropDownListFor(m => m.CaracteristicasFilter, filters, new { @class = "form-control", @id = "ddlCaracteristicasFilter" })
                    @Html.TextBoxFor(m => m.AdvanceCaracteristicas, new { @class = "form-control", @type = "text", @id = "txtAdvanceCaracteristicas" })
                    <label class="">@Resources.Resources.OrSelect </label>
                    @if (varCaracteristicas_platillo_Caracteristicas!=null)
                    {
                        @Html.ListBoxFor(m => m.AdvanceCaracteristicasMultiple, varCaracteristicas_platillo_Caracteristicas,
                        new
                        {
                            @class = "form-control chosen-select",
                            @multiple = "multiple",
                            @data_placeholder = "",
                            @style = "width: 350px;",
                            @id = "ddlAdvanceCaracteristicas"
                        });
                    }
                </div>

                <div class="MarginBottom form-inline">
                    <input type="submit" class="btn btn-primary" value="@Resources.Resources.Search"><a class="btn btn-primary" href="@Url.Content("~/Frontal/MS_Caracteristicas_Platillo/")" value=" ">@Resources.Resources.Cancelar</a>
                </div>
            }
        </div>
    </div>
</div>

<script>
    function InitiateAdvanceSearch() {
        var advanceSearch = {}, hasParameter = false;
        if ($("#txtAdvanceFromFolio").val() != "" || $("#txtAdvanceToFolio").val() != "") {
            hasParameter = true;
            advanceSearch.MS_Caracteristicas_PlatilloFolioFrom = $("#txtAdvanceFromFolio").val();
            advanceSearch.MS_Caracteristicas_PlatilloFolioTo = $("#txtAdvanceToFolio").val();
        }
        advanceSearch.CaracteristicasSearchType = 0;
        if ($('#ddlAdvanceCaracteristicas').val().length > 0 || $("#txtAdvanceCaracteristicas").val() == "") {


            if (isMS_Caracteristicas_PlatilloTextSearch) {
                advanceSearch.CaracteristicasName = $("#txtAdvanceCaracteristicas").val();
                advanceSearch.CaracteristicasNameSearchType = $("#ddlAdvanceCaracteristicas").val();
                advanceSearch.CaracteristicasSearchType = 1;
            }
            else {

                advanceSearch.CaracteristicasToSearch = $("#ddlAdvanceCaracteristicas").val();
                advanceSearch.CaracteristicasSearchType = 2;
            }
        }

        advanceSearchQuery = hasParameter ? JSON.stringify(advanceSearch) : "";
        $('#tblMS_Caracteristicas_Platillo').DataTable().draw();
        return false;
    }

    var advanceSearchQuery = "", isMS_Caracteristicas_PlatilloTextSearch = true;

    $(document).ready(function () {
        $('.clock').clockpicker();
		$('.date').datepicker({
			todayBtn: "linked",
			keyboardNavigation: false,
			forceParse: false,
			calendarWeeks: true,
			autoclose: true,
			format: "@ConfigurationProperty.DateFormatDatePicker"
		});
        $("#txtAdvanceCaracteristicas").keypress(function () {
            isMS_Caracteristicas_PlatilloTextSearch = true;
            $("#ddlAdvanceCaracteristicas").val('').trigger("chosen:updated");
        });

        $("#ddlAdvanceCaracteristicas").change(function () {
            isMS_Caracteristicas_PlatilloTextSearch = false;
            $("#txtAdvanceCaracteristicas").val('');
        });

    });
</script>


@section Styles {
    @Styles.Render("~/Content/plugins/chosen/chosenStyles")
    @Styles.Render("~/plugins/dateRangeStyles")
    @Styles.Render("~/plugins/dataPickerStyles")
    @Styles.Render("~/plugins/clockpickerStyles")

}

@section Scripts {
    @Scripts.Render("~/plugins/chosen")
    @Scripts.Render("~/plugins/dataPicker")
    @Scripts.Render("~/plugins/clockpicker")


    <script>
        //Adding Rules
        jQuery.validator.unobtrusive
      .adapters.addSingleVal("isnumberafter", "propertyname");
        jQuery.validator.unobtrusive
    .adapters.addSingleVal("isdateafter", "propertyname");

        //Number after method
        jQuery.validator.addMethod("isnumberafter", function (val, element, other) {
            var modelPrefix = element.name.substr(
                0, element.name.lastIndexOf(".") + 1);
            var otherVal = $("[name=" + modelPrefix + other + "]").val();
            if (val && otherVal) {
                //if (Date.parse(val) <= Date.parse(otherVal)) {
                if (Number(val) < Number(otherVal)) {
                    return false;
                }
                        }
            return true;
        });

        //Date after after method
        jQuery.validator.addMethod("isdateafter", function (val, element, other) {
            var modelPrefix = element.name.substr(
                0, element.name.lastIndexOf(".") + 1);
            var otherVal = $("[name=" + modelPrefix + other + "]").val();
            if (val && otherVal) {

                if (GetDateObject(val) < GetDateObject(otherVal)) {

                    return false;
                    }
                }
            return true;
            });


        function GetDateObject(value) {
            var delimiter = GetDelimiter(dateFormat);
            var dateCollection = value.split(delimiter);
            var formatCollection = dateFormat.split(delimiter);
            var dateModel = GetDateModel(dateCollection, formatCollection);
            return new Date(dateModel.Year, dateModel.Month - 1, dateModel.Day);
        }
        //Used to get the date model by parsing the date and the format
        function GetDateModel(dateCollection,formatCollection) {
            var day, month, year;
            for (var i = 0; i < formatCollection.length; i++) {
                switch (formatCollection[i][0].toUpperCase()) {
                
                    case "D":
                        day = dateCollection[i];
                        break;

                    case "Y":
                        year = dateCollection[i];
                        break;

                    case "M":
                        month = dateCollection[i];
                        break;
                }
            }

            return {
                Day:day,
                Month:month,
                Year:year
            };
        }

        function GetDelimiter(format) {

            for (var i = 0; i < format.length; i++) {
                if (/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(format[i])) {
                    return format[i];
                }
            }
            return null;
        }

        var dateFormat = "@ConfigurationProperty.DateFormatDatePicker";

        $(document).ready(function () {
            $('.input-daterange').datepicker({
                keyboardNavigation: false,
                forceParse: false,
                autoclose: true,
                format: dateFormat,
                message: 'The date is not a valid'
            });

            $(".chosen-select").chosen({ width: "22%", no_results_text: "Oops, nothing found!" });
			if ($('#IsFilterModal').val() == 'true') {
                $('.ShowAdvanceFilter #right-sidebar').css('display', 'none');
                $('.ShowAdvanceFilter .navbar-default').css('display', 'none');
                $('.ShowAdvanceFilter .border-bottom').css('display', 'none');
                $('.ShowAdvanceFilter #page-wrapper').css('margin', '0 auto');
                $('.ShowAdvanceFilter .btn-save').val('@Resources.Resources.Filter');
                $('.ShowAdvanceFilter .btn-cancelar').attr('data-dismiss', 'modal');
                $('.ShowAdvanceFilter .btn-cancelar').removeAttr('href');
                $('#idFilter').val($('#IdReport').val());
            }
        });

    </script>
}

