/**
 * 交付-标准预测-查看
 *
 * Created by jt on 2019/9/17.
 */
'use strict';
var page = {
    colList: [],
    initData: function () {
        var loadIndex = layer.msg('加载中...', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/order/basicPlan/rest/get/date/interval',
            data: {planId: planId},
            type: 'POST',
            success: function (res) {
                layer.close(loadIndex);
                page.initTable(res);
            },
            error: function (e) {
                console.log(e);
                layer.close(loadIndex);
                var respObj = JSON.parse(e.responseText);
                layer.alert(respObj.message || (typeof (respObj === 'string') ? respObj : "出现错误"), {icon: 2});
            }
        });
    },
    initTable: function (data) {
        // 异常数据数组
        var dataWarning = [];
        if (data) {
            var colList, colList2;
            colList = [
                {fixed: 'left', type: 'numbers'}
                , {field: 'productCode', width: 100, align: 'center', title: '物料编码', fixed: 'left'}
                , {field: 'productName', width: 150, align: 'center', title: '物料名称', fixed: 'left'}
                , {field: 'factoryCode', width: 100, align: 'center', title: '工厂编码', fixed: 'left'}
            ];
            if (Number(isSupplier) === 1) {
                $('#supplier_search_box').hide();
                $('#purchaseGroupCode_search_box').hide();
            } else {
                colList.push({field: 'isRisk', width: 100, align: 'center', title: '是否预警', fixed: 'left'});
                colList.push({field: 'supplierCode', width: 100, align: 'center', title: '供应商编码', fixed: 'left'});
                colList.push({field: 'supplierName', width: 150, align: 'center', title: '供应商名称', fixed: 'left'});
            }
            colList2 = [];
            $.each(data, function (index, item) {
                var colTitle = 'M' + (index + 1);
                //发布量
                colList.push({
                    field: this['dateTime'],
                   // title: '<span style="color:black;font-weight: bold;font-size: 14px">'+colTitle+'</span>' + '<span style="color: #0b69e3;font-weight: bold;font-size: 14px">发布量</span>/<span style="color:orangered;font-weight: bold;font-size: 14px">回复量</span>',
                    title: '<span style="color:black;font-weight: bold;font-size: 14px">'+colTitle+'</span>' + '<span style="color: #000000FF;font-weight: bold;font-size: 14px">发布量</span>',
                    width: 180,
                    align: 'center',
                    colspan: 1,
                    templet: function(d){
                        if( d[this['field']]===undefined){
                            return ''
                        }
                        var arr = d[this['field']].split('/');
                        var color = "";
                        if(arr[1] - arr[0] < 0 ){
                            color = "#FF4500FF";
                        }else{
                            color = "#000000FF";
                        }
                        return '<span style="color:' + color +  '">' + arr[0] + '</span>';
                    }
                });
                //回复量
                colList.push({
                    field: this['dateTime'],
                    title: '<span style="color:black;font-weight: bold;font-size: 14px">'+colTitle+'</span>' + '<span style="color:#000000FF;font-weight: bold;font-size: 14px">回复量</span>',
                    width: 180,
                    align: 'center',
                    colspan: 1,
                    templet: function(d){
                        if( d[this['field']]===undefined){
                            return ''
                        }
                        var arr = d[this['field']].split('/');
                        var color = "";
                        if(arr[1] - arr[0] < 0 ){
                            color = "#FF4500FF";
                        }else{
                            color = "#000000FF";
                        }
                        return  '<span style="color:' + color +  '">' + arr[1] + '</span>';
                    }
                });
                //测试
                // });
            });
            // if(isSupplier){
            //
            // }else{
            //     colList2 = [];
            //     $.each(data, function () {
            //         colList.push({
            //             field: this['dateTime'], title: this['dateTime'], align: 'center', colspan: 2
            //         });
            //         colList2.push({
            //             field: this['dateTime'], width: 80, align: 'center', title: '发布量'
            //         });
            //         colList2.push({
            //             field: this['dateTime'] + '_reply', width: 80, align: 'center', title: '回复量'
            //             ,templet: function (d) {
            //                 var dateSplit = this['field'].split('_');
            //                 if(dateSplit[0] && Number(d[this['field']] || 0) > Number(d[dateSplit[0]] || 0)){
            //                     dataWarning.push((Number(d['LAY_INDEX']) - 1) + '_1-' + this['key']);
            //                 }
            //                 return '<span style="">'+ d[this['field']] +'</span>'
            //             }
            //         });
            //     });
            // }

            page.colList = [colList, colList2];
        }
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#showDetailTable'
                , url: g.ctx + 'pc/order/basicPlan/rest/get/date/data?planId=' + planId
                , width: '100%'
                , even: true
                , height: gridheight - 30
                , limits: [10, 20, 40, 60]
                , limit: 20//每页默认显示的数量
                , cols: page.colList
                , page: true
                , method: 'POST'
                , where: {
                    q: JSON.stringify({
                        product: $("#productCode").val(),
                        supplier: $('#supplierCode').val(),
                        purchaseGroupCode: $('#purchaseGroupCode').val(),
                        isRisk: $('#isRisk').val(),
                        serialNumber: $('#serialNumber').val()
                    })
                },
                done: function (res, curr, count) {// 表格渲染完成之后的回调

                    if (res['data'] && res['data'].length) {
                        var trList = $("#table_div .layui-table-box .layui-table tr");
                        $.each(res['data'], function (index, item) {
                            $.each(page.colList[0], function () {
                                if (trList[this] && trList[this + '_reply'] && Number(trList[this]) !== Number(trList[this + '_reply'])) {
                                    $(trList[index]).addClass('my_highlight')
                                }
                            });
                        });
                    }
                    var layTable = $('[lay-id="showDetailTable"] .layui-table-main table.layui-table');
                    $.each(dataWarning, function () {
                        var split = this.split('_');
                        layTable.find('tr[data-index=' + split[0] + ']').find('[data-key="' + split[1] + '"]').addClass('data_warning');
                    });
                    dataWarning = [];
                }
            });
            // 监听自定义导出按钮点击事件
            /*$('#exportBtn').on('click', function () {
                alert(111);
                window.open(g.ctx + 'pc/order/basicPlan/rest/get/reply/export?planId=' + planId
                    +"&status=1&isRiskMaterial=" + isRiskMaterial + "&supplier=" + supplierCode+ "&purchaseGroup=" + purchaseGroup);
            });*/

            // 监听工具条
           /* table.on('toolbar(showDetailTable)', function(obj){
                alert(1111);
                if(obj.event === 'export'){
                    // 在这里添加导出数据的逻辑，可以调用后端接口导出数据
                    console.log('导出按钮被点击了');
                }
            });*/
        });
    }
};



$(function () {
    page.initData();

    $('#search_btn').on('click', function () {
        page.initTable();
    });
    $('#reset_btn').on('click', function () {
        $('#productCode').val('');
        $('#supplierCode').val('');
        $('#isRisk').val('');
        page.initTable();
    });
});

/**
 * 导出
 */
$('#export_btn').on('click', function () {
    var productCode = $('#productCode').val();
        //$('#productCode').combobox('getValue');
    var supplierCode = $('#supplierCode').val();
    var isRisk = $('#isRisk').val();
    var serialNumber = $('#serialNumber').val();
    window.open(g.ctx + 'pc/order/basicPlan/rest/downLoadRisk?product=' + productCode
        +"&supplier="+supplierCode+"&isRisk=" + isRisk + "&serialNumber=" + serialNumber);
});

