<#assign title="我的账号"/>
<#include "/cust/frag.head.ftl" />

<header class="demos-header">
    <h1 class="demos-title">${title}</h1>
</header>

<div class="weui-cells weui-cells_form">
<#--<#if user?? && user.openId??>-->
<#--<input type="hidden" id="openId" value="${user.openId}">-->
<#--</#if>-->
    <input type="hidden" id="placeId" value="${placeId}"/>
    <input type="hidden" id="userType" value="${userType}"/>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
        <div class="weui-cell__bd">
            <#if user??>
                <input class="weui-input" id="staffName" placeholder="姓名" readonly="readonly"
                       value="${user.staffName}"/>
            <#else >
                <input class="weui-input" id="staffName" placeholder="姓名"/>
            </#if>
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd">
            <label class="weui-label">手机号</label>
        </div>
        <div class="weui-cell__bd">
<#if user??>
    <input class="weui-input" id="phone" type="tel" readonly="readonly" value="${user.phone}"/>
<#else >
    <input class="weui-input" id="phone" type="tel" placeholder="手机号"/>
</#if>
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">场地</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="placeName" readonly="readonly" value="${placeName}"/>
        </div>
    </div>

    <div class="weui-cell">
        <div class="weui-cell__hd">
            <label class="weui-label">角色</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="userTypeName" readonly="readonly" value="${userType}"/>
        </div>
    </div>

</div>

<div class="weui-btn-area">
    <a id="btnOk" class="weui-btn weui-btn_primary" href="javascript:">绑定账号</a>
</div>

<#include "/cust/frag.foot.ftl" />
<script>

    (function ($) {

        var userType = '${userType}';
        var placeId = '${placeId}';
        var higherId = '${higherId}';
        var openId = '${openId}';
        var nickName = '${nickName}';
        var sexDesc = '${sexDesc}';
        var sex = '${sex}';
        var city = '${city}';
        var province = '${province}';
        var country = '${country}';
        var headImgUrl = '${headImgUrl}';

        if (userType === 1 || userType === '1') {
            $('#userTypeName').val('一级分销');
        }
        else if (userType === 2 || userType === '2') {
            $('#userTypeName').val('二级分销');
        }

        $('#btnOk').on('click', function () {
            var phone = $('#phone').val();
            var staffName = $('#staffName').val();
            var userType = $('#userType').val();

            if (staffName === null || staffName === '') {
                $.alert('请填写姓名');
                return;
            }
            if (phone === null || phone === '') {
                $.alert('请填写手机号');
                return;
            }

            var data = {
                'placeId': placeId,
                'staffName': staffName,
                'phone': phone,
                'userType': userType,
                'higherId': higherId,
                'openId': openId,
                'nickName': nickName,
                'sexDesc': sexDesc,
                'sex': sex,
                'city': city,
                'province': province,
                'country': country,
                'headImgUrl': headImgUrl
            };

            $.showLoading();
            $.ajax({
                url: '/cust/user_bind.json',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                type: 'post',
                success: function (data) {
                    if (data.success) {
                        window.location.replace('/prompt/prompt.html?title=绑定成功&message=' + staffName + '-' + $('#userTypeName').val());
                    } else {
                        $.alert(data.msg);
                    }
                },
                error: function () {
                    $.alert("网络异常");
                },
                complete: function () {
                    $.hideLoading();
                }
            });

        });

    })(jQuery);
    //
    // Date.prototype.Format = function (fmt) {
    //     var o = {
    //         "M+": this.getMonth() + 1, //月份
    //         "d+": this.getDate(), //日
    //         "H+": this.getHours(), //小时
    //         "m+": this.getMinutes(), //分
    //         "s+": this.getSeconds(), //秒
    //         "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    //         "S": this.getMilliseconds() //毫秒
    //     };
    //     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    //     for (var k in o)
    //         if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    //     return fmt;
    // }

</script>