let userInfo = window.localStorage && JSON.parse(localStorage.getItem('_userInfo'))


$(function () {

    $("#jqGrid").Grid({
        url: '../recommend/selectByType',

        dataType: "json",
        ajaxGridOptions: {
            contentType: "application/json"
        },
        mtype: "POST",
        serializeGridData: function (postData) {
            return JSON.stringify(postData)
        },
        postData: {
            token: localStorage.getItem('token'),
            type: vm.info.type,
            // pageNum:1,
            // pageSize:10
        },
        rownumWidth: 60,
        multiboxonly: true,
        jsonReader: {
            root: "list",
            page: "pageNum",
            total: "pages",
            records: "total"
        },
        colModel: [{
                label: 'ID',
                name: 'id',
                index: "id",
                hidden: true,
                key: true,
                width: 40,
                align: 'center',
            },
            {
                label: '封面',
                // name: 'listPicUrl',
                name: 'image',
                width: 75,
                align: 'center',
                formatter: function (value) {
                    if (value == '' || !value) {
                        value = '../statics/img/pic_bg@2x.png'
                    }
                    return transImg(value, 100, 'auto');
                }
            },


            {
                label: '标题',
                name: 'title',
                width: 100,
                align: 'center',
            },
            {
                label: '排序号',
                name: 'orderNum',
                width: 100,
                align: 'center',
            },
            {
                label: '链接',
                name: 'sceneUrl',
                width: 100,
                sortable: false,
                align: 'center',
            },
            {
                label: '编辑时间',
                name: 'createTime',
                align: 'center',
                width: 80,
                sortable: false,
                hidden: false,
                formatter: function (value) {
                    return transDate(value);
                }
            }

        ]
    });
});





var vm = new Vue({
    el: '#rrapp',
    data: {
        myScene: [{
            sceneNum: '',
            name: ''
        }],
        selectDisabled: false,
        q: {
            title: '',

        },
        showList: true,

        appShow: {
            name: '',
            isShow: '',
            listPicUrl: '',
            appListPicUrl: '',
            id: '',
            num: '',
            eqIndex: ''
        },
        title: null,
        user: {
            status: 1,
            deptName: '',
            roleIdList: [],
            brandIdList: [],
            brandList: []
        },
        info: {
            id: null,
            sceneUrl: '',
            title: '',
            image: '',
            orderNum: '',
            connectId: '',
            type: 11
        },
        ruleValidate: {

            title: [{
                required: true,
                message: '标题不能为空',
                trigger: 'blur'
            }],
            orderNum: [{
                required: true,
                type: 'number',
                message: '推荐排序不能为空',
                trigger: 'blur'
            }],
            connectId: [{
                required: true,
                type: 'number',
                message: '请选择场景链接',
                trigger: 'change'
            }],

            image: [{
                required: true,
                message: '请上传封面',
                trigger: 'change'
            }],
        },


    },
    watch: {

    },
    mounted() {
        this.getScene()
        window.addEventListener('message', function (e) {
            var data = e.data;
            if (data === 'close') {
                layer.closeAll()
            }

            if (data === 'closeReload') {
                layer.closeAll()
                vm.reload()
            }




            if (data.website) {
                // vm.brand.sceneUrl = data.website[0] || ''
                vm.brand.name = data.website[1] || ''
                layer.closeAll()
            }

            if (data.userId) {
                vm.brand.adminId = parseInt(data.userId) || ''
                vm.brand.adminName = data.nickname || ''
                layer.closeAll()
            }
        }, false);
    },
    methods: {

        getScene: function () {
            let params = {
                deptId: userInfo.deptId,
                token: localStorage.getItem('token'),
                page: 1,
                limit: 100000,
                // livestreamStatus: 1,
            }
            Ajax.request({
                // url: "../brand/getAllScene",
                url: "../brand/brandBindList",
                type: 'GET',
                params: params,
                contentType: "application/json",
                async: true,
                successCallback: function (r) {
                    vm.myScene = r.list
                }
            });
        },

        changTab(key) {
            this.q.title = '';
            this.info.type = key - 0
            this.$nextTick(() => {
                vm.showList = true;
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {
                        title: '',
                        type: this.info.type,
                    },
                    page: 1
                }).trigger("reloadGrid");
                vm.handleReset('formValidate');
            })

        },

        query: function () {
            vm.reload(1);
        },
        add: function () {
            vm.showList = false;
            vm.info.id = null
            vm.info.sceneUrl = ''
        },
        edit: function () {
            var id = getSelectedRow("#jqGrid");
            console.log(id)

            if (id == null) {
                return;
            }
            let params = {
                id: id,
                type: vm.info.type
            }
            vm.info.id = id
            vm.showList = false;
            Ajax.request({
                url: "../recommend/selectById",
                params: JSON.stringify(params),
                contentType: "application/json",
                type: 'POST',
                successCallback: function (r) {
                    vm.info = r.data
                }
            });

        },
        limitNum() {
            this.info.orderNum = this.info.orderNum.replace(/[^0-9]/g, '')
        },
        del: function () {
            var id = getSelectedRow("#jqGrid");
            console.log(id)
            if (id == null) {
                return;
            }
            var params = {
                id: id,
                type: vm.info.type

            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../recommend/delete",
                    params: JSON.stringify(params),
                    contentType: "application/json",
                    type: 'POST',
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.info.id == null ? "../recommend/save" : "../recommend/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.info),
                contentType: "application/json",
                type: 'POST',
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload(1);
                    });
                }
            });
        },
        currUrl() {
            let item = this.myScene.filter(res => {
                return res.id == this.info.connectId
            })
            this.info.sceneUrl = item[0].sceneUrl

        },
        handleSuccessListPicUrl: function (res, file) {
            vm.info.image = file.response.url + window.fixImg + 'm_fixed,w_400,h_400';
            this.$refs.formValidate.validateField('image')

        },
        handleFormatError: function (file) {
            this.loading = false
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确。'
            });
        },
        handleMaxSize: function (file) {
            this.loading = false
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大。'
            });
        },

        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page') || event;
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    title: vm.q.title,
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },



        handleSubmit: function (name) {

            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },

        handleReset: function (name) {
            handleResetForm(this, name);
        },
    }
});