<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="keywords" content="酷医,酷医云,酷医聚客,病人关系管理,微信管理平台,肾病科宣,慢病管理云平台,SCRM,SPRM,血透管理,科室品牌推广">
    <meta name="description"
        content="酷医云，是专为肾科和血透中心研发的免费血透管理平台，覆盖诊前、诊中、诊后全流程管理；为血透中心提供覆盖智能营销管理、血透管理、慢病管理、医患沟通、进销存管理以及商城管理等全流程一体化的管理平台。全国数百家血透中心信赖的选择。">
    <title>酷医云-填写机构信息</title>
    <link rel="stylesheet" href="/static/css/mobile.css?v=1.0.0">
    <link rel="stylesheet" href="/static/css/weui.css" />
    <link rel="stylesheet" href="/static/css/weuix.css" />

    <script src="/static/js/zepto.min.js"></script>
    <script src="/static/js/zepto.weui.js"></script>
    <script src="/static/js/picker.city.js"></script>
    <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?7577c975fa7ae685efd61deb009d1b5b";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
        </script>
</head>

<body>
    <div class="tip">请正确填写以下信息，后续可在账号设置中修改</div>
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__hd">
                <label class="weui-label">机构名称</label>
            </div>
            <div class="weui-cell__bd">
                <input id="org_name" class="weui-input">
            </div>
        </div>
        <div class="weui-cell arrow">
            <div class="weui-cell__hd">
                <label class="weui-label">机构类型</label>
            </div>
            <div class="weui-cell__bd">
                <input class="weui-input" id="org_cat" type="text" value="">
                <input id="cat_id" type="hidden" value="0">
            </div>
        </div>
        <div class="weui-cell arrow">
            <div class="weui-cell__hd">
                <label class="weui-label">机构地址</label>
            </div>
            <div class="weui-cell__bd">
                <input id="city" class="weui-input" type="text" value="">
                <input id="province" type="hidden" value="">
                <input id="city_n" type="hidden" value="">
                <input id="district" type="hidden" value="">
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd">
                <label class="weui-label">详细地址</label>
            </div>
            <div class="weui-cell__bd">
                <input id="address" class="weui-input">
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd">
                <label class="weui-label">机构联系人</label>
            </div>
            <div class="weui-cell__bd">
                <input id="contact" class="weui-input">
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd">
                <label class="weui-label">机构电话</label>
            </div>
            <div class="weui-cell__bd">
                <input id="org_phone" class="weui-input">
            </div>
        </div>
        <!-- 多选 -->
        <div class="weui-cell weui-cell-flex">
            <div class="weui-cell__hd">
                <label class="weui-label">选择启用的应用</label>
            </div>
            <div class="weui-form">
                <div class="weui-form-li iblock">
                    <input class="weui-form-checkbox" name="appcb" id="app3" value="血透管理" type="checkbox">
                    <label for="app3">
                        <i class="weui-icon-checkbox"></i>
                        <div class="weui-form-text">
                            <p>血透管理</p>
                        </div>
                    </label>
                </div>
                <div class="weui-form-li iblock">
                    <input class="weui-form-checkbox" name="appcb" id="app4" value="慢病管理" type="checkbox">
                    <label for="app4">
                        <i class="weui-icon-checkbox"></i>
                        <div class="weui-form-text">
                            <p>慢病管理</p>
                        </div>
                    </label>
                </div>
                <div class="weui-form-li iblock">
                    <input class="weui-form-checkbox" name="appcb" id="app1" value="SCRM" type="checkbox">
                    <label for="app1">
                        <i class="weui-icon-checkbox"></i>
                        <div class="weui-form-text">
                            <p>SCRM</p>
                        </div>
                    </label>
                </div>
                <div class="weui-form-li iblock">
                    <input class="weui-form-checkbox" name="appcb" id="app5" value="微商城" type="checkbox">
                    <label for="app5">
                        <i class="weui-icon-checkbox"></i>
                        <div class="weui-form-text">
                            <p>微商城</p>
                        </div>
                    </label>
                </div>
            </div>
        </div>
        <!-- 按钮 -->
        <div class="join">
            <button id="submit" class="submit">立即开启智慧管理之旅</button>
        </div>
    </div>
</body>

<script type="text/javascript">
    $(function () {
        $("#app3").attr("checked", "checked");

        var catsJSON = "{{.categories}}";
        var cats = $.parseJSON(catsJSON);

        var catOptions = [];
        for (let index = 0; index < cats.length; index++) {
            catOptions.push(cats[index].name);
        }

        $("#org_cat").picker({
            title: "请选择您的机构类型",
            cols: [{
                textAlign: 'center',
                values: catOptions,
            }],
            onChange: function (p, v, dv) {
                var cat = v[0];
                for (let index = 0; index < cats.length; index++) {
                    const c = cats[index];
                    if (c.name == cat) {
                        $("#cat_id").val(c.id);
                        return;
                    }
                }
                $("#cat_id").val("0");
            },
        });

        $("#city").cityPicker({
            title: "选择目的地",
            onChange: function (picker, codes, city_names) {
                $("#province").val(city_names[0]);
                $("#city_n").val(city_names[1]);
                $("#district").val(city_names[2]);
            }
        });

        $("#submit").click(function() {
            var checkErr = checkInfoFull();
            if (checkErr.length > 0) {
                $.toast(checkErr, "cancel");
                return;
            }

            var open_xt = false;
            var open_cdm = false;
            var open_scrm = false;
            var open_mall = false;
            if ($("#app3").prop("checked") == true) {
                open_xt = true
            }
            if ($("#app4").prop("checked") == true) {
                open_cdm = true
            }
            if ($("#app1").prop("checked") == true) {
                open_scrm = true
            }
            if ($("#app5").prop("checked") == true) {
                open_mall = true
            }
            if (!open_xt && !open_cdm && !open_scrm && !open_mall) {
                $.toast("请选择要启用的应用", "cancel");
                return;
            }

            var postData = {
                name: $("#org_name").val(),
                contact_name: $("#contact").val(),
                province: $("#province").val(),
                city: $("#city_n").val(),
                district: $("#district").val(),
                address: $("#address").val(),
                category: $("#cat_id").val(),
                org_phone: $("#org_phone").val(),
                open_xt: open_xt,
                open_cdm: open_cdm,
                open_scrm: open_scrm,
                open_mall: open_mall,
            }

            $.showLoading("加载中");

            $.ajax({
                url: "/mobile/org/create/submit",
                type: "POST",
                data: postData,
                async: true,
                dataType: "json",
                headers: {
                    "X-Xsrftoken": function() {
                        var xsrf, xsrflist;
                        xsrf = getCookie("_xsrf");
                        if (xsrf == null || xsrf == "" || typeof(xsrf) == "undefined") {
                            return "";
                        }
                        xsrflist = xsrf.split("|");
                        var _xsrf = atob(xsrflist[0]);
                        return _xsrf;
                    }()
                },
                success: function(json) {
                    $.hideLoading();
                    if (json.state == 1) {
                        window.location.href = "/mobile/finish";

                    } else {
                        $.toast(json.msg, "cancel");
                    }
                },
                error: function() {
                    $.hideLoading();
                    $.toast("连接失败，请检查网络", "cancel");
                }
            });
        });
    });

    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

    function checkInfoFull() {
        if ($("#org_name").val().length == 0) {
            return "请填写机构名称";
        }
        if ($("#cat_id").val().length == 0 || $("#cat_id").val() == "0") {
            return "请选择机构类型";
        }
        if ($("#city").val().length == 0) {
            return "请填写机构地址";
        }
        if ($("#address").val().length == 0) {
            return "请填写详细地址";
        }
        if ($("#contact").val().length == 0) {
            return "请填写联系人姓名";
        }
        if ($("#org_phone").val().length > 0) {
            if (/^(\d{3,4}-?\d{7,8}$)|(1\d{10}$)/.test($("#org_phone").val()) == false) {
                return "请填写正确的机构电话";
            }
        }
        return "";
    }
</script>

</html>
