var el = {
    newPrice: '#newPrice'
};

$(function () {

    $('#tt').tabs({
        border: false,
        onSelect: function (title) {
            if (title === '最新报价') {
                loadNewPriceGrid();
            } else if (title === '本次报价过程') {
                loadQuoteProcess();
            } else if (title === '历史价格分析') {
                loadHistoryPrice();
            }
        }
    });

    $('.materialCodeList').combobox({
        onChange: function (newValue, oldValue) {
            var xunJiaId = $('#xunJiaId').val();
            changeByMaterialCode(newValue, xunJiaId);
            //window.location.href = g.ctx + 'baojia/biJia/assistant?materialCode=' + newValue + '&xunJiaId=' + $('#xunJiaId').val();

        }
    })


});

//最新价格记录报表
function loadNewPriceGrid() {
    $(el.newPrice).datagrid({
        loadMsg: '数据加载中......',
        pagination: false,
        url: g.ctx + 'pc/inquiry/supplier/quote/log/rest/list',
        idField: 'id',
        height: 400,
        rownumbers: true,
        queryParams: {
            page:1,
            rows:1000,
            inquiryId: $('#inquiryId').val(),
            materialCode: $('#materialCode').val()
        },
        columns: [
            [
                {
                    field: 'supplierCode',
                    title: '供应商编码',
                    width: 100
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 260
                },
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 150
                },
                {
                    field: 'materialName',
                    title: '物料名称',
                    width: 400
                },
                {
                    field: 'type',
                    title: '报价类型',
                    width: 80,
                    formatter: function (value) {
                        if (value === 1) {
                            return '报价';
                        } else if (value === 2) {
                            return '议价';
                        }
                    }
                },
                {
                    field: 'taxPrice',
                    title: '含税报价',
                    width: 90,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'noTaxPrice',
                    title: '未税报价',
                    width: 90,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'createTime',
                    title: '报价时间',
                    width: 140,
                    exportDateFormatter:'dateTime',
                    formatter: function (value) {
                        if (value) {
                            return new Date(value).format('yyyy-MM-dd hh:mm:ss');
                        }
                        return value;
                    }
                }

            ]
        ]
    });
}


function loadQuoteProcess() {
    var option = {
        tooltip: {
            trigger: 'axis',
            formatter: function (params) {

                return "供应商：" +
                    params[0].data.name +
                    "<br/>" +
                    params[0].seriesName +
                    "：" +
                    params[0].data.value;
                // +
                // "<br/>" +
                // "物料：" +
                // params[0].data.material;

            }
        },
        // legend: {
        //     data: ['单价'],
        // },
        calculable: true,
        xAxis: [
            {
                type: 'category',
                data: []
            }
        ],
        yAxis: [
            {
                type: 'value',
                axisLabel: {formatter: '{value}元'},
                splitNumber: 5
            }
        ],
        series: [
            // {
            //     name: '单价',
            //     type: 'bar',
            //     data: [],
            //     itemStyle: {
            //         normal: {
            //             color: '#0acf97'
            //         }
            //     },
            //
            // }
        ]
    };

    var dom = document.getElementById("quoteProcess");
    var myChart = echarts.init(dom);
    myChart.setOption(option, true);

    myChart.showLoading();
    $.ajax({
        url: g.ctx + 'pc/inquiry/supplier/quote/log/rest/chart',
        data: {
            inquiryId: $('#inquiryId').val(),
            materialCode: $('#materialCode').val()
        },
        success: function (data) {
            //list数据
            var list = data[0];
            //按供应商分组map数据
            var map = data[1];
            //供应商柱形颜色
            var supplierColors = [];
            var colors = [];
            //随机生成不重复供应商柱子颜色
            for (var key in map) {
                var supplierColor = {name: key, value: randomHexColor(colors)};
                supplierColors.push(supplierColor);
            }


            var dataSeries = [], dataX = [];

            $.each(list, function (j, value) {
                dataX.push(new Date(value.createTime).format('yyyy-MM-dd hh:mm:ss'));
                for (var i = 0; i < supplierColors.length; i++) {
                    if (value.supplierName === supplierColors[i].name) {
                        dataSeries.push(
                            {
                                name: value.supplierName,
                                value: value.taxPrice,
                                material: value.materialName,
                                itemStyle: {
                                    normal: {
                                        color: supplierColors[i].value
                                    }

                                }
                            });
                        break;
                    }
                }

            });
            myChart.hideLoading();
            myChart.setOption({
                xAxis: {
                    data: dataX
                },
                series: [{
                    name: '单价',
                    type: 'bar',
                    barWidth: 22,
                    data: dataSeries
                }]

            });
        },
        error: function (data) {
            myChart.hideLoading();
        }
    });

    $('#quoteProcessTb').datagrid({
        loadMsg: '数据加载中......',
        pagination: true,
        url: g.ctx + 'pc/inquiry/supplier/quote/log/rest/list',
        idField: 'id',
        height: 250,
        rownumbers: true,
        queryParams: {
            page:1,
            rows:1000,
            inquiryId: $('#inquiryId').val(),
            materialCode: $('#materialCode').val()
        },
        columns: [
            [
                {
                    field: 'id',
                    title: 'id',
                    hidden: true
                },
                {
                    field: 'supplierCode',
                    title: '供应商编码',
                    width: 100
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 260
                },
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 150
                },
                {
                    field: 'materialName',
                    title: '物料名称',
                    width: 400
                },
                {
                    field: 'type',
                    title: '报价类型',
                    width: 80,
                    formatter: function (value) {
                        if (value === 1) {
                            return '报价';
                        } else if (value === 2) {
                            return '议价';
                        }
                    }
                },
                {
                    field: 'taxPrice',
                    title: '含税报价',
                    width: 90,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'noTaxPrice',
                    title: '未税报价',
                    width: 90,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'createTime',
                    title: '报价时间',
                    width: 140,
                    exportDateFormatter:'dateTime',
                    formatter: function (value) {
                        if (value) {
                            return new Date(value).format('yyyy-MM-dd hh:mm:ss');
                        }
                        return value;
                    }
                }

            ]
        ]
    })
}

//历史价格分析 type: 1:全部时间 2：一年 3：三月
function loadHistoryPrice(type) {
    var option1 = {
        tooltip: {
            trigger: 'axis',
            formatter: function (params) {

                return "供应商：" +
                    params[0].data.name +
                    "<br/>" +
                    params[0].seriesName +
                    "：" +
                    params[0].data.value;
                // +
                // "<br/>" +
                // "物料：" +
                // params[0].data.material;

            }
        },
        calculable: true,
        xAxis: [
            {
                type: 'category',
                data: []
            }
        ],
        yAxis: [
            {
                type: 'value',
                axisLabel: {formatter: '{value}元'},
                splitNumber: 5
            }
        ],
        series: [
            // {
            //     name: '单价',
            //     type: 'line',
            //     data: [],
            //     barWidth: 22,
            //     itemStyle: {
            //         normal: {
            //             color: '#0acf97'
            //         }
            //     }
            // }
        ]
    };

    var dom = document.getElementById("historyPrice");
    var myChart1 = echarts.init(dom, 'light');
    myChart1.setOption(option1, true);

    myChart1.showLoading();
    $.ajax({
        url: g.ctx+"pc/inquiry/price/rest/chart",
        data: {
            materialCode: $('#materialCode').val(),
            type: type
        },
        success: function (result) {
            var dataX = [];
            var data = [];
            var dataSeries = [];
            result.forEach(function(tmp, value){
                dataX.push(new Date(tmp.createTime).format('yyyy-MM-dd'));
                data.push(tmp.taxPrice);
                dataSeries.push(
                    {
                        name: tmp.supplierName,
                        value: tmp.taxPrice,
                        material: tmp.materialName
                    });
            });
            // console.log(dataLend);
            myChart1.hideLoading();
            myChart1.setOption({
                xAxis: {
                    data: dataX
                },
                series: [{
                    name:'中标价',
                    data: dataSeries,
                    type: 'line',
                    barWidth: 22,
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值'},
                            {type: 'min', name: '最小值'}
                        ]
                    }
                }]

            });
        },
        error: function (data) {
            myChart1.hideLoading();
        }
    });
}


//根据物料改变页面数据
function changeByMaterialCode(materialCode, xunJiaId) {
    $('#materialCode').val(materialCode);
    loadNewPriceGrid();
    loadQuoteProcess();
    loadHistoryPrice();
}


//随机生成颜色
function randomHexColor(colors) {
    var hex = Math.floor(Math.random() * 16777216).toString(16); //生成ffffff以内16进制数
    while (hex.length < 6) { //while循环判断hex位数，少于6位前面加0凑够6位
        hex = '0' + hex;
    }
    var color = '#' + hex; //返回‘#'开头16进制颜色
    if (colors) {
        for (var i = 0; i < colors.length; i++) {
            if (color === colors[i]) {
                randomHexColor(colors);
            }
        }
    }
    return color;
}