$(function () {
    let sendType = getQueryString("sendType");
    let menuName = getQueryString("menuName");
    let url = '../coupon/list';
    if (sendType) {
        url += '?sendType=' + sendType;
        vm.sendType = sendType;
        vm.menuName = menuName;
    }

    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '优惠券名称', name: 'name', index: 'name', width: 120},
            // {
            //     label: '优惠券类型', name: 'couponType', index: 'couponType', width: 80, formatter: function (value) {
            //     if (value == 0) {
            //         return '通用优惠券';
            //     } else if (value == 1) {
            //         return '门店优惠券';
            //     } else if (value == 2) {
            //         return '产品优惠券';
            //     }
            //     return '-';
            // }
            // },
            {
                label: '状态', name: 'status', index: 'status', width: 50, formatter: function (value) {
                if (value == 0) {
                    return '失效';
                } else if (value == 1) {
                    return '正常';
                }
                return '-';
            }
            },
            {label: '订单最小金额', name: 'minAmount', index: 'minAmount', width: 80},
            {label: '订单最大金额', name: 'maxAmount', index: 'maxAmount', width: 80},
            {label: '金额', name: 'typeMoney', index: 'type_money', width: 80},
            // {label: '最小商品金额', name: 'minGoodsAmount', index: 'min_goods_amount', width: 80},
            {label: '有效天数', name: 'invalidDays', index: 'invalidDays', width: 80},
            // {
            //     label: '操作', width: 70, align: 'center', sortable: false, formatter: function (value, col, row) {
            //     if (row.couponType == 1) {
            //         return '<button class="btn btn-outline btn-info ivu-btn-small" onclick="vm.lookCouponStoreList(' + row.id + ',\'' + row.name + '\')"><i class="fa fa-info-circle"></i>&nbsp;门店列表</button>&nbsp;';
            //     } else if (row.couponType == 2) {
            //         return '<button class="btn btn-outline btn-info ivu-btn-small" onclick="vm.lookCouponProductList(' + row.id + ',\'' + row.name + '\')"><i class="fa fa-info-circle"></i>&nbsp;产品列表</button>&nbsp;';
            //     }
            //     return '';
            // }
            // }
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showDiv: 1,// 1List 2edit 3publish 4store 5product
        title: null,
        menuName: null,
        sendType: 0,
        coupon: {sendType: 0, status: 1, couponType: 0, name: '', minAmount: 0, maxAmount: 0, minGoodsAmount: 0},
        ruleValidate: {
            name: [
                {required: true, message: '优惠券名称不能为空', trigger: 'blur'},
            ],
        },
        q: {
            name: ''
        },
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showDiv = 2;
            vm.title = "新增";
            vm.coupon = {
                sendType: vm.sendType,
                status: 1,
                couponType: 0,
                name: '',
                minAmount: 0,
                maxAmount: 0,
                minGoodsAmount: 0
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showDiv = 2;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.coupon.id == null ? "../coupon/save" : "../coupon/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.coupon),
                contentType: "application/json",
                type: 'POST',
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../coupon/delete",
                    params: JSON.stringify(ids),
                    contentType: "application/json",
                    type: 'POST',
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../coupon/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.coupon = r.coupon;
                }
            });
        },
        reload: function (event) {
            vm.showDiv = 1;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
    }
});