{% load staticfiles %}

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="format-detection" content="telephone=no,email=no,address=no">
        <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
        <title>店员授权</title>

        <link href="//res.wx.qq.com/open/libs/weui/0.4.3/weui.min.css" rel="stylesheet" type="text/css" />
        <link href="{% static 'page/css/weui.ext.css' %}?v=1" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div class="container" >
            <div class="weui_cells_title">基本信息</div>
            <div class="weui_cells weui_cells_form">
                <div class="weui_cell weui_cell_select weui_select_after">
                    <div class="weui_cell_hd"><label for="" class="weui_label">经销商</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                        <select id="distributor" class="weui_select" name="select" {% if not modified %}disabled{% endif %}>
                            {% for distributor in distributors %}
                            <option value="{{ distributor.distributor_id }}" {% ifequal distributor.distributor_id clerk_info.distributor_id %}selected{% endifequal %}>{{ distributor.distributor_name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="weui_cell">
                    <div class="weui_cell_hd"><label for="" class="weui_label">姓名</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                        <input id="name" class="weui_input" type="text" value="{{ clerk_info.clerk_name }}" placeholder="请输入姓名" {% if not modified %}disabled{% endif %}>
                    </div>
                </div>
                <div class="weui_cell weui_cell_select weui_select_after">
                    <div class="weui_cell_hd"><label for="" class="weui_label">性别</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                        <select id="sex" class="weui_select" name="select" {% if not modified %}disabled{% endif %}>
                            <option value="1" {% ifequal clerk_info.clerk_sex 1 %}selected{% endifequal %}>男</option>
                            <option value="0" {% ifequal clerk_info.clerk_sex 0 %}selected{% endifequal %}>女</option>
                        </select>
                    </div>
                </div>
                <div class="weui_cell">
                    <div class="weui_cell_hd"><label for="" class="weui_label">手机号</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                        <input id="phone" class="weui_input" type="text" required="required" pattern="[0-9]{11}" value="{{ clerk_info.clerk_phone }}" placeholder="请输入手机号" {% if not modified %}disabled{% endif %}>
                    </div>
                </div>
            </div>

            {% if clerk_info %}
            <div class="weui_cells_title">审核状态</div>
            <div class="weui_cells">
                <div class="weui_cell">
                    <div class="weui_cell_bd weui_cell_primary">
                        <p>状态</p>
                    </div>
                    <div class="weui_cell_ft">
                        {% ifequal clerk_info.status -1 %}已拒绝{% endifequal %}
                        {% ifequal clerk_info.status 0 %}审核中{% endifequal %}
                        {% ifequal clerk_info.status 1 %}已激活{% endifequal %}
                        {% ifequal clerk_info.status 2 %}已禁用{% endifequal %}
                        {% ifequal clerk_info.status 3 %}已删除{% endifequal %}
                    </div>
                </div>
            </div>
            {% endif %}


            {% ifequal clerk_info.status -1 %}
            <div class="weui_cells_title">拒绝原因</div>
            <div class="weui_cells">
                <div class="weui_panel_bd">
                    <div class="weui_media_box weui_media_text">
                        <p class="weui_media_desc">{{ clerk_info.refused_reason|safe|linebreaks }}</p>
                    </div>
                </div>
            </div>
            {% endifequal %}

            <br>

            {% if modified %}<button id="submit" class="weui_btn weui_btn_warn">请等待审核</button>{% endif %}

            <div class="weui_dialog_alert" id="dialog" style="display: none">
                <div class="weui_mask"></div>
                <div class="weui_dialog">
                    <div class="weui_dialog_hd"><strong id="title" class="weui_dialog_title">弹窗标题</strong></div>
                    <div id="content" class="weui_dialog_bd">弹窗内容，告知当前页面信息等</div>
                    <div class="weui_dialog_ft">
                        <a href="javascript:;" class="weui_btn_dialog primary">确定</a>
                    </div>
                </div>
            </div>

            <div id="toast" style="display: none;">
                <div class="weui_mask_transparent"></div>
                <div class="weui_toast">
                    <i class="weui_icon_toast"></i>
                    <p class="weui_toast_content">已完成</p>
                </div>
            </div>
        </div>

        <script src="//cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js"></script>
        <script>
            {% if modified %}
            $(function() {
                function show_error_dialog(title, content) {
                    $('#dialog #title').text(title);
                    $('#dialog #content').text(content);
                    $('#dialog').show();
                }

                function data_check() {
                    var unionid = '{{ request.GET.unionid }}';
                    if (!unionid) {
                        show_error_dialog('微信授权', '微信授权失败，请重新打开页面');
                        return false;
                    }

                    var name = $('#name').val();
                    if (!name) {
                        show_error_dialog('姓名', '姓名错误，请检查重新输入');
                        return false;
                    }

                    var phone = $('#phone').val();
                    var phone_valid = $('#phone').is(':valid');
                    if (!(phone && phone_valid)) {
                        show_error_dialog('手机号', '手机号错误，请检查重新输入');
                        return false;
                    }

                    return {
                        unionid: unionid,
                        openid: '{{ request.GET.openid }}',
                        brand_id: '{{ request.GET.brand_id }}',
                        distributor_id: $('#distributor option:checked').val(),
                        name: name,
                        sex: $('#sex option:checked').val(),
                        phone: phone,
                    }
                }

                $('#submit').click(function () {
                    var check_result = data_check();
                    if (check_result){
                        $.ajax({
                            type: 'POST',
                            url: '{{ domain }}/api/clerk/submit',
                            data: check_result,
                            success: function(data) {
                                if (data.status == 200) {
                                    $('#toast').show();
                                    setTimeout(function () {
                                        $('#toast').hide();
                                    }, 1000);
                                    window.location.reload();
                                } else {
                                    show_error_dialog('错误', data.description);
                                }
                            }
                        })
                    }
                });

                $('#dialog .weui_btn_dialog').click(function () {
                    $('#dialog').hide();
                })
            });
            {% endif %}
        </script>
        <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
        <script type="text/javascript" src="{% static 'kodo/js/jswe-0.0.4.js' %}"></script>
        <script>
            V.initWxData({
                imgUrl: 'http://pai.ai/static/kodo/img/paiai_96_96.png',
                link: 'http://pai.ai/w/o?r=http%3A%2F%2Fkodo.xfoto.com.cn%2Fp%2Fclerk',
                desc: '店员授权',
                title: '店员授权',
                timeLine: ''
            }, true);
            V.hideOptionMenu();
        </script>
    </body>
</html>