@model PantheonRA.BackOffice.Models.DeviceViewModel

<div class="row m-b-sm m-t-sm">
    <div class="col-lg-12">
        <div class="m-b-md tooltips-wrapper">
            @if (Model.CanChangeCompanyUnit)
            {

                <a href="@Url.Action("Edit", "Devices", new { @id = Model.Id })" class="btn btn-primary btn-sm pull-right">@Loc.ChangeCompanyUnit</a>
            }
            else
            {

                <button type="button" class="btn btn-primary btn-sm pull-right" disabled="disabled" data-toggle="tooltip" data-placement="left" data-original-title="@Loc.YouWillNotBeAbleToChangeCompanyUnit">@Loc.ChangeCompanyUnit</button>
                <i class="fa fa-info-circle pull-right" data-toggle="tooltip" data-placement="left" data-original-title="@Loc.YouWillNotBeAbleToChangeCompanyUnit"></i>
            }

            <h2>@Model.Description <small>@@ @Model.CompanyUnitId @Loc.CompanyUnit</small></h2>
        </div>
        @if (Model.IsMasterDevice == "Y")
            {
            <div class="col-lg-12 alert alert-info">
                @Loc.ThisIsMasterDevice
            </div>
        }
    </div>

    <div class="col-md-5 col-sm-12">
        <dl class="dl-horizontal">
            <dt>@Loc.Status</dt>
            @if (Model.IsActivated == "Y")
            {
                <dd id="isActiveTrueDd"><span class="label label-primary">@Loc.Active</span></dd>
                <dd id="isActiveFalseDd" style="display:none"><span class="label label-danger">@Loc.Inactive</span></dd>
            }
            else
            {
                <dd id="isActiveTrueDd" style="display:none;"><span class="label label-primary">@Loc.Active</span></dd>
                <dd id="isActiveFalseDd"><span class="label label-danger">@Loc.Inactive</span></dd>
            }
            <dt>@Loc.DeviceNumber</dt>
            <dd>@Model.DeviceNumber</dd>
            <dt>@Loc.DevicePassword</dt>
            <dd>@Model.DevicePassword</dd>

            @*<dt>Fiscal cert. download:</dt>
                @if (Model.IsFiscalCertDownloadEnabled == "Y")
                {
                    <dd id="isFiscalCertDownloadEnabled" class="text-success"><b>enabled</b></dd>
                    if (Model.IsFiscalCertDownloaded == "Y")
                    {
                        <dt></dt>
                            <dd id="isFiscalCertDownloaded" class="text-success">Fiscal certificate downloaded.</dd>
                    }
                    else
                    {
                        <dt></dt>
                            <dd id="isFiscalCertDownloaded" class="text-warning">Fiscal certificate is not downloaded!</dd>
                    }
                }
                else
                {
                        <dd id="isFiscalCertDownloadEnabled" class="text-danger"><b>disabled</b></dd>
                }*@
        </dl>
    </div>
    <div class="col-md-7 col-sm-12">

        @*@if (Model.IsFiscalCertDownloadEnabled == "Y")
            {
                <button id="changeCertDownloadStatusButton" type="button" class="btn btn-sm btn-block btn-warning btn-outline">Disable cert. download</button>
                <input id="isCertDownloadEnabledHidden" type="hidden" value="true" />
            }
            else
            {
                <button id="changeCertDownloadStatusButton" type="button" class="btn btn-sm btn-block btn-primary">Enable cert. download</button>
                <input id="isCertDownloadEnabledHidden" type="hidden" value="false" />
            }*@

        @if (Model.IsActivated == "Y")
            {
            <div class="alert alert-info">
                <p><b>@Loc.DeviceDeactivationInfo</b></p>
                <p>@Loc.DeactivationMessage</p>

                <div class="hr-line-dashed"></div>
                <button id="changeActiveStatusButton" type="button" class="btn btn-sm btn-block btn-outline btn-success">@Loc.DeactivateDevice</button>
                <input id="isActivatedHidden" type="hidden" value="true" />
            </div>
        }
        else
        {
            @*<button id="changeActiveStatusButton" type="button" class="btn btn-sm btn-block btn-primary">Activate device</button>*@
            <input id="isActivatedHidden" type="hidden" value="false" />
        }
    </div>
</div>

<div class="ibox-footer">
    <div class="pull-right">
        <small>@Loc.LastUpdate @@ @Model.LastChangeTime</small>
    </div>
    <div class="clearfix"></div>
</div>


<script type="text/javascript">
    var isActive = $("#isActivatedHidden").val().toLowerCase() === "true";
    //var isCertificateDownloadEnabled = $("#isCertDownloadEnabledHidden").val().toLowerCase() === "true";
    var deviceId = @Model.Id;

    $('.tooltips-wrapper').tooltip({
        selector: "[data-toggle=tooltip]",
        container: "body"
    });



    function RefreshActiveStatusChangeButton(isActive)
    {
        if(isActive)
        {
            swal({title:"@Model.Description is now activated!", text:"You have activated the device", type:"success", confirmButtonColor: "#18a689"});
            $("#changeActiveStatusButton").addClass("btn-outline").addClass("btn-warning").removeClass("btn-primary").text("Deactivate device");
            $("#isActiveTrueDd").show();
            $("#isActiveFalseDd").hide();
        }
        else
        {

            //$("#changeActiveStatusButton").removeClass("btn-outline").removeClass("btn-warning").addClass("btn-primary").text("Activate device");

            swal({title:"@Model.Description @Loc.PosXIsNowDeactivated", text: "@Loc.YouHaveDeactivatedDevice", type:"success", confirmButtonColor: "#18a689"});
            $("#changeActiveStatusButton").hide();
            $("#isActiveTrueDd").hide();
            $("#isActiveFalseDd").show();
        }
    }


    $(".device-details-wrapper").on("click", "#changeActiveStatusButton", function () {
        swal({
            title: "@Loc.AreYouSureYouWontBeAble",
            text: isActive ? "@Loc.YouWillDeactivatePosX @Model.Description" : "@Loc.YouWillActivatePosX @Model.Description",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#18a689",
            confirmButtonText: "@Loc.Yes",
            cancelButtonText: "@Loc.No",
            showLoaderOnConfirm: true,
            closeOnConfirm: false,
            closeOnCancel: true },
            function(isConfirm){
                if (isConfirm) {
                    $.ajax({
                        method: "POST",
                        url: "@Url.Action("ChangeActiveStatus", "Devices")",
                        data: { isActive: isActive, deviceId: deviceId },
                        success: function (processStatusMessage) {
                            isActive = !isActive;
                            RefreshActiveStatusChangeButton(isActive);
                        },
                        error: function ()
                        {
                            swal({title:"Error", text:"Error happened while changing active status for @Model.Description.", type:"error"});
                        }
                    });
                }
            });
    });
</script>