$(function () {
    $("#jqGrid").jqGrid({
        url: '../ad/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '所属商户', name: 'merchName', index: 'merchName', width: 100, align: 'center'},
            {label: '所属门店', name: 'storeName', index: 'storeName', width: 80, align: 'center'},
            {label: '广告位置', name: 'adPositionName', index: 'ad_Position_id', width: 80},
            {label: '广告名称', name: 'name', index: 'name', width: 80},
            {label: '链接', name: 'link', index: 'link', width: 80},
            {
                label: '图片', name: 'imageUrl', index: 'image_url', width: 80, formatter: function (value) {
                return transImg(value);
            }
            },
            {label: '内容', name: 'content', index: 'content', width: 80},
            {
                label: '结束时间', name: 'endTime', index: 'end_time', width: 80, formatter: function (value) {
                return transDate(value);
            }
            },
            {label: '排序', name: 'sortOrder', index: 'sortOrder', width: 80},
            {label: '状态', name: 'enabled', index: 'enabled', width: 80,
                formatter: function (value) {
                    return value === 0 ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }
            },
            {label: '操作', width: 90, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    if(hasPermission('ad:saveCopyAd')) {
                        return "&nbsp;&nbsp;&nbsp;&nbsp;<button class='btn btn-primary' " +
                            "onclick='vm.copyAd(" + row.id + ")'>复制广告</button>";
                    }else{
                        return '-';
                    }
                }
            }],
        viewrecords: true,
        height: 550,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        ad: {enabled: 1, imageUrl: '', mediaType: 0},
        ruleValidate: {
            name: [
                {required: true, message: '广告名称不能为空', trigger: 'blur'}
            ],
            imageUrl: [
                {required: true, message: '图片不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        adPositions: [],
        storeList: [],
        merchList: [],
        copyAdDto:{
            adId: '',
            storeList: ''
        },
        showCopyList: true,
        showViewList: false,
    },
    methods: {
        copyAd: function(id){
            vm.copyAdDto.adId = id;
            vm.copyAdDto.storeList = "";
            vm.showCopyList = false;
            vm.showList = true;
            vm.showViewList = true;
            vm.title = "复制广告";
        },
        handleSubmitCopyAd: function () {
            if(vm.copyAdDto.adId == ""){
                alert('广告id不能为空');
                return;
            }
            if(vm.copyAdDto.storeList == ""){
                alert('门店id批量上传数据没有上传或未上传成功');
                return;
            }
            var url = "../ad/saveCopyAd";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.copyAdDto),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.showCopyList = true;
                            vm.showList = true;
                            vm.showViewList = false;
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        uploadExcelSuccess: function (data) {
            if(data.code==0){
                alert(data.msg, function (index) {
                    vm.copyAdDto.storeList = data.storeIds;
                });
            }else{
                alert(data.msg);
            }
        },
        uploadExcelError: function (data) {
            console.log(data);
            alert('上传出现异常，请重试！');
        },
        uploadExcelFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 xls 或 xlsx 格式的文件。'
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.showCopyList = true;
            vm.showViewList = true;
            vm.title = "新增";
            vm.ad = {enabled: 1, imageUrl: '', mediaType: 0};
            vm.adPosition = [];
            this.getAdPositions();
            vm.storeList = [];
            vm.merchList = [];
            vm.getMerchList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showCopyList = true;
            vm.showViewList = true;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getMerchList();
            vm.getStoresByMerch();
            this.getAdPositions();
        },
        saveOrUpdate: function (event) {
            var url = vm.ad.id == null ? "../ad/save" : "../ad/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.ad),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../ad/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../ad/info/" + id, function (r) {
                vm.ad = r.ad;
            });
        },
        getStoresByMerch: function (opt) {
            var value = opt.value;
            $.get("../store/getStoresByMerch?merchSn=" + value, function (r) {
                vm.storeList = r.list;
            });
        },
        getMerchList: function() {
            $.get("../merch/queryAll", function (r) {
                vm.merchList = r.list;
            });
        },
        reload: function (event) {
            vm.showViewList = false;
            vm.showList = true;
            vm.showCopyList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSuccess: function (res, file) {
            vm.ad.imageUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 100K。'
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        eyeImage: function () {
            var url = vm.ad.imageUrl;
            eyeImage(url);
        },
        /**
         * 获取会员级别
         */
        getAdPositions: function () {
            $.get("../adposition/queryAll", function (r) {
                vm.adPositions = r.list;
            });
        }
    }
});
