coupon.page.js 4.87 KB
/* eslint-disable eqeqeq */
require('admin/user.page.css');
require('bootpag/lib/jquery.bootpag.min');

const _ = require('lodash');

function bind_table_pagination() {
    const $ul = $('.coupon-list');
    const $up = $('.coupon-pagination');

    let uploadFn = function() {
        let id = $(this).data('id');
        let $form = $('#uploadForm_' + id)[0];

        let formData = new FormData($form);

        formData.append('id', $(this).data('id'));

        if ($form) {
            $.ajax({
                url: '/admin/api/coupon/batchAddNo',
                data: formData,
                method: 'POST',
                cache: false,
                processData: false,
                contentType: false,
                success: function(data) {
                    if (!data.result) {
                        alert(data.message);
                    } else {
                        alert('导入成功');// eslint-disable line
                    }
                }
            });
        }
    };

    const fetchRender = (pageNo, pageSize) => {
        $.ajax({
            url: '/admin/api/coupon/list',
            data: {
                pageNo,
                pageSize
            }
        })
            .then(result => {
                const list = result.data;
                const totalPage = result.totalPage;

                let html = '';

                _.each(list, item => {
                    if (item.status) {
                        item.statusText = '已上架';
                    } else {
                        item.statusText = '已下架';
                    }
                    if (item.type == 1) {
                        item.typeText = '通用码';
                    } else if (item.type == 2) {
                        item.typeText = '一人一码';
                    } else if (item.type == 3) {
                        item.typeText = '不用码';
                    }
                    html += `
                    <tr class="even pointer">
                        <td class="">${item.id}</td>1
                        <td class="">${item.couponName}</td>
                        <td class="">${item.shopName}</td>
                        <td class=""><img src="${item.shopLogoUrl}" width="100" height="50"/></td>
                        <td class="">${item.statusText}</td>
                        <td class="">${item.typeText}</td>
                        <td class="">${item.sort}</td>
                        <td class="">${item.createTime}</td>
                        <td class="">
                            <form id="uploadForm_${item.id}" enctype="multipart/form-data">
                                    <input id="up_excel" name="up_excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" style="display: inline-block; width: 80%;" type="file">
                            </form>
                            <button type="button" class="btn btn-danger btn-import" style="display: inline-block;" data-id="${item.id}">导入券码</button>
                            <button class="btn btn-danger btn-export-no" data-id="${item.id}">导出券码
                            </button>
                            <a class="btn btn-danger btn-update" href="/admin/coupon/update?id=${item.id}">修改</a>
                            <a class="btn btn-danger btn-delete" data-id="${item.id}">删除</a>
                        </td>
                    </tr>`;
                });

                $ul.html(html);

                if (pageNo === 1) {
                    $up.bootpag({
                        total: totalPage,
                        page: 1,
                        maxVisible: 10,
                    }).on('page', function(event, num) {
                        fetchRender(num, 20);
                    });

                }
                $('.btn-export-no').on('click', function() {

                    let id = $(this).attr('data-id');

                    window.location = '/admin/api/coupon/downloadNo?id=' + id;
                });
                $('.btn-import').on('click', uploadFn);

                $('.btn-delete').click(function() {
                    let id = $(this).attr('data-id');

                    console.log(id);
                    $.ajax({
                        url: '/admin/api/coupon/delete?id=' + id,
                        method: 'get',
                        cache: false,
                        processData: false,
                        contentType: false,
                        success: function(data) {
                            if (!data.result) {
                                alert(data.message);
                            } else {
                                window.location.reload();
                            }
                        }
                    });
                });
            });
    };


    fetchRender(1, 20);
}

(function() {
    bind_table_pagination();
}());