<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>库存管理-物料基础信息</title>
    <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" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css" />
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab layui-tab-brief">

            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="CKGL_WLJC_WLJC_T" html='<a href="${ctx}/material/findMaterialList"><li class="layui-this">物料信息库</li></a>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10"  lay-submit lay-filter="resetButton" >
                    <i class="layui-icon fs-12">&#xe615;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10" id="searchBtn" lay-submit lay-filter="LAY-user-back-search">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>
            </ul>
        </div>
        <div class="layui-card-body layuiadmin-card-header-auto">
            <form class="layui-form layui-card" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料代码：</label>
                        <div class="layui-input-block">
                            <input type="text" name="code" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">外部编码：</label>
                        <div class="layui-input-block">
                            <input type="text" name="qqCode" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="materialName" class="layui-input" >
                        </div>
                    </div>

                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料类型：</label>
                        <div class="layui-input-block" >
                            <select name="materialTypeId" id="materialTypeId"  >
                                <option value="">请选择</option>
                                <c:forEach items="${types}" var="ty">
                                    <option value="${ty.columns.id}" >${ty.columns.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料规格：</label>
                        <div class="layui-input-block">
                            <input type="text" name="materialSpecs" class="layui-input" >
                        </div>
                    </div>

                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料描述：</label>
                        <div class="layui-input-block">
                            <input type="text" name="remarks" class="layui-input" >
                        </div>
                    </div>

                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">状态：</label>
                        <div class="layui-input-block" >
                            <select name="status">
                                <option value="">请选择</option>
                                <option value="1">使用中</option>
                                <option value="3">已禁用</option>
                            </select>
                        </div>
                    </div>

                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <sfTags:pagePermission authFlag="WLJC_ADD_B" html='<button class="layui-btn layui-btn-sm" lay-event="addMaterial"><i class="layui-icon layui-icon-addition"></i>新增</button>'/>
                   <%-- <sfTags:pagePermission authFlag="WLJC_DELE_B" html=' <button class="layui-btn layui-btn-sm" lay-event="deleteMaterial"><i class="layui-icon layui-icon-export"></i>删除</button>'/>--%>
                    <sfTags:pagePermission authFlag="WLJC_IMPORT_B" html=' <button class="layui-btn layui-btn-sm" lay-event="import"><i class="layui-icon layui-icon-export"></i>导入</button>'/>
                    <button class="layui-btn layui-btn-sm" lay-event="export" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出</button>
                    <button class="layui-btn layui-btn-sm"  id="synchronize" onclick="synchronize()">同步物料价格</button>
                </div>
            </script>
        </div>
    </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();

        //开始日期
        var insStart = laydate.render({
            elem: '#repairTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var insEnd = laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        var enable = '${fns:checkPagePermission(request, "WLJC_ENABLE_B")}';
        var disable = '${fns:checkPagePermission(request, "WLJC_DISA_B")}';
        var editBut = '${fns:checkPagePermission(request, "WLJC_EDIT_B")}';

        tableExt.render({
            elem: '#test'
            , height: 'full-180'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,method: 'post'
            , url: '${ctx}/material/findMaterialPage'
            , cols: [[
                {type: 'numbers',fixed:'left',title:'序号'}
                , {field: 'id', type: 'checkbox',fixed: 'left'}
                , {field: 'id', title: '操作',minWidth:'130', sort: false, align: 'center', templet:function(d){
                        var html = "";
                        if(editBut == '0'){
                            html += "<a href=\"#\" class=\"layui-table-link layui-icon   \" style='font-size:14px;' onclick=\"editMaterial('"+d.id+"')\">编辑</a>&nbsp;&nbsp;";
                        }

                            if(d.status=='3'){
                                if(enable == '0') {
                                    html += "<a href=\"#\" class=\"layui-table-link layui-icon  ml-20 \" style='font-size:14px;' onclick=\"enableMaterial('" + d.id + "')\">启用</a>";
                                }
                            }else{
                                if(disable == '0') {
                                    html += "<a href=\"#\" class=\"layui-table-link layui-icon  ml-20 \" style='font-size:14px;' onclick=\"disableMaterial('" + d.id + "')\">禁用</a>";
                                }
                            }
                            return html;

                    }}
                , {field: 'code', title: '物料代码', minWidth:'150',sort: false, align: 'center',exportable:true ,event:'singleclick'
                    /*,templet:function(d){
                        return "<a href=\"#\" class=\"layui-table-link\" onclick=\"orderDetail('"+d.id+"')\">"+d.code+"</a>";
                    }*/}
                , {field: 'material_name', title: '物料名称', sort: false, align: 'center',exportable:true,minWidth:'200',event:'singleclick'}
                , {field: 'typeName', title: '物料类型', sort: false, align: 'center',exportable:true,minWidth:'100',event:'singleclick'}
                , {field: 'material_specs', title: '物料规格', sort: false, align: 'center',exportable:true,minWidth:'100',event:'singleclick'
                    ,templet:function(d){
                        if (d.typeName == '组件') {
                            return d.material_specs+"Wp";
                        } else {
                            return d.material_specs;
                        }
                    },expFormat:function(d){
                        if (d.typeName == '组件') {
                            return d.material_specs+"Wp";
                        } else {
                            return d.material_specs;
                        }
                    }
                }
                , {field: 'material_model', title: '物料型号', sort: false, align: 'center',exportable:true,minWidth:'100',event:'singleclick'}
                , {field: 'customer_price', title: '零售价', sort: false, align: 'center', templet:function(d){
                        var html = "";
                        html += "<a href=\"#\" class=\"layui-table-link layui-icon   \" style='font-size:14px;' onclick=\"viewPrice('"+d.qq_code+"')\">查看</a>";
                        return html;

                    }}
                , {field: 'remarks', title: '物料描述', sort: false, align: 'center',exportable:true,minWidth:'200',event:'singleclick'}
                , {field: 'qq_code', title: '外部编码', sort: false, align: 'center',exportable:true,minWidth:'100',event:'singleclick'}
                , {field: 'material_brand', title: '物料品牌', sort: false, align: 'center',exportable:true,minWidth:'100',event:'singleclick'}
                , {field: 'unit_name', title: '库存单位', sort: false, align: 'center',minWidth:'60',exportable:true,event:'singleclick'}
                ,{field: 'min_apply_amount', title: '最小发货单位', sort: false, align: 'center',minWidth:'60',exportable:true,event:'singleclick'}
                , {field: 'component_shape', title: '组件玻型', sort: false, align: 'center',exportable:true,minWidth:'25',event:'singleclick'
                    ,templet:function(d){
                        if (d.typeName == '组件') {
                            if (d.component_shape == '1') {
                                return "单玻";
                            } else if (d.component_shape == '2') {
                                return "双玻";
                            }
                        }
                        return "";
                    },expFormat:function(d){
                        if (d.typeName == '组件') {
                            if (d.component_shape == '1') {
                                return "单玻";
                            } else if (d.component_shape == '2') {
                                return "双玻";
                            }
                        }
                        return "";
                    }
                }
                , {field: 'battery_param', title: '电池参数', sort: false, align: 'center',exportable:true,minWidth:'25',event:'singleclick'
                    ,templet:function(d){
                        if (d.typeName == '组件') {
                            if (d.battery_param == '1') {
                                return "N型";
                            } else if (d.battery_param == '2') {
                                return "P型";
                            }
                        }
                        return "";
                    },expFormat:function(d){
                        if (d.typeName == '组件') {
                            if (d.battery_param == '1') {
                                return "N型";
                            } else if (d.battery_param == '2') {
                                return "P型";
                            }
                        }
                        return "";
                    }
                }
                , {field: 'status', title: '状态', sort: false, align: 'center',minWidth:'50',exportable:true,event:'singleclick',templet:function(d){
                    if(d.status=='1'){
                        return "使用中";
                    }else if(d.status=='3'){
                        return "已禁用";
                    }
                        return "";
                    }
                    , expFormat: function (d) {
                            if(d.status=='1'){
                                return "使用中";
                            }else if(d.status=='3'){
                                return "已禁用";
                            }
                            return "";
                        }
                    }
            ]]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '物料信息库',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 5000,                    //当pageData为false生效
                fileName: '物料信息库',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            switch(obj.event){
                case 'import':
                    layer.open({
                        type: 2,
                        title:"导入物料",
                        resize:false,
                        area: ['40%', '43%'],
                        content: '${ctx}/material/importMaterialForm'
                    });
                    break;
                case 'addMaterial':
                    layer.open({
                        type: 2,
                        title:"新增",
                        resize:false,
                        area: ['50%', '65%'],
                        content: '${ctx}/material/addMateriaForm'
                    });
                    break;
                case 'deleteMaterial':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请选择一条数据",{icon:5,anim:6});
                        return;
                    }
                  /*  var status = data[0].status;
                    if(status != 1){
                        layer.msg("该物料不可删除",{icon:5,anim:6});
                        return;
                    }*/
                    var id = data[0].id;

                    layer.confirm('确认删除该物料',{
                        btn: ['确定', '取消'],
                        success: function (layero, index) {
                            this.enterEsc = function (event) {
                                if (event.keyCode == 13 ) {
                                    //确认  执行的代码
                                    deleteMaterial(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 () {
                            //确认执行的代码
                            deleteMaterial(id);
                        },
                        no: function () {

                        }
                    });

                    break;
            };
        });

        //监听行单击事件
        table.on('tool(test)', function(obj){
            if(obj.event=='singleclick'){
                var data = obj.data;
                var oldData = table.cache["test"];
                var checkCell=obj.tr.find(".layui-form-checkbox");
                var CHECKED;
                if(obj.tr.hasClass("xuanzhong-special")){
                    CHECKED = false;
                    obj.tr.removeAttr("style");
                    obj.tr.removeClass("xuanzhong-special");
                    obj.tr.removeClass('layui-table-click');
                    checkCell.removeClass('layui-form-checked');
                }else{
                    CHECKED=true
                    $(obj.tr).css('background-color','#CCEAEA');
                    $(obj.tr).addClass("xuanzhong-special");
                    obj.tr.addClass('layui-table-click');
                    checkCell.addClass('layui-form-checked');
                }
                $.each(oldData,function(i,val){
                    if(val.id == data.id){
                        oldData[i].LAY_CHECKED=CHECKED
                    }
                })
            }



        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field,
                page:{
                    curr:1
                }
            });
        });

        $('input').on('keydown', function(event){
            if(event.keyCode == 13){
                //执行相关操作
                $("#searchBtn").click();
            }
        });

        //重置
        form.on('submit(resetButton)', function(data){
            $("#searchForm")[0].reset();
            layui.form.render();

        });

    });

    function synchronize(){

        layer.open({
            type: 2,
            title:"同步物料价格",
            resize:false,
            area: ['20%', '20%'],
            content: '${ctx}/material/synchronizeProject'
        });

       /* setTimeout(function() {
            layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
            });
        }, 5000);
        $.ajax({
            type: "POST",
            url: "${ctx}/material/synchronize",
            success: function (result) {

            },
            error:{
            }
        })*/
    }

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

                    layer.msg('操作失败!'+result.errMsg, {icon: 2});
                }
            },
            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>

