@using System.Collections.Generic
@using Spartane.Web.Areas.Frontal.Models
@using Spartane.Web.Helpers
@model Spartane.Web.Areas.Frontal.Models.Planes_AlimenticiosAdvanceSearchModel
@{
    List<SelectListItem> varSpartan_User_Usuario_que_Registra = null;
    if (ViewBag.Spartan_Users_Usuario_que_Registra != null)
    { varSpartan_User_Usuario_que_Registra = (List<SelectListItem>)ViewBag.Spartan_Users_Usuario_que_Registra; }
    List<SelectListItem> varPacientes_Paciente = null;
    if (ViewBag.Pacientess_Paciente != null)
    { varPacientes_Paciente = (List<SelectListItem>)ViewBag.Pacientess_Paciente; }
    List<SelectListItem> varEstatus_Estatus = null;
    if (ViewBag.Estatuss_Estatus != null)
    { varEstatus_Estatus = (List<SelectListItem>)ViewBag.Estatuss_Estatus; }

    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", "Planes_Alimenticios", FormMethod.Post, new { @id = "frmAdvanceSearch", @role = "form" }))
            {
				<input type="hidden" value="-1" name="idFilter" id="idFilter" />
                <div class="MarginBottom ">
                    <h3>@Resources.Planes_AlimenticiosResources.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.Planes_AlimenticiosResources.Fecha_de_Registro</h3>
        </div>
        <div class="MarginBottom form-inline">
            <label class="">@Resources.Resources.From:</label>
            <div class="input-group date">
                <input class="form-control" id="txtAdvanceFromFecha_de_Registro" name="FromFecha_de_Registro" type="text" value="@Model.FromFecha_de_Registro" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
            <label class="">@Resources.Resources.To: </label>
            <div class="input-group date">
                <input class="form-control" id="txtAdvanceToFecha_de_Registro" name="ToFecha_de_Registro" type="text" value="@Model.ToFecha_de_Registro" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
        <div class="MarginBottom ">
            <h3>@Resources.Planes_AlimenticiosResources.Hora_de_Registro</h3>
        </div>
        <div class="MarginBottom form-inline">
            <label class="">@Resources.Resources.From:</label>
            <div class="input-group clock" data-autoclose="true">
                <input class="form-control" id="txtAdvanceFromHora_de_Registro" name="FromHora_de_Registro" type="text" value="@Model.FromHora_de_Registro" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-time"></span>
                </span>
            </div>
            <label class="">@Resources.Resources.To: </label>
            <div class="input-group clock" data-autoclose="true">
                <input class="form-control" id="txtAdvanceToHora_de_Registro" name="ToHora_de_Registro" type="text" value="@Model.ToHora_de_Registro" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-time"></span>
                </span>
            </div>
        </div>
                <div class="MarginBottom">
                    <h3>@Resources.Planes_AlimenticiosResources.Usuario_que_Registra</h3>
                </div>
                <div class="MarginBottom form-inline">
                    <label class="">@Resources.Resources.Filter:</label>
                    @Html.DropDownListFor(m => m.Usuario_que_RegistraFilter, filters, new { @class = "form-control", @id = "ddlUsuario_que_RegistraFilter" })
                    @Html.TextBoxFor(m => m.AdvanceUsuario_que_Registra, new { @class = "form-control", @type = "text", @id = "txtAdvanceUsuario_que_Registra" })
                    <label class="">@Resources.Resources.OrSelect </label>
                    @if (varSpartan_User_Usuario_que_Registra!=null)
                    {
                        @Html.ListBoxFor(m => m.AdvanceUsuario_que_RegistraMultiple, varSpartan_User_Usuario_que_Registra,
                        new
                        {
                            @class = "form-control chosen-select",
                            @multiple = "multiple",
                            @data_placeholder = "",
                            @style = "width: 350px;",
                            @id = "ddlAdvanceUsuario_que_Registra"
                        });
                    }
                </div>
        <div class="MarginBottom ">
            <h3>@Resources.Planes_AlimenticiosResources.Fecha_inicio_del_Plan</h3>
        </div>
        <div class="MarginBottom form-inline">
            <label class="">@Resources.Resources.From:</label>
            <div class="input-group date">
                <input class="form-control" id="txtAdvanceFromFecha_inicio_del_Plan" name="FromFecha_inicio_del_Plan" type="text" value="@Model.FromFecha_inicio_del_Plan" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
            <label class="">@Resources.Resources.To: </label>
            <div class="input-group date">
                <input class="form-control" id="txtAdvanceToFecha_inicio_del_Plan" name="ToFecha_inicio_del_Plan" type="text" value="@Model.ToFecha_inicio_del_Plan" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
        <div class="MarginBottom ">
            <h3>@Resources.Planes_AlimenticiosResources.Fecha_fin_del_Plan</h3>
        </div>
        <div class="MarginBottom form-inline">
            <label class="">@Resources.Resources.From:</label>
            <div class="input-group date">
                <input class="form-control" id="txtAdvanceFromFecha_fin_del_Plan" name="FromFecha_fin_del_Plan" type="text" value="@Model.FromFecha_fin_del_Plan" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
            <label class="">@Resources.Resources.To: </label>
            <div class="input-group date">
                <input class="form-control" id="txtAdvanceToFecha_fin_del_Plan" name="ToFecha_fin_del_Plan" type="text" value="@Model.ToFecha_fin_del_Plan" />
                <span class="form-inline input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
                <div class="MarginBottom ">
                    <h3>@Resources.Planes_AlimenticiosResources.Semana</h3>
                </div>
                <div class="MarginBottom form-inline">
                    <label class="">@Resources.Resources.From:</label>
                    @Html.TextBoxFor(m => m.FromSemana, new { @class = "form-control greaterThanZero", @type = "text", @onkeypress = "return isNumberKey(this, event);", @id = "txtAdvanceFromSemana" })
                    @Html.ValidationMessageFor(m => m.FromSemana)
                    <label class="">@Resources.Resources.To: </label>
                    @Html.TextBoxFor(m => m.ToSemana, new { @class = "form-control greaterThan greaterThanZero", @type = "text", @onkeypress = "return isNumberKey(this, event);", @id = "txtAdvanceToSemana", @data_min = "txtAdvanceFromSemana" })
                    @Html.ValidationMessageFor(m => m.ToSemana)
                </div>
                <div class="MarginBottom">
                    <h3>@Resources.Planes_AlimenticiosResources.Paciente</h3>
                </div>
                <div class="MarginBottom form-inline">
                    <label class="">@Resources.Resources.Filter:</label>
                    @Html.DropDownListFor(m => m.PacienteFilter, filters, new { @class = "form-control", @id = "ddlPacienteFilter" })
                    @Html.TextBoxFor(m => m.AdvancePaciente, new { @class = "form-control", @type = "text", @id = "txtAdvancePaciente" })
                    <label class="">@Resources.Resources.OrSelect </label>
                    @if (varPacientes_Paciente!=null)
                    {
                        @Html.ListBoxFor(m => m.AdvancePacienteMultiple, varPacientes_Paciente,
                        new
                        {
                            @class = "form-control chosen-select",
                            @multiple = "multiple",
                            @data_placeholder = "",
                            @style = "width: 350px;",
                            @id = "ddlAdvancePaciente"
                        });
                    }
                </div>
                <div class="MarginBottom">
                    <h3>@Resources.Planes_AlimenticiosResources.Estatus</h3>
                </div>
                <div class="MarginBottom form-inline">
                    <label class="">@Resources.Resources.Filter:</label>
                    @Html.DropDownListFor(m => m.EstatusFilter, filters, new { @class = "form-control", @id = "ddlEstatusFilter" })
                    @Html.TextBoxFor(m => m.AdvanceEstatus, new { @class = "form-control", @type = "text", @id = "txtAdvanceEstatus" })
                    <label class="">@Resources.Resources.OrSelect </label>
                    @if (varEstatus_Estatus!=null)
                    {
                        @Html.ListBoxFor(m => m.AdvanceEstatusMultiple, varEstatus_Estatus,
                        new
                        {
                            @class = "form-control chosen-select",
                            @multiple = "multiple",
                            @data_placeholder = "",
                            @style = "width: 350px;",
                            @id = "ddlAdvanceEstatus"
                        });
                    }
                </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/Planes_Alimenticios/")" value=" ">@Resources.Resources.Cancelar</a>
                </div>
            }
        </div>
    </div>
</div>

<script>
    function InitiateAdvanceSearch() {
        var advanceSearch = {}, hasParameter = false;
        if ($("#txtAdvanceFromFolio").val() != "" || $("#txtAdvanceToFolio").val() != "") {
            hasParameter = true;
            advanceSearch.Planes_AlimenticiosFolioFrom = $("#txtAdvanceFromFolio").val();
            advanceSearch.Planes_AlimenticiosFolioTo = $("#txtAdvanceToFolio").val();
        }
        if ($("#txtAdvanceFromFecha_de_Registro").val() != "" || $("#txtAdvanceToFecha_de_Registro").val() != "") {
            hasParameter = true;
            advanceSearch.Planes_AlimenticiosFecha_de_RegistroFrom = $("#txtAdvanceFromFecha_de_Registro").val();
            advanceSearch.Planes_AlimenticiosFecha_de_RegistroTo = $("#txtAdvanceToFecha_de_Registro").val();
        }
        if ($("#txtAdvanceHora_de_Registro").val() != "") {
            hasParameter = true;
            advanceSearch.Planes_AlimenticiosHora_de_Registro = $("#txtAdvanceHora_de_Registro").val();
            advanceSearch.Planes_AlimenticiosHora_de_RegistroSearchType = $("#ddlAdvanceHora_de_RegistroFilter").val();
        }
        advanceSearch.Usuario_que_RegistraSearchType = 0;
        if ($('#ddlAdvanceUsuario_que_Registra').val().length > 0 || $("#txtAdvanceUsuario_que_Registra").val() == "") {


            if (isPlanes_AlimenticiosTextSearch) {
                advanceSearch.Usuario_que_RegistraName = $("#txtAdvanceUsuario_que_Registra").val();
                advanceSearch.Usuario_que_RegistraNameSearchType = $("#ddlAdvanceUsuario_que_Registra").val();
                advanceSearch.Usuario_que_RegistraSearchType = 1;
            }
            else {

                advanceSearch.Usuario_que_RegistraToSearch = $("#ddlAdvanceUsuario_que_Registra").val();
                advanceSearch.Usuario_que_RegistraSearchType = 2;
            }
        }
        if ($("#txtAdvanceFromFecha_inicio_del_Plan").val() != "" || $("#txtAdvanceToFecha_inicio_del_Plan").val() != "") {
            hasParameter = true;
            advanceSearch.Planes_AlimenticiosFecha_inicio_del_PlanFrom = $("#txtAdvanceFromFecha_inicio_del_Plan").val();
            advanceSearch.Planes_AlimenticiosFecha_inicio_del_PlanTo = $("#txtAdvanceToFecha_inicio_del_Plan").val();
        }
        if ($("#txtAdvanceFromFecha_fin_del_Plan").val() != "" || $("#txtAdvanceToFecha_fin_del_Plan").val() != "") {
            hasParameter = true;
            advanceSearch.Planes_AlimenticiosFecha_fin_del_PlanFrom = $("#txtAdvanceFromFecha_fin_del_Plan").val();
            advanceSearch.Planes_AlimenticiosFecha_fin_del_PlanTo = $("#txtAdvanceToFecha_fin_del_Plan").val();
        }
        if ($("#txtAdvanceFromSemana").val() != "" || $("#txtAdvanceToSemana").val() != "") {
            hasParameter = true;
            advanceSearch.Planes_AlimenticiosSemanaFrom = $("#txtAdvanceFromSemana").val();
            advanceSearch.Planes_AlimenticiosSemanaTo = $("#txtAdvanceToSemana").val();
        }
        advanceSearch.PacienteSearchType = 0;
        if ($('#ddlAdvancePaciente').val().length > 0 || $("#txtAdvancePaciente").val() == "") {


            if (isPlanes_AlimenticiosTextSearch) {
                advanceSearch.PacienteName = $("#txtAdvancePaciente").val();
                advanceSearch.PacienteNameSearchType = $("#ddlAdvancePaciente").val();
                advanceSearch.PacienteSearchType = 1;
            }
            else {

                advanceSearch.PacienteToSearch = $("#ddlAdvancePaciente").val();
                advanceSearch.PacienteSearchType = 2;
            }
        }
        advanceSearch.EstatusSearchType = 0;
        if ($('#ddlAdvanceEstatus').val().length > 0 || $("#txtAdvanceEstatus").val() == "") {


            if (isPlanes_AlimenticiosTextSearch) {
                advanceSearch.EstatusName = $("#txtAdvanceEstatus").val();
                advanceSearch.EstatusNameSearchType = $("#ddlAdvanceEstatus").val();
                advanceSearch.EstatusSearchType = 1;
            }
            else {

                advanceSearch.EstatusToSearch = $("#ddlAdvanceEstatus").val();
                advanceSearch.EstatusSearchType = 2;
            }
        }

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

    var advanceSearchQuery = "", isPlanes_AlimenticiosTextSearch = true;

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

        $("#ddlAdvanceUsuario_que_Registra").change(function () {
            isPlanes_AlimenticiosTextSearch = false;
            $("#txtAdvanceUsuario_que_Registra").val('');
        });
        $("#txtAdvancePaciente").keypress(function () {
            isPlanes_AlimenticiosTextSearch = true;
            $("#ddlAdvancePaciente").val('').trigger("chosen:updated");
        });

        $("#ddlAdvancePaciente").change(function () {
            isPlanes_AlimenticiosTextSearch = false;
            $("#txtAdvancePaciente").val('');
        });
        $("#txtAdvanceEstatus").keypress(function () {
            isPlanes_AlimenticiosTextSearch = true;
            $("#ddlAdvanceEstatus").val('').trigger("chosen:updated");
        });

        $("#ddlAdvanceEstatus").change(function () {
            isPlanes_AlimenticiosTextSearch = false;
            $("#txtAdvanceEstatus").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>
}

