<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">

    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base" />

</head>
<body >

<div >
    <p>${synchronizeProjectTime}</p>
    <p>${msg}</p>


    <div class="operate-footer" style="background-color: #f2f2f2 !important">
        <c:if test="${synchronize == null}">


            <button class="layui-btn" id="synchronize"  onclick="synchronize()" >
                同步
            </button>

        </c:if>
    </div>

</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form'], function(){
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        laypage = layui.laypage;
        laydate = layui.laydate;
        form = layui.form;
        table = tableExt.getLayTable();

    });


    function synchronize(){

        $("#synchronize").hide();

        $.ajax({
            type: "POST",
            url: "${ctx}/material/synchronize",
            success: function (result) {
                    layer.msg('操作成功,数据正在同步中', {icon: 1, time: 1000}, function () {
                    });
            },
            error:{

            }
        })
    }




    function reload(){
        table.reload('test', {
            page:{
                curr:1
            }
        });
    }

    /**
     * 编辑
    */
    function editMaterial(id){
        layer.open({
            type: 2,
            title:"编辑",
            resize:false,
            area: ['50%', '65%'],
            content: '${ctx}/material/editMateriaForm?id='+id
        });
    }

    /**
     * 查看零售价
     */
    function viewPrice(code){
        layer.open({
            type: 2,
            title:"项目价格本",
            resize:false,
            area: ['60%', '60%'],
            content: '${ctx}/material/detailPrice?code='+code
        });
    }

    /**
     * 禁用
     */
    function disableMaterial(id){
        layer.confirm('禁用后，该物料将不支持申请、入库等操作，确认禁用该物料？',{
            btn: ['确定', '取消'],
            success: function (layero, index) {
                this.enterEsc = function (event) {
                    if (event.keyCode == 13 ) {
                        //确认  执行的代码
                        disableFunction(id);
                        //关闭弹窗
                        layer.close(index);
                        console.log("回车确认时事件")
                        //取消连续回车键，导致不断弹出询问框
                        $(':focus').blur();
                    }
                };
                //监听键盘事件
                $(document).on('keydown', this.enterEsc);
                //esc键触发取消
                this.escQuit = (ev) => {
                    let code_val = ev.which;
                    if(code_val == 0x1B){
                        $('.layui-layer-btn1').click();
                        return false;
                    }
                };
                $(document).on('keydown', this.escQuit);
            },
            end: function () {
                //取消键盘监听事件
                $(document).off('keydown', this.enterEsc);
                $(document).off('keydown', this.escQuit);
            },
            yes: function () {
                //确认执行的代码
                disableFunction(id);
            },
            no: function () {

            }
        });
    }

    function disableFunction(id){
        $.ajax({
            type: "POST",
            url: "${ctx}/material/disableMaterial",
            data: {
                id: id
            },
            success: function (result) {
                if(result.code=='200'){
                    layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                        window.location.reload();
                    });
                }else {

                    layer.msg('操作失败!', {icon: 2});
                }
            },
            error:{
            }
        })
    }
    /**
     * 启用
    */
    function enableMaterial(id){
        layer.confirm('确认启用该物料？',{
            btn: ['确定', '取消'],
            success: function (layero, index) {
                this.enterEsc = function (event) {
                    if (event.keyCode == 13 ) {
                        //确认  执行的代码
                        enableFunction(id);
                        //关闭弹窗
                        layer.close(index);
                        console.log("回车确认时事件")
                        //取消连续回车键，导致不断弹出询问框
                        $(':focus').blur();
                    }
                };
                //监听键盘事件
                $(document).on('keydown', this.enterEsc);
                //esc键触发取消
                this.escQuit = (ev) => {
                    let code_val = ev.which;
                    if(code_val == 0x1B){
                        $('.layui-layer-btn1').click();
                        return false;
                    }
                };
                $(document).on('keydown', this.escQuit);
            },
            end: function () {
                //取消键盘监听事件
                $(document).off('keydown', this.enterEsc);
                $(document).off('keydown', this.escQuit);
            },
            yes: function () {
                //确认执行的代码
                enableFunction(id);
            },
            no: function () {

            }
        });
    }

    function  enableFunction(id){
        $.ajax({
            type: "POST",
            url: "${ctx}/material/enableMaterial",
            data: {
                id: id
            },
            success: function (result) {
                if(result.code=='200'){
                    layer.msg('启用成功！', {icon: 1, time: 1000}, function () {
                        window.location.reload();
                    });
                }else if(result.code=='2005'){
                    layer.msg('物料状态发生改变，启用失败!', {icon: 2});
                    return
                }else {
                    layer.msg('操作失败!', {icon: 2});
                }
            },
            error:{
            }
        })
    }

    /*工单详情*/
    function orderDetail(id) {
        layer.open({
            type: 2,
            title:" 工单信息",
            resize:false,
            area: ['1100px', '81%'],
            content: '${ctx}/order/getplatformOrderForm?orderId='+id
        });
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

