<!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" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" id="header">
        <a class="aui-pull-left aui-btn " tapmode onclick="domeclose()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">报工</div>
    </header>

    <section class="aui-content" id="subheader">
        <div class="aui-grid aui-bg-info">
            <div class="aui-row sub-title c_stationDesc" id="classify"></div>
        </div>
    </section>

    <div class="aui-content aui-margin-b-15 container">
        <ul class="aui-list aui-select-list" id="baogong">
            <li class="aui-list-header">已上料没有报工完成信息:</li>
            <script id="baogongTemplate" type="text/x-dot-template">
                {{~it.data:value:index}}
                <li class="aui-list-item baogong-item" row-id="2">
                    <div class="aui-list-item-label">
                        <input class="aui-radio c_traceId" type="radio" name="traceId" value="{{= value.traceId}}" data-workcode="{{= value.workCode}}" data-traceId="{{= value.traceId}}">
                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-info">
                            <div class="aui-info-item">工单: {{= value.workCode}}</div>
                            <div class="aui-info-item ">筐号: {{= value.number ? value.number : ''}} </div>
                        </div>
                        <div class="aui-info">
                            <div class="aui-info-item">品名: {{= value.productName}}</div>
                            <div class="aui-info-item">型号: {{= value.productType}}</div>
                        </div>
                        <div class="aui-info">
                            <div class="aui-info-item">上料数量: {{= value.upCount}}</div>
                            <div class="aui-info-item">{{= value.time}}</div>
                        </div>
                        <div class="aui-info">
                            <div class="aui-info-item">已报工: {{= value.okCount + value.nokCount + value.nokCount2}}</div>
                            <div class="aui-info-item">NG数量: {{= value.nokCount + value.nokCount2}}</div>
                        </div>
                    </div>
                </li>
                {{~}}
            </script>

        </ul>
    </div>

    <div class="aui-content lastcont">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        筐号
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" class="c_number" placeholder="筐号">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div onClick="doSelectUser()" class="aui-list-item-label">
                        人员
                    </div>
                    <!-- <div class="aui-list-item-input">
                        <select id="userid">
                            <option>Option1</option>
                            <option>Option2</option>
                            <option>Option3</option>
                        </select>
                    </div> -->
                    <div class="aui-list-item-input" id="usedisplay" onClick="doSelectUser()"></div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        OK数量
                    </div>
                    <div class="aui-list-item-input">
                        <input type="number" class="c_okcount" placeholder="请输入OK数量" pattern="[0-9]*">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        工废数量
                    </div>
                    <div class="aui-list-item-input">
                        <input type="number" class="c_nokcount" placeholder="请输入工废数量" pattern="[0-9]*">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        料废数量
                    </div>
                    <div class="aui-list-item-input">
                        <input type="number" class="c_nokcount2" placeholder="请输入料废数量" pattern="[0-9]*">
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-content-padded">
            <div class="aui-list-item-inner aui-list-item-center aui-list-item-btn">
                <p>
                    <div class="aui-btn aui-btn-info aui-btn-block aui-btn-sm baogong_submit">提交</div>
                </p>
            </div>
        </div>
    </footer>

</body>

<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/md5.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/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/zepto.min.js"></script>
<script type="text/javascript" src="../script/jquery.min.js"></script>
<script type="text/javascript" src="../script/lang.js"></script>

<script type="text/javascript">
    var stationDesc, station, machine, qrcode, host, fromqrcode, user, selecteduserid, UIListCheck;
    var userWinshow = false;

    apiready = function() {
        fnReady();
        ajaxsetup();
        host = $api.getStorage('host');
        user = JSON.parse($api.getStorage('current_user'));
        selecteduserid = user.mid;
        $('#usedisplay').html(user.name);
        console.log(user);
        var c_params = JSON.parse(decodeURIComponent(api.pageParam.params));
        stationDesc = c_params.stationDesc;
        station = c_params.station;
        machine = c_params.machine;
        qrcode = api.pageParam.qrcode;
        fromqrcode = c_params.qrcode;
        $('.c_stationDesc').html(stationDesc);
        $('.c_number').val(qrcode);
        var url = host + '/acc/ProductReport/UnReportList?station=' + station + '&machineid=' + machine;
        console.log(JSON.stringify({
            "Number": qrcode
        }));
        $.ajax({
            url: host + "/acc/ProductReport/CheckBasket",
            data: JSON.stringify({
                "Number": qrcode
            }),
            dataType: 'json',
            　　　　　　contentType: 'application/json',
            type: 'POST',
            success: function(result) {
                console.log(JSON.stringify(result));
                if (result.ret == 1) {
                    getList(url, qrcode);
                } else {
                    customToast(lang[result.msg], 2000);
                    setTimeout(function() {
                        api.closeFrame();
                        api.closeWin();
                    }, 1000);
                }
            }
        })
    };

    function doSelectUser() {
        $.ajax({
            url: host + '/acc/ProductReport/GetUsers',
            type: 'get',
            success: function(result) {
                // console.log(JSON.stringify(result));
                var userDrop = [];
                if (result.ret = '1') {
                    $.each(result.data, function(index, val) {
                        var useroption = {};
                        useroption.employee_id = val.userID;
                        useroption.name = val.userName;
                        useroption.phonetic = val.phonetic;
                        useroption.selected = false;
                        userDrop.push(useroption);
                    });
                    UIListCheck = api.require('UIListCheck');
                    UIListCheck.open({
                        rect: {
                            x: 0,
                            y: headerHeight,
                            w: api.frameWidth,
                            h: api.frameHeight - headerHeight - 10
                        },
                        isRadio: true,
                        contacts: userDrop,
                        fixedOn: api.frameName
                    }, function(ret) {
                        if (ret.eventType == 'click') {
                            // alert(JSON.stringify(selectedUser));
                            if (ret.selected) {
                                selecteduserid = ret.contact.employee_id;
                                $('#usedisplay').html(ret.contact.name);
                            } else {
                                selecteduserid = '';
                                $('#usedisplay').html('');
                            }
                            userWinshow = false;
                            UIListCheck.close();
                        } else if (ret.eventType == 'show') {
                            userWinshow = true;
                        }
                    });
                } else {
                    customToast(result.msg, 2000);
                }
            },
            error: function(error) {
                // console.log(error)
                customToast(lang['9999999'], 2000);
            }
        })
    }

    function domeclose() {
        if (userWinshow) {
            userWinshow = false;
            UIListCheck.close();
        } else {
            closeFrame();
        }
    }

    // var url = 'http://61.177.28.246:8100/acc/ProductReport/UnReportList?station=998&machineid=449';
    // getList(url);
    function getList(url, qrcode) {
        $.ajax({
            url: url,
            type: 'get',
            success: function(result) {
                console.log(JSON.stringify(result));
                if (result.ret == 1) {
                    console.log(JSON.stringify(result));


                    if (result.data.length == 0) {
                        customToast("没有数据", 2000);
                        setTimeout(function() {
                            api.closeFrame();
                        }, 1000);
                    }

                    var testTemplate = doT.template($("#baogongTemplate").text());
                    $("#baogong").html('<li class="aui-list-header">已上料没有报工完成信息:</li>' + testTemplate(result));
                    // var c_params = '{"stationDesc":"' + stationDesc + '","machine":"' + machine + '","station":"' + station + '"}';
                    // $(".b_shangliao").attr("data-params", encodeURIComponent(c_params));
                    // getSelectOptions(host + '/acc/ProductReport/GetUsers', "#userid");
                    // setTimeout(function() {
                    //     $('#userid').val(user.mobile).change();
                    // }, 500);
                } else {
                    customToast(lang[result.msg], 2000);
                }
            }
        })

    }


    $(".baogong-item").click(function() {
        $("input[type=radio]").attr("checked", false);
        $(this).find("input[type=radio]").attr("checked", 'checked');
        $(this).find("input[type=radio]").attr("checked", 'checked');
    });

    $(".baogong_submit").click(function() {

        var userid = selecteduserid;
        // console.log(station);
        // console.log(machine);
        console.log(userid);
        var number = $('.c_number').val();
        var okcount = $('.c_okcount').val();
        var nokcount = $('.c_nokcount').val() ? $('.c_nokcount').val() : 0;
        var nokcount2 = $('.c_nokcount2').val() ? $('.c_nokcount2').val() : 0;
        // var traceId = $('.c_traceId').val();
        var traceId = $('input.c_traceId:checked').attr("data-traceId");
        var workcode = $('input.c_traceId:checked').attr("data-workcode");
        if (!workcode) {
            customToast('请选择工单!', 2000);
            return;
        }
        if (!okcount) {
            customToast('请填写OK数量!', 2000);
            return;
        }
        console.log(workcode);
        console.log(JSON.stringify({
            "TraceId": traceId,
            "Station": station,
            "machine": machine,
            "workcode": workcode,
            "number": number,
            "OkCount": okcount,
            "NokCount": nokcount,
            "NokCount2": nokcount2,
            "UserId": userid
        }));
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '请稍后...',
            text: '正在报工...',
            modal: true
        });
        var url = host + '/acc/ProductReport/Report';
        $.ajax({
            url: url,
            data: JSON.stringify({
                "TraceId": traceId,
                "Station": station,
                "machine": machine,
                "workcode": workcode,
                "number": number,
                "OkCount": okcount,
                "NokCount": nokcount,
                "NokCount2": nokcount2,
                "UserId": userid
            }),
            dataType: 'json',
            　　　　　　contentType: 'application/json',
            type: 'post',
            success: function(result) {
                console.log(JSON.stringify(result));
                api.hideProgress();
                if (result.ret == 1) {
                    customToast(lang[result.msg], 2000);
                    // setTimeout(function() {

                        api.openFrame({
                            name: 'shangliaobaogong',
                            reload: true,
                            url: 'shangliaobaogong.html',
                            pageParam: {
                                qrcode: fromqrcode
                            }
                        });

                        api.closeFrame();


                    // }, 1000);

                } else {
                    customToast(lang[result.msg], 2000);
                    //  setTimeout(function(){
                    //   api.closeFrame();
                    // },1000);
                }

            }
        })


    });
</script>

</html>
