<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
    <meta http-equiv="cache-control" content="max-age=0" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
    <meta http-equiv="pragma" content="no-cache" />
    <title>房间号：<%: ViewData["RoomNumber"] %></title>
    <link type="text/css" rel="stylesheet" href="/Styles/WeiXin/common.css" />
    <link type="text/css" rel="stylesheet" href="/Styles/WeiXin/tip.css" />
    <link type="text/css" rel="stylesheet" href="/Styles/WeiXin/air.css" />
    <script type="text/javascript" src="/Scripts/jquery.min.js"></script>
    <script type="text/javascript" src="/Scripts/avalon.mobile.min.js"></script>
    <script type="text/javascript" src="/Scripts/WeiXin/tip.js"></script>
    <script type="text/javascript">

        var vm = avalon.define({
            $id: 'air-panel',
            air: [],
            hostId: '',
            airId: '',
            currentIndex: -1,
            Running: false, /*空调运行状态，0/停止，1/运行*/
            SettingTemp: 0, /*设定温度*/
            Speed: 0, /*风速：0/停止, 1/低速, 2/中速, 3/高速, 4/自动*/
            HightTemp: 35, /*最高温度*/
            LowerTemp: 16, /*最低温度*/
            Mode: 5, /*模式:  1/制冷模式，2/制热模式，4/送风模式，8/除湿模式*/
            hostAir: null,
            airClick: function (index) {
                if (vm.currentIndex == index) {
                    return;
                }

                vm.airId = vm.air[index].RoomTypeAirID;
                vm.hostId = vm.air[index].HostID;
                vm.currentIndex = index;

                LoadAirControl(vm.hostId, vm.airId, function (r) {
                    if (r != null) {
                        vm.Running = r.Running;
                        vm.SettingTemp = r.SettingTemp;
                        vm.Speed = r.Speed;
                        vm.HightTemp = r.HightTemp;
                        vm.LowerTemp = r.LowerTemp;
                        vm.Mode = r.Mode;
                    }
                });
            },
            settingAirClick: function (type, val) {
                if (vm.currentIndex == -1) {
                    return;
                }
                var status = 0;
                switch (type) {
                    case "OnOff":
                        vm.Running = !vm.Running;
                        status = vm.Running ? 1 : 0;
                        break;
                    case "SettingTemp":
                        if (val == '+') {
                            if (vm.HightTemp >= vm.SettingTemp) {
                                vm.SettingTemp = vm.SettingTemp + 1;
                            } else {
                                return;
                            }

                        } else if (val == '-') {
                            if (vm.LowerTemp <= vm.SettingTemp) {
                                vm.SettingTemp = vm.SettingTemp - 1;
                            } else {
                                return;
                            }
                        }
                        status = vm.SettingTemp;
                        break;
                    case "Mode":
                        vm.Mode = val;
                        status = vm.Mode;
                        break;
                    case "Speed":
                        vm.Speed = val;
                        status = vm.Speed;
                        break;
                }
                setAirCondition(vm.hostId, vm.airId, type, status, function (r) {
                    if (!r.IsSuccess) {
                        showTip(r.Message);
                    }
                    hideLoading();
                });
            }
        });

        function setAirCondition(HostId, AirId, Type, Status, callback) {
            showLoading();
            $.ajax({
                url: '/WeiXin/Air/',
                type: 'POST',
                async: false,
                dataType: 'JSON',
                data: {
                    roomNumber: $('#hfRoomNumber').val(),
                    airId: AirId,
                    property: Type,
                    status: Status
                },
                success: callback
            });
        }

        function LoadAirControl(hostID, roomTypeAirID, callback) {
            showLoading();
            $.ajax({
                url: '/WeiXin/LoadAirSetting/',
                data: { hostId: hostID, airId: roomTypeAirID },
                type: "POST",
                dataType: "JSON",
                success: callback
            });
        }

        $(function () {
            showLoading();
            $.ajax({
                url: "/WeiXin/LoadAir/",
                data: { roomNumber: $('#hfRoomNumber').val() },
                type: "POST",
                dataType: "JSON",
                success: function (r) {
                    vm.air = r;
                    if (vm.air.length > 0) {
                        vm.airId = vm.air[0].RoomTypeAirID;
                        vm.hostId = vm.air[0].HostID;
                        vm.currentIndex = 0;

                        LoadAirControl(vm.hostId, vm.airId, function (r) {
                            if (r != null) {
                                vm.Running = r.Running;
                                vm.SettingTemp = r.SettingTemp;
                                vm.Speed = r.Speed;
                                vm.HightTemp = r.HightTemp;
                                vm.LowerTemp = r.LowerTemp;
                                vm.Mode = r.Mode;
                            }
                            hideLoading();
                        });
                    }
                    hideLoading();
                }
            });
        });

    </script>
</head>
<body>
    <input id="hfRoomNumber" type="hidden" name="RoomNumber" value='<%: ViewData["RoomNumber"] %>' />
    <div id="air-panel" class="clearfix" ms-controller="air-panel">
        <div>
            <ul class="air-list-panel clearfix">
                <li ms-repeat="air"><a href="javascript:void(0)" class="air-btn air-btn-air" ms-class="{{$index == currentIndex? 'selected':''}}"
                    ms-click='airClick($index)'>{{el.Name}}</a></li>
            </ul>
        </div>
        <div id="air-switch-bar" class="clearfix">
            <a id="air-switch" href="javascript:void(0)" ms-class="{{Running ? 'selected':''}}"
                ms-click="settingAirClick('OnOff','')"><span class="icon-air-switch"></span>
            </a>
        </div>
        <div id="air-temp-bar" class="clearfix">
            <a id="air-temp-down" href="javascript:void(0)" ms-click="settingAirClick('SettingTemp','-')">
                <span class="icon-temp-down"></span></a><span id="air-temp">{{SettingTemp}}<i>℃</i></span>
            <a id="air-temp-up" href="javascript:void(0)" ms-click="settingAirClick('SettingTemp','+')">
                <span class="icon-temp-up"></span></a>
        </div>
        <div id="air-speed-bar" class="clearfix">
            <a id="air-speed-auto" href="javascript:void(0)" ms-class="{{Speed == 4 ? 'selected':''}}"
                ms-click="settingAirClick('Speed','4')"><i class="icon icon-speed-auto"></i><em class="text">
                    自动</em> </a><a id="air-speed-low" href="javascript:void(0)" ms-class="{{Speed == 1 ? 'selected':''}}"
                        ms-click="settingAirClick('Speed','1')"><i class="icon icon-speed-low"></i><em class="text">
                            低风</em> </a><a id="air-speed-middle" href="javascript:void(0)" ms-class="{{Speed == 2 ? 'selected':''}}"
                                ms-click="settingAirClick('Speed','2')"><i class="icon icon-speed-middle"></i><em
                                    class="text">中风</em> </a><a id="air-speed-high" href="javascript:void(0)" ms-class="{{Speed == 3 ? 'selected':''}}"
                                        ms-click="settingAirClick('Speed','3')"><i class="icon icon-speed-high"></i><em class="text">
                                            高风</em> </a>
        </div>
        <div id="air-mode-bar">
            <a id="air-mode-cold" href="javascript:void(0)" ms-class="{{Mode == 1 ? 'selected':''}}"
                ms-click="settingAirClick('Mode','1')"><i class="icon icon-mode-cold"></i><em class="text">
                    制冷</em> </a><a id="air-mode-hot" href="javascript:void(0)" ms-class="{{Mode == 2 ? 'selected':''}}"
                        ms-click="settingAirClick('Mode','2')"><i class="icon icon-mode-hot"></i><em class="text">
                            制热</em> </a><a id="air-mode-wind" href="javascript:void(0)" ms-class="{{Mode == 4 ? 'selected':''}}"
                                ms-click="settingAirClick('Mode','4')"><i class="icon icon-mode-wind"></i><em class="text">
                                    扫风</em> </a>
        </div>
    </div>
    <% Html.RenderPartial("Footer"); %>
</body>
</html>
