material.page.js 4.64 KB
var $ = require('yoho-jquery');
var brandTmp = require('hbs/material/brand.hbs');
var tableTmp = require('hbs/material/tbl.hbs');
var $mask = $('.mask');
var $brand = $('#brand_container');
var query = {
    page: 1,
    maxSortId: '',
    middleSortId: '',
    smallSortId: ''
};
var brandList = {
    list1: [],
    list2: [],
    list3: [],
    list4: []
};
var option0 = '<option value="">--请选择--</option>';

function brandItemEventBind() {
    $('#brand_container .item').off().on('click', function() {
        var id = $(this).data('id');
        var text = $(this).text();

        query.brandId = id;
        $('#brand').val(text);
        $mask.hide();
        $brand.hide();
    });
}

/* 获取品牌数据*/
function getBrandList(_index, yhChannel) {
    $.get('/3party/material/newBrandList?yh_channel=' + yhChannel, function(data) {
        var list = [];

        list = Object.keys(data.data.all_list).map(item => {
            return { name: item, content: data.data.all_list[item] };
        });
        list['focus' + _index] = true;
        brandList['list' + _index] = list;
        $('#brand_container').html(brandTmp({ list }));
        brandItemEventBind();
    });
}

function getCategoryOpt(opts) {
    var html = option0;

    $.each(opts, function(k, c) {
        html += '<option value="' + c.sort_id + '">' + c.sort_name + '</option>';
    });

    return html;
}

function findSub(data, id) {
    var sub = [];

    $.each(data, function(k, c) {
        if (c.sort_id === id) {
            sub = c.sub;
        }
    });

    return sub;
}

getBrandList(1, 1);

/* 获取品类数据*/
$.get('/3party/material/getCategory', function(res) {
    var $category1 = $('#category-1'),
        $category2 = $('#category-2'),
        $category3 = $('#category-3'),
        sub = [],
        categoryData = [];

    if (res && res.code === 200) {
        categoryData = res.data.sort;

        $category1.html(getCategoryOpt(categoryData));
        query.maxSortId = $category1.find('option').eq(0).attr('value');

        $category1.on('change', function() {
            var id = $(this).val();
            var html = option0;

            query.maxSortId = '';
            query.middleSortId = '';
            query.smallSortId = '';

            if ($(this).find('option:selected').index() !== 0) {
                sub = findSub(categoryData, id);
                html = getCategoryOpt(sub);
                query.maxSortId = id;
            }

            $category2.html(html);
            $category3.html(option0);
        });

        $category2.on('change', function() {
            var id = $(this).val();
            var third = [];

            query.smallSortId = '';
            query.middleSortId = '';

            if ($(this).find('option:selected').index() !== 0) {
                third = findSub(sub, id);
                $category3.html(getCategoryOpt(third));
                query.middleSortId = id;
            }
        });

        $category3.on('change', function() {
            query.smallSortId = $(this).val();
        });
    }
});

/* 品牌点击*/
$('#brand').on('click', function() {
    if ($brand.css('display') === 'none') {
        $brand.css('display', 'block');
        $mask.show();
    } else {
        $brand.hide();
        $mask.hide();
    }
});

$mask.click(function() {
    $mask.hide();
    $brand.hide();
});

$(document).on('click', '#brand_container .nav span', function() {
    var yhChannel = $(this).attr('yh_channel');
    var index = $(this).attr('_index');

    $('#brand_container .nav span').removeClass('focus');
    $(this).addClass('focus');

    if (brandList['list' + index].length > 0) {
        $('#brand_container').html(brandTmp({list: brandList['list' + index]}));
        brandItemEventBind();
    } else {
        getBrandList(index, yhChannel);
    }
});

$('.start-time').on('change', function() {
    query.shelveTimeBegin = $(this).val() + ' 00:00:00';
});

$('.end-time').on('change', function() {
    query.shelveTimeEnd = $(this).val() + ' 23:59:59';
});

$('.search-btn').on('click', function() {
    $.ajax({
        url: '/3party/material/getList',
        data: query,
        type: 'get',
        success: function(data) {
            if (data && data.code === 200) {
                $('.table-data').html(tableTmp({product_list: data.data.product_list}));
            }
        }
    });
});

$('.search-recommend-btn').on('click', function() {
    $.ajax({
        url: '/3party/material/getRecommendlist',
        type: 'get',
        success: function(data) {
            if (data && data.code === 200) {
                $('.table-data').html(tableTmp({product_list: data.data.product_list}));
            }
        }
    });
});