<template>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <div id="itemTitle"></div>
                </section>
                <!-- .content -->
                <section>
                    <div id="table"></div>
                </section>
                <div id="toolbar">
                    <div class="col-sm-12">
                        <label for="poolType" class="col-sm-2 control-label">奖池类型:</label>
                        <div class="col-sm-2">
                            <select name="poolType" id="searchPoolType" data-btn-class="btn-warning" class="form-control">
                                <option value="1">密藏奖池</option>
                                <option value="2">幸运值奖池</option>
                                <option value="3">森林奖池</option>
                                <option value="4">合成奖池</option>
                            </select>
                        </div>
                        <label for="poolGroup" class="col-sm-2 control-label">奖池分组:</label>
                        <div class="col-sm-2">
                            <select name="poolGroup" id="searchPoolGroup" data-btn-class="btn-warning" class="form-control">
                                <option value="1">基础奖池</option>
                                <option value="2">高级奖池</option>
                            </select>
                        </div>
                        <label for="poolLevel" class="col-sm-2 control-label">奖池等级:</label>
                        <div class="col-sm-2">
                            <select name="poolLevel" id="searchPoolLevel" data-btn-class="btn-warning" class="form-control">
                                <option value="1">基础奖池</option>
                                <option value="2">中级奖池</option>
                                <option value="3">高级奖池</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <button id="btnAdd" class="btn btn-default" @click="btnAdd">
                            <i class="glyphicon glyphicon-plus-sign"></i>新增
                        </button>
                        <button id="btnSearch" class="btn btn-default" @click="btnSearch">
                            <i class="glyphicon glyphicon-search"></i>查询
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <div class="modal fade" id="editModal" 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">&times;</span>
                    </button>
                    <h4 class="modal-title" id="modalLabel">关联奖品</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <input type="hidden" name="id" id="id" />
                        <div class="form-group">
                            <label for="poolType" class="col-sm-3 control-label">奖池类型：</label>
                            <div class="col-sm-9">
                                <select name="poolType" id="poolType" class="form-control">
                                    <option value="1">密藏奖池</option>
                                    <option value="2">幸运值奖池</option>
                                    <option value="3">森林奖池</option>
                                    <option value="4">合成奖池</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="poolGroup" class="col-sm-3 control-label">奖池分组：</label>
                            <div class="col-sm-9">
                                <select name="poolGroup" id="poolGroup" class="form-control">
                                    <option value="1">基础奖池</option>
                                    <option value="2">高级奖池</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="poolLevel" class="col-sm-3 control-label">奖池等级：</label>
                            <div class="col-sm-9">
                                <select name="poolLevel" id="poolLevel" class="form-control">
                                    <option value="1">基础奖池</option>
                                    <option value="2">中级奖池</option>
                                    <option value="3">高级奖池</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="rewardId" class="col-sm-3 control-label">关联奖品：</label>
                            <div class="col-sm-9">
                                <select name="rewardId" id="rewardId" class="form-control">
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="poolNum" class="col-sm-3 control-label">个数：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="poolNum">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="ratio" class="col-sm-3 control-label">概率：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="ratio">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="showRatio" class="col-sm-3 control-label">展示概率：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="showRatio">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="itemIndex" class="col-sm-3 control-label">显示位置：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="itemIndex">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="itemOrder" class="col-sm-3 control-label">排序：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="itemOrder">
                            </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';
import { apiResult } from '@/utils/maintainer';
import { getRewardList } from '@/api/treasure/reward';
import { savePoolItem }  from '@/api/treasure/poolItem';

export default {
    name: "SeizeTreasurePoolItemView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initTable();
        });
    },
    mounted() {
        this.optEdit();
    },
    methods: {
        initTable() {
            $('#table').bootstrapTable('destroy');
            $('#table').bootstrapTable({
                columns: [
                    {
                        field: 'poolType',
                        title: '奖池类型',
                        align: 'center',
                        width: '5%',
                        formatter: function (val, row, index) {
                            var value = '';
                            if (val == '1') {
                                value = '密藏奖池';
                            } else if (val == '2') {
                                value = '幸运值奖池';
                            } else if (val == '3') {
                                value = '森林奖池';
                            } else if (val == '4') {
                                value = '合成奖池';
                            }
                            return value;
                        }
                    },
                    {
                        field: 'poolGroup',
                        title: '奖池分组',
                        align: 'center',
                        width: '5%',
                        formatter: function (val, row, index) {
                            var value = '';
                            if (val == '1') {
                                value = '基础奖池';
                            } else if (val == '2') {
                                value = '高级奖池';
                            }
                            return value;
                        }
                    },
                    {
                        field: 'poolLevel',
                        title: '奖池等级',
                        align: 'center',
                        width: '5%',
                        formatter: function (val, row, index) {
                            var value = '';
                            if (val == '1') {
                                value = '基础奖池';
                            } else if (val == '2') {
                                value = '中级奖池';
                            } else if (val == '3') {
                                value = '高级奖池';
                            }
                            return value;
                        }
                    },
                    { field: 'rewardName', title: '奖品名称', align: 'center', width: '5%' },
                    { field: 'rewardValue', title: '平台价值', align: 'center', width: '5%' },
                    { field: 'rewardShowValue', title: '平台展示价值', align: 'center', width: '5%' },
                    { field: 'poolNum', title: '个数', align: 'center', width: '5%' },
                    {
                        field: 'totalRewardValue',
                        title: '总价值',
                        align: 'center',
                        width: '5%',
                        formatter: function (val, row, index) {
                            return row.rewardValue * row.poolNum;
                        }
                    },
                    {
                        field: 'ratio',
                        title: '概率',
                        align: 'center',
                        width: '5%',
                        formatter: function (val, row, index) {
                            return val;
                        }
                    },
                    {
                        field: 'showRatio',
                        title: '展示概率',
                        align: 'center',
                        width: '5%',
                        formatter: function (val, row, index) {
                            return val;
                        }
                    },
                    { field: 'itemIndex', title: '显示位置', align: 'center', width: '5%' },
                    { field: 'itemOrder', title: '排序', align: 'center', width: '5%' },
                    {
                        field: 'handle',
                        title: '操作',
                        align: 'center',
                        width: '5%',
                        valign: 'middle',
                        formatter: function (val, row, index) {
                            return '<button class="btn btn-sm btn-success opt-edit" data-index="' + index + '">编辑</button>';
                        }
                    }
                ],
                cache: false,
                striped: true,
                showRefresh: false,
                pageSize: 20,
                pagination: true,
                pageList: [20, 50, 100],
                search: false,
                sidePagination: "server", 
                queryParamsType: "undefined",
                queryParams: function queryParams(params) {
                    var param = {
                        page: params.pageNumber,
                        pageSize: params.pageSize,
                        poolType: $('#searchPoolType').val(),
                        poolGroup: $('#searchPoolGroup').val(),
                        poolLevel: $('#searchPoolLevel').val()
                    };
                    return param;
                },
                toolbar: '#toolbar',
                url: '/admin/seize-treasure/pool/item/page',
                onLoadSuccess: function () { 
                    console.log("load success");
                },
                onLoadError: function () {
                    console.log("load fail");
                }
            });
        },
        btnSearch() {
            TableHelper.doRefresh('#table');
        },
        showReward(rewardId) {
            getRewardList().then(res => {
                const data = res.data;
                if (data && data.length > 0) {
                    let $select = $('#rewardId');
                    for (let i = 0, len = data.length; i < len; i++) {
                        let reward = data[i];
                        let $option = $('<option/>');
                        $option.attr('value', reward.id);
                        $option.data('name', reward.rewardName);
                        $option.html(reward.rewardName);
                        if (rewardId && rewardId == reward.id) {
                            $option.attr('selected', true);
                        }
                        $select.append($option);
                    }
                }
            });
        },
        btnAdd() {
            $("#id").val('');
            $("#rewardId").val('');
            $("#poolType").val('');
            $('#poolGroup').val('');
            $('#poolLevel').val('');
            $("#poolNum").val('');
            $("#ratio").val('');
            $("#showRatio").val('');
            $("#itemIndex").val('');
            $("#itemOrder").val('');
            this.showReward();
            $("#editModal").modal('show');
        },
        optEdit() {
            $('#table').on('click', '.opt-edit', function () {
                const currentData = $('#table').bootstrapTable('getData')[$(this).data('index')];
                let id = currentData.id;
                let rewardId = currentData.rewardId;
                let poolType = currentData.poolType;
                let poolGroup = currentData.poolGroup;
                let poolLevel = currentData.poolLevel;
                let poolNum = currentData.poolNum;
                let ratio = currentData.ratio;
                let showRatio = currentData.showRatio;
                let itemIndex = currentData.itemIndex;
                let itemOrder = currentData.itemOrder;
                $("#id").val(id);
                $("#rewardId").val(rewardId);
                $("#poolType").val(poolType);
                $('#poolGroup').val(poolGroup);
                $('#poolLevel').val(poolLevel);
                $("#poolNum").val(poolNum);
                $("#ratio").val(ratio);
                $("#showRatio").val(showRatio);
                $("#itemIndex").val(itemIndex);
                $("#itemOrder").val(itemOrder);
                this.showReward(rewardId);
                $("#editModal").modal('show');
            });
        },
        save() {
            let data = {
                rewardId: $('#rewardId').val(),
                poolType: $('#poolType').val(),
                poolGroup: $('#poolGroup').val(),
                poolLevel: $('#poolLevel').val(),
                poolNum: $('#poolNum').val(),
                ratio: $('#ratio').val(),
                showRatio: $('#showRatio').val(),
                itemIndex: $('#itemIndex').val(),
                itemOrder: $('#itemOrder').val()
            };
            let id = $('#id').val();
            if (id) {
                data.id = id;
            }
            savePoolItem(data).then(res => {
                apiResult(res);
                $("#editModal").modal('hide');
            });
        }
    },
};
</script>

<style scoped></style>