<template>
    <section class="content">
        <div class="box box-primary">
            <section class="content-header">
                <h1 id="itemTitle"></h1>
            </section>
            <section class="content">
                <div id="table"></div>
                <div id="userMessage">
                    <div class="avatar"><img src="" alt=""></div>
                    <div class="nick"></div>
                    <div class="erbanNo"></div>
                </div>
                <div id="toolbar">
                    <input type="text" id="userErbanNo">
                    <button id="searchBtn" class="btn btn-default">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                    <!--<button id="addBtn" class="btn btn-default">
                    <i class="glyphicon glyphicon-plus"></i>增加
                </button>-->
                    <!--<select name="sort" id="sort">-->
                    <!--<option value="1">用户车库座驾信息</option>-->
                    <!--<option value="2">用户座驾购买记录</option>-->
                    <!--</select>-->
                </div>
            </section>
        </div>
    </section>
    <!-- 为用户增加特定座驾弹窗 -->
    <div class="modal fade" id="addCarModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">x</span></button>
                    <h4 class="modal-title" id="modalLabel">为用户添加座驾</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="carAddForm">
                        <div class="form-group">
                            <label for="addErbanNo" class="col-sm-3 control-label">平台号</label>
                            <div class="col-sm-9">
                                <input type="text" name="erbanNo" id="addErbanNo" class="form-control validate[required]" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addCarId" class="col-sm-3 control-label">座驾类型</label>
                            <div class="col-sm-9">
                                <select name="carId" id="addCarId">
                                    <option value="">-- 请选择 --</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addDays" class="col-sm-3 control-label">体验天数</label>
                            <div class="col-sm-9">
                                <select name="days" id="addDays">
                                    <option value="">-- 请选择 --</option>
                                    <option value="1">1天</option>
                                    <option value="3">3天</option>
                                    <option value="5">5天</option>
                                    <option value="7">7天</option>
                                    <option value="15">15天</option>
                                    <option value="30">30天</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addDesc" class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <input type="text" name="desc" id="addDesc" class="form-control validate[required]" />
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="save">保存</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';

export default {
    name: "CarAdminView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                var carGoodsObj = {};
                getCarGoodsMsg();

                function initTable() {
                    $('#table').bootstrapTable('destroy');
                    $('#table').bootstrapTable({
                        columns: [
                            { field: 'uid', title: 'uid', align: 'center', width: '5%', valign: 'center' },
                            //                    {field: 'users.erbanNo', title:'平台号', align: 'center', width: '10%',valign:'center'},
                            {
                                field: 'carId', title: '拥有座驾', align: 'center', valign: 'center', formatter: function (val, row, index) {
                                    if (val) {
                                        return carGoodsObj[val];
                                    }
                                }
                            },
                            {
                                field: 'status', title: '座驾状态', align: 'center', width: '10%', valign: 'center', formatter: function (val, row, index) {
                                    switch (val) {
                                        case 1:
                                            return '下架';
                                        case 2:
                                            return '过期';
                                        case 3:
                                            return '有效';
                                    }
                                }
                            },
                            {
                                field: 'buyTime', title: '购买时间', align: 'center', valign: 'center', formatter: function (val, row, index) {
                                    if (val) {
                                        var date = new Date(val);
                                        return date.format("yyyy-MM-dd hh:mm:ss");
                                    } else {
                                        return '-';
                                    }
                                }
                            },
                            {
                                field: 'expireTime', title: '有效时间', align: 'center', valign: 'center', formatter: function (val, row, index) {
                                    if (val) {
                                        var date = new Date(val);
                                        return date.format("yyyy-MM-dd hh:mm:ss");
                                    } else {
                                        return '-';
                                    }
                                }
                            }
                        ],
                        cache: false,
                        striped: true,
                        showRefresh: false,
                        pageSize: 20,
                        pagination: true,
                        pageList: [1, 20, 50, 100, 200],
                        search: false,
                        sidePagination: "server", //表示服务端请求
                        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                        //设置为limit可以获取limit, offset, search, sort, order
                        queryParamsType: "undefined",
                        queryParams: function queryParams(params) {   //设置查询参数
                            var param = {
                                page: params.pageNumber,
                                pageSize: params.pageSize,
                                erbanNo: $('#userErbanNo').val()
                            };
                            return param;
                        },
                        toolbar: '#toolbar',
                        url: '/admin/car/getUserCarPort',
                        onLoadSuccess: function (data) {  //加载成功时执行
                            if (data.users) {
                                var $user = $('#userMessage');
                                $user.find('.avatar img').attr('src', data.users.avatar);
                                $user.find('.nick').html('昵称：' + data.users.nick);
                                $user.find('.erbanNo').html('平台号：' + data.users.erbanNo);
                            }
                            console.log("load success");
                        },
                        onLoadError: function () {  //加载失败时执行
                            console.log("load fail");
                        }
                    })
                }


                $('#addBtn').on('click', function () {
                    clearForm();
                    $('#addCarModal').modal('show');
                });

                $('#save').on('click', function () {
                    if (!($('#addCarId').val() || $('#addDays'))) {
                        $("#tipMsg").text("请选择天数或座驾");
                        $("#tipModal").modal('show');
                        return;
                    }
                    $.ajax({
                        type: 'post',
                        url: "/admin/car/experByOfficial",
                        data: $('#carAddForm').serialize(),
                        dataType: 'json',
                        success: function (res) {
                            if (res.code == 100) {
                                $('#addCarModal').modal('hide');
                                $("#tipMsg").text("添加成功");
                                $("#tipModal").modal('show');
                                TableHelper.doRefresh("#table");
                            } else {
                                $("#tipMsg").text("保存失败，错误码：" + res.code);
                                $("#tipModal").modal('show');
                            }
                        }
                    })

                });

                $('#searchBtn').on('click', function () {
                    //            $('#table').bootstrapTable('destroy');
                    //            TableHelper.doRefresh('#table');
                    initTable();
                });
                $('#sort').on('change', function () {
                    var val = parseInt($(this).val());
                    if (val == 1) {
                        $('#table').show();
                        $('#table2').hide();
                        TableHelper.doRefresh('#table');
                    } else if (val == 2) {
                        $('#table').hide();
                        $('#table2').show();
                        TableHelper.doRefresh('#table2');
                    }
                });

                function getCarGoodsMsg() {
                    $.get('/admin/car/goods', { page: 1, pageSize: 100, enable: -1 }, function (res) {
                        if (res.rows.length > 0) {
                            renderSelect(res.rows);
                            initTable();
                        }
                    })
                }

                function isShow($ele) {
                    if ($ele.attr('display') == 'block') {
                        return true;
                    }
                    return false;
                }
                function renderSelect(data) {
                    var $select = $('#addCarId');
                    for (var i = 0; i < data.length; i++) {
                        var $option = $('<option value="' + data[i].id + '" />');
                        $option.html(data[i].name);
                        $select.append($option);

                        carGoodsObj[data[i].id] = data[i].name;
                    }
                }
                function clearForm() {
                    var $form = $('#addCarModal');
                    $form.find('input').val('');
                    $form.find('select').val('');
                }
            })
        }
    },

};
</script>

<style scoped>
#userMessage {
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

#userMessage .avatar {
    width: 50px;
    height: 50px;
}

#userMessage .avatar img {
    width: 100%;
    height: 100%;
}

#userMessage>div {
    margin-right: 30px;
}</style>