<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta name="format-detection" content="telephone=no"/>
    <title>精准导购系统企业号应用</title>
    <link rel="stylesheet" href="/static/css/wechat/weui.min.css"/>
    <link rel="stylesheet" type="text/css" href="/static/css/wechat/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="/static/css/wechat/newstyle.css?v=04130948">
</head>

<body ontouchstart>
<header>
    <div class="header">
        <a href="javascript:history.go(-1);" class="btn-a-back"><i class="iconfont">&#xe61f;</i></a>
        <h2>跟踪客户</h2>
    </div>
</header>
<input type="hidden" id="customerId" th:value="${customerId}"/>
<input type="hidden" id="phone" th:value="${phone}"/>
<div class="weui-cells weui-cells_form mt0">
    <div class="weui-cell weui-cell_select weui-cell_select-after">
        <div class="weui-cell__hd">
            <label for="" class="weui-label">购房意向</label>
        </div>
        <div class="weui-cell__bd" th:switch="${buyIntention}">
            <select class="weui-select" name="buyIntention" id="buyIntention">
<!--                <option value="99" th:selected="${buyIntention==''}">待定</option>-->
                <option value="1" th:selected="${buyIntention=='1'}">A</option>
                <option value="2" th:selected="${buyIntention=='2'}">B</option>
                <option value="3" th:selected="${buyIntention=='3'}">C</option>
                <option value="4" th:selected="${buyIntention=='4'}">D</option>
                <!--<option value="5" th:selected="${buyIntention=='5'}">E</option>-->
            </select>
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__bd">
            <textarea class="weui-textarea" id="callContent" placeholder="请在此输入跟踪或回访信息" rows="5"
                      maxlength="600"></textarea>
            <div class="weui-textarea-counter"><span id="contentLengthStr">0</span>/600</div>
        </div>
    </div>
    <div class="weui-cell" style="flex-wrap: wrap;">
        <span th:each="template : ${callbackTemplateList}">
            <a style="margin-right: 15px;margin-bottom: 15px;" onclick="setCallbackContent(this)" class="weui-btn weui-btn_mini weui-btn_default" th:text="${template.content}"></a>
        </span>
    </div>
</div>
<div class="btn-modify">
    <a class="weui-btn weui-btn_blue" href="javascript:saveRemarks()" id="showToast">提交</a>
</div>


<!--提交成功提示-->
<div id="toast" style="display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-success-no-circle weui-icon_toast"></i>
        <p class="weui-toast__content">提交成功</p>
    </div>
</div>

<div class="js_dialog" id="deleteDialog" style=" display: none;">
    <div class="weui-mask"></div>
    <div class="weui-dialog">
        <div class="weui-dialog__hd"><strong class="weui-dialog__title">设置无效后，客户将被转移至公海，是否继续？</strong></div>
        <div class="weui-dialog__bd"></div>
        <div class="weui-dialog__ft">
            <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default">否</a>
            <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">是</a>
        </div>
    </div>
</div>

<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/statistics.js"></script>
<script type="text/javascript">
    String.prototype.trim=function(){
        return this.replace(/(^\s*)|(\s*$)/g, "");
    }

    var contentLength = 0;
    $(function () {

        var textarea = document.getElementById("callContent");

        if (textarea.addEventListener) {
            // all browsers except IE before version 9
            textarea.addEventListener("input", OnInput, false);
            // Google Chrome and  Safari
            textarea.addEventListener("textInput", OnTextInput, false);
            // Internet Explorer from version 9
            textarea.addEventListener("textinput", OnTextInput, false);
        }

        if (textarea.attachEvent) {
            textarea.attachEvent("onpropertychange", OnPropChanged);
        }

        // Google Chrome, Safari and Internet Explorer from version 9
        function OnTextInput(event) {
            $("#contentLengthStr").text($(this).val().length);
        }

        // Firefox, Google Chrome, Opera, Safari from version 5, Internet Explorer from version 9
        function OnInput(event) {
            $("#contentLengthStr").text($(this).val().length);
        }

        // Internet Explorer
        function OnPropChanged(event) {
            // 使用event.propertyName 过滤
            if (event.propertyName.toLowerCase() == "value") {
                $("#contentLengthStr").text($(this).val().length);
            }
        }

        $('#callContent').bind('input propertychange', function () {
            $("#contentLengthStr").text($(this).val().length);
        });

        document.getElementById("callContent").addEventListener("input", function () {
            contentLength = this.value.length;
            $("#contentLengthStr").text(contentLength);
        })

        $('#deleteDialog').on('click', '.weui-dialog__btn_default', function () {
            $('#deleteDialog').fadeOut(200);
        });

        $('#deleteDialog').on('click', '.weui-dialog__btn_primary', function () {
            var callContent = $("#callContent").val();
            var buyIntention = $("#buyIntention").val();

            $.ajax({
                url: "/project/waitaddvisit",
                data: {
                    customerId: $("#customerId").val(),
                    callContent: callContent,
                    buyIntention: buyIntention
                },
                type: "post",
                success: function (data) {
                    if (data.result == "1") {
                        // 提交成功提示
                        var $toast = $('#toast');
                        $toast.find("i").removeClass().addClass("weui-icon_toast").addClass("weui-icon-success-no-circle")
                        $toast.find("p").text("提交成功");
                        if ($toast.css('display') != 'none') return;
//                        $('#deleteDialog').fadeOut(200);
                        $toast.fadeIn(100);
                        window.location.href="/customer/myWaitCallCustomer";
//                        setTimeout(function () {
//                            $toast.fadeOut(100);
//                            history.go(-1);
//                        }, 2000);
                    }
                }
            });
        });
    });
    function saveRemarks() {
        var callContent = $("#callContent").val().trim();
        if (callContent == "" || callContent == undefined) {
            showMessage("error", "回访信息不能为空", 1000);
            return false;
        }
        var buyIntention = $("#buyIntention").val();
        if (buyIntention == '5') {
            var deleteDialog = $('#deleteDialog');
            deleteDialog.fadeIn(200);
        } else {
            $.ajax({
                url: "/project/waitaddvisit",
                data: {
                    customerId: $("#customerId").val(),
                    callContent: callContent,
                    buyIntention: buyIntention
                },
                type: "post",
                success: function (data) {
                    if (data.result == "1") {
                        // 提交成功提示
                        var $toast = $('#toast');
                        $toast.find("i").removeClass().addClass("weui-icon_toast").addClass("weui-icon-success-no-circle")
                        $toast.find("p").text("提交成功");
                        if ($toast.css('display') != 'none') return;
                        $('#deleteDialog').fadeOut(200);
                        $toast.fadeIn(100);
                        setTimeout(function () {
                            $toast.fadeOut(100);
                            history.go(-1);
                        }, 2000);
                        window.location.href="/customer/myWaitCallCustomer";
                    }
                }
            });
        }
    }

    function showMessage(type, message, timeout, url) {
        var $toast = $('#toast');
        if (type == "error") {
            $toast.find("i").removeClass().addClass("weui-icon_toast")
        } else if (type == "success") {
            $toast.find("i").removeClass().addClass("weui-icon_toast").addClass("weui-icon-success-no-circle")
        }
        $toast.find("p").text(message);
        if ($toast.css('display') != 'none') return;

        $toast.fadeIn(100);
        setTimeout(function () {
            $toast.fadeOut(100);
            if (url != undefined) {
                window.location.href = url;
            }
        }, timeout);
    }

    function setCallbackContent(template) {
        var content = $(template).text();
        $('#callContent').val(content);
        $("#contentLengthStr").text(content.length);
    }
</script>
</body>

</html>
