<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>离线盘点</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <style type="text/css">
        .pdtb table {
            width: 100%;
        }

        .pdtb table tr td {
            text-align: center;
        }

        .pdtb table thead th {
            background: #fff;
            text-align: center;
        }

        .checkedcss td {
            background: #66FFCC;
        }

        .uncheckedcss td {
            background: #fff;
        }
    </style>
    }
</head>

<body>
    <header class="aui-bar aui-bar-nav" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="letmeclose()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="titlelocation">离线盘点</div>
    </header>
    <ul class="aui-list aui-form-list" id="dispDetail" style="margin-top: 60px;">
        <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label-icon">
                    <i class="aui-iconfont aui-icon-mobile"></i>
                </div>
                <div class="aui-list-item-input">
                    <input type="text" placeholder="料号" id="metalNum" class="mo" />
                </div>
                <div class="aui-list-item-label-icon" onclick="saometal()">
                    <img src="../image/qrcode_dark.png" style="width: 20px;height: 20px;" />
                </div>
            </div>
        </li>
        <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label-icon">
                    <i class="aui-iconfont aui-icon-lock"></i>
                </div>
                <div class="aui-list-item-input">
                    <input type="number" placeholder="数量" class="quantity" />
                </div>
            </div>
        </li>
    </ul>
    <section class="aui-content-padded  " id="detail">
        <script id="DotTemplate" type="text/x-dot-template">
            <div class="aui-card-list">
                <div class="aui-card-list-header aui-card-list-user">
                    <div class="aui-card-list-user-name">
                        <div class="aui-font-size-16">
                            <b>{{= it.order_number}}</b> &nbsp;
                            <span style="color:#ccc;"></span>&nbsp;
                        </div>
                        <small>{{= getWmsPandianStatus(it.order_status)}}</small>
                    </div>
                </div>
                <div class="aui-card-list-content aui-border-t" style="margin-left: 1rem;">
                    <ul class="aui-list aui-list-noborder">
                        <div class="aui-list-item aui-padded-l-0 aui-padded-r-15" style="min-height: 1.2rem;">
                            <div class="aui-list-item-title aui-font-size-14 goods-title">创建人:{{= it.creatername}}</div>
                            <div class="aui-list-item-title aui-font-size-14 goods-title">创建时间:{{= it.create_time}}</div>
                        </div>
                        <div class="aui-list-item aui-padded-l-0 aui-padded-r-15" style="min-height: 1.2rem;">
                            <div class="aui-list-item-title aui-font-size-14 goods-title">审核人:{{= it.auditname}}</div>
                            <div class="aui-list-item-title aui-font-size-14 goods-title">审核时间:{{= it.audittime}}</div>
                        </div>
                        <div class="aui-list-item aui-padded-l-0 aui-padded-r-15" style="min-height: 1.2rem;">
                            <div class="aui-list-item-title aui-font-size-14 goods-title">种类:{{= it.totalCount}}</div>
                            <div class="aui-list-item-title aui-font-size-14 goods-title" id="sumcheckcount">已盘点数:{{= it.checkCount}}</div>
                        </div>
                    </ul>
                </div>
            </div>
        </script>
    </section>
    <div class="aui-content-padded pdtb" style="margin-bottom: 75px;">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>料号/批次号/序号</th>
                    <th>总数</th>
                    <th>已盘数</th>
                </tr>
            </thead>
            <tbody id="con1">
                <script id="DotTemplate1" type="text/x-dot-template">
                    {{~it.detail:value:index}} {{ if(value.isChecked == 1){ }}
                    <tr class="checkedcss">
                        {{ } else { }}
                        <tr class="uncheckedcss">
                            {{ } }}
                            <td style="width:10%">{{= index+1}}</td>
                            <td style="width:50%">
                                {{ if (value.manageMode == 1) { }} {{= value.batch}} {{ } }} {{ if (value.manageMode == 2) { }} {{= value.sn}} {{ } }} {{ if (value.manageMode == 3) { }} {{= value.inventorysn}} {{ } }}
                            </td>
                            <td style="width:20%">{{= value.totalCount}}</td>
                            <td style="width:20%">{{= value.checkCount}}</td>
                            <td style="display:none">{{= value.id}}</td>
                        </tr>
                        {{~}}
                </script>
            </tbody>
        </table>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <input type="hidden" name="repairstatus" class="repairstatus">
        <div class="aui-content-padded">
            <div class="aui-list-item-inner aui-list-item-center aui-list-item-btn" id="yjwx_btns">

                <div class="aui-row">
                    <div class="aui-col-xs-6 aui-padded-r-10">
                        <div class="aui-btn aui-btn-info aui-btn-block aui-btn-sm btn_submit">提交（本地）</div>
                    </div>

                    <div class="aui-col-xs-6">
                        <div class="aui-btn aui-btn-primary aui-btn-block aui-btn-sm btn_upload">上传（服务器）</div>
                    </div>
                </div>



            </div>

        </div>
    </footer>
    <input type="hidden" name="" class="data_id">
    <input type="hidden" name="" class="data_json">

</body>

<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/md5.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/swipe.js"></script>
<script type="text/javascript" src="../script/SHA1.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/remotedb.js"></script>
<script type="text/javascript" src="../script/localdb.js"></script>
<script type="text/javascript" src="../script/list.js"></script>
<script type="text/javascript" src="../script/aui-tab.js"></script>
<script type="text/javascript" src="../script/aui-toast.js"></script>
<script type="text/javascript" src="../script/aui-selectbox.js"></script>
<script type="text/javascript" src="../script/aui-popup-new.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/jquery.min.js"></script>
<script type="text/javascript" src="../script/CryptoJS/core.js"></script>
<script type="text/javascript" src="../script/CryptoJS/cipher-core.js"></script>
<script type="text/javascript" src="../script/CryptoJS/mode-ecb.js"></script>
<script type="text/javascript" src="../script/CryptoJS/aes.js"></script>
<script type="text/javascript" src="../script/lang.js"></script>
<script type="text/javascript" src="../script/aui-collapse.js"></script>

<script type="text/javascript">
    var host, id, params, Template1;

    apiready = function() {
        fnReady();
        initRequire()
        ajaxsetup();
        host = $api.getStorage('host');
        id = api.pageParam.id;
        // params = api.pageParam.params;
        // if (params) {
        //     var paramsobj = JSON.parse(decodeURIComponent(api.pageParam.params));
        //     id = paramsobj.id;
        //     console.log('解析后的id' + id);
        // }
        console.log("id" + id);
        fnInitLocal();
        getDetail(id);
        if (headerHeight > 69) {
            $("#dispDetail").attr("style", "margin-top: 84px;");
        }
    };

    function getDetail(id) {

        fnSelectPandianById(id, function(ret, err) {
            // console.log(JSON.stringify(ret));
            // console.log(JSON.stringify(err));
            if (ret.status && ret.data.length > 0) {
                // console.log('读详情');
                // console.log(ret.data[0].jsonCon);
                var json = decodeURIComponent(ret.data[0].jsonCon);
                console.log(json);

                var testTemplate = doT.template($("#DotTemplate").text());
                $("#detail").html(testTemplate(JSON.parse(json)));
                Template1 = doT.template($("#DotTemplate1").text());

                var tranjson = JSON.parse(json);
                tranjson.detail.sort(function(a, b) {
                    if (a.isChecked > b.isChecked) return 1;
                    if (a.isChecked < b.isChecked) return -1;
                    return 0;
                });
                $("#con1").html(Template1(tranjson));
                $('.data_id').val(JSON.parse(json).id);
                $('.data_json').val(json);
            }
        });
    }

    function reloadMe() {
        location.reload();
    }


    $(".btn_submit").click(function() {
        var chkedId;
        var metalNum = $('#metalNum').val();
        if (!metalNum) {
            customToast("料号不能为空！", 2000);
            return;
        }
        var quantity = $('.quantity').val();
        if (!quantity) {
            customToast("请填写数量！", 2000);
            return;
        }
        var conLength = $("#con1 tr").length;
        var metalCont;
        var hasExist = false;
        for (var i = 0; i < conLength; i++) {
            if ($("#con1 tr")[i].cells[1].innerHTML.trim().toLowerCase() == metalNum.toLowerCase()) {
                $("#con1 tr")[i].cells[3].innerHTML = quantity;
                $($("#con1 tr")[i]).removeClass('uncheckedcss');
                $($("#con1 tr")[i]).addClass('checkedcss');
                chkedId = $("#con1 tr")[i].cells[4].innerHTML;
                hasExist = true;
                break;
            }
        }
        if (!hasExist) {
            customToast("料号不存在！", 2000);
            return;
        }
        // var _this = $(this);
        // var mo = $('.mo').val();
        var id = $('.data_id').val();
        // var quantity = $('.quantity').val();
        // if (!quantity) {
        //     customToast("请填写数量", 2000);
        // }

        var json = $('.data_json').val();
        var obj = JSON.parse(json);
        var isin = 0;
        var chkedCnt = 0;
        $.each(obj.detail, function(index, val) {
            if (val.id == chkedId) {
                isin = 1;
                val.checkCount = parseInt(quantity);
                val.isChecked = '1';
            }
            if (val.isChecked == '1') {
                chkedCnt++;
            }
        });
        obj.checkCount = chkedCnt;
        if (isin === 0) {
            customToast("料号不正确", 2000);
            return;
        }
        $('#sumcheckcount').html('已盘点数:' + chkedCnt);

        obj.detail.sort(function(a, b) {
            if (a.isChecked > b.isChecked) return 1;
            if (a.isChecked < b.isChecked) return -1;
            return 0;
        });
        $("#con1").html(Template1(obj));

        // console.log(JSON.stringify(obj));
        fnInsertPandian(id, JSON.stringify(obj), function(ret, err) {
            if (ret) {
                console.log(JSON.stringify(ret));
            }
            if (err) {
                customToast(err, 2000);
                return;
            }
            // console.log(JSON.stringify(ret));
            // console.log(JSON.stringify(err));
        });
        // $("#con1").html(Template1(obj));
        $('.data_json').val(JSON.stringify(obj));
        customToast("本地提交成功！", 2000);
        return;
    });

    $(".btn_upload").click(function() {
        // var _this = $(this);
        var id = $('.data_id').val();
        var json = $('.data_json').val();
        var subobj = {};
        subobj.id = id;
        var arr = [];
        if (json) {
            var obj = JSON.parse(json);
            $.each(obj.detail, function(index, val) {
                if (val.isChecked == '1') {
                    var node = {
                        "id": val.id,
                        "num": val.checkCount
                    }
                    arr.push(node);
                }
            });
            subobj.nodes = arr;
        }
        console.log(JSON.stringify(subobj));
        $.ajax({
                url: host + "/wms/App/UpdateCheck",
                data: JSON.stringify(subobj),
                type: 'POST',
                success: function(result) {
                    console.log(JSON.stringify(result));
                    if (result.ret == 1) {
                        // customToast(lang[result.ret], 2000);
                        letmeclose(lang[result.ret]);
                    } else {
                        customToast(lang[result.msg], 2000);
                    }
                },
                error: function(error) {
                    console.log(JSON.stringify(error));
                    customToast(lang['9999999'], 2000);
                }
            })
            // letmeclose();
    });

    function saometal() {
        var FNScanner = api.require('FNScanner');
        FNScanner.open({
            autorotation: true,
            isAlbum: true
        }, function(ret, err) {
            if (ret) {
                if (ret.eventType == "success") {
                    var datartn = eval('(' + JSON.stringify(ret) + ')');
                    if (datartn['content']) {
                        $('#metalNum').val(datartn['content']);
                    }
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function letmeclose(pmsgcont) {
        api.sendEvent({
            name: 'refreashOfflineData',
            extra: {
                msgcont: pmsgcont
            }
        });
        closethisWin();
    }
</script>

</html>
