var nowPageList = []
function request (url, data) {
    return new Promise((resovle, reject) => {
        Ajax.request({
            type: data.method,
            url: url,
            contentType: "application/json",
            params: JSON.stringify(data.data),
            successCallback: function (r) {
                resovle(r)
            }
        })
    })
    
}
$(function () {
    var types = {
     0:'服饰',
     1:'家居',
     2:'数码',
     3:'美妆',
     4:'其他',
    }
     
     $("#jqGrid").Grid({
         url: '../slideShow/getAndQuery',
         rownumWidth:60,
         savedRow: ['coverImage'],
         colModel: [{
             label: 'id', name: 'id', index: 'slideId', key: true, hidden: true
         }, 
         {
            label: 'slideId', name: 'slideId', index: 'slideId', hidden: true
        },
        {
            label: 'id', name: 'id', index: 'slideId', key: true, hidden: true
        },
         {
             label: '标题', name: 'title', index: 'title', width: 80
         },
         {
            label: '封面图片',align:'center', name: 'coverImage', index: 'cover_image', width: 120, formatter: function (value) {
                return transImg(value,100,'auto');
            }
        },
         {
             label: '跳转链接', name: 'redirectUrl', index: 'redirect_url', width: 140,formatter: function (value) {
                 if (value) {
                    return '<span title="'+value+'">'+(value.length > 40 ?(value.substr(0,40) + '...'):value)+'</span>'
                 }
                 return ''
            }
         },
         {
             label: '排序',align:'center', name: 'sortOrder', index: 'sort_order', width: 80
         }, 
         {
            label: '更新人员', name: 'updater', index: 'updater', width: 80,formatter: function (value) {
                return value || '-'
            }
        }, 
        {
            label: '更新时间', name: 'updateTime', index: 'update_time', width: 80, formatter: function (value) {
                return value
            }
        },
         {
             label: '显示',align:'center', name: 'isShow', index: 'is_show', width: 80, formatter: function (value) {
                 return transIsNot(value)
             }
         },
     ],
     loadComplete (data) {
        nowPageList = data.page.list
     }
     });
  });
  
  

  const validateSceneUrlCheck = function (rule, value, callback){
     var link1 ='https://test.4dkankan.com/eShopMobile.html?m='
     var link2 ='https://www.4dkankan.com/eShopMobile.html?m='
  
     if (value === ''||!value) {
         callback(new Error('场景地址不能为空'));
     } else if(!~value.indexOf(link1)&&!~value.indexOf(link2)){
         callback(new Error('场景地址需为'+link2+'或'+link1));
     } else {
         callback();
     }
  };
  let user = JSON.parse(localStorage.getItem('userInfo'))

  
  var vm = new Vue({
     el: '#rrapp',
     data: {
        loading:false,
        userInfo:user,
        showList: true,
         title: null,
         carousel: {title: '',coverImage:'',redirectUrl:'',sortOrder:'',updater:'', isShow: 1,remark:''},
         ruleValidate: {
            title: [
                {required: true, message: '标题不能为空', trigger: 'blur'}
            ],
            coverImage: [
                {required: true, message: '轮播图片不能为空', trigger: 'blur'}
            ]
         },
         q: {
             name: ''
         }
     },
     methods: {
        handleRemove (file) {
            
        },
         query: function () {
             vm.reload();
         },
         add: function () {
             vm.showList = false;
             vm.title = "新增";
             vm.carousel =  {title: '',coverImage:'',redirectUrl:'',sortOrder:'',updater:'',remark:'', isShow: 1}
         },
         update: function (event) {
             var data = getSelectedRowData("#jqGrid");
             if (data.id == null) {
                 return;
             }
             vm.showList = false;
             vm.title = "修改";
             
             vm.getInfo(data.slideId)
         },
         saveOrUpdate: function (event) {
             var url = vm.carousel.slideId == null ? "../slideShow/add" : "../slideShow/update";
             let user = JSON.parse(localStorage.getItem('_userInfo') || "{}")
             vm.carousel.updater = user.username
             
             vm.carousel.updateUserDeptId=vm.userInfo.deptId
             vm.carousel.updateUserId=vm.userInfo.userId
             vm.carousel.createUserId=vm.userInfo.userId
             vm.carousel.createUserDeptId=vm.userInfo.deptId
             Ajax.request({
                 type: "POST",
                 url: url,
                 contentType: "application/json",
                 params: JSON.stringify(vm.carousel),
                 successCallback: function () {
                     alert('操作成功', function (index) {
                         vm.reload();
                     });
                 }
             });
         },
         getAndQuery () {
            Ajax.request({
                type: 'get',
            })
         },
         del: function (event) {
             var ids = getSelectedRows("#jqGrid");
             if (ids == null) {
                 return;
             }
             console.log($("#jqGrid").jqGrid('getRowData',ids))
             const slideIds = ids.map(id => $("#jqGrid").jqGrid('getRowData',id) && $("#jqGrid").jqGrid('getRowData',id).slideId)
            //  ids.forEach(item => {

            //  })
             let delArr = nowPageList.filter(item => slideIds.indexOf(item.slideId) > -1)
             confirm('确定要删除选中的记录？', function () {
                Promise.all(delArr.map(item => {
                    item.enable = 0
                    return request('../slideShow/update', {method: 'POST', data: item})
                })).then(res => {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                })
                //  Ajax.request({
                //      type: "POST",
                //      url: "../slideShow/update",
                //      contentType: "application/json",
                //      params: JSON.stringify(ids),
                //      successCallback: function () {
                //          alert('操作成功', function (index) {
                //              vm.reload();
                //          });
                //      }
                //  });
             });
         },
         getInfo: function (id) {
            vm.defaultList = []
             Ajax.request({
                 url: "../slideShow/getById?slideId=" + id,
                 async: true,
                 successCallback: function (r) {
                     vm.carousel = r.page;
                 }
             });
         },
         reload: function (event) {
             vm.showList = true;
             var page = $("#jqGrid").jqGrid('getGridParam', 'page');
             $("#jqGrid").jqGrid('setGridParam', {
                 postData: {'name': vm.q.name},
                 page: page
             }).trigger("reloadGrid");
             vm.handleReset('formValidate');
         },
         handleSuccessListPicUrl: function (res, file) {
             vm.brand.listPicUrl = file.response.url;
             vm.brand.appListPicUrl = file.response.url;
         },
         handleSuccessPicUrl: function (res, file) {
             vm.brand.picUrl = file.response.url;
         },
         handleSuccessAppListPicUrl: function (res, file) {
             vm.brand.appListPicUrl = file.response.url;
         },
         handleSuccessCarouselImage: function (res, file) {
            this.loading = false
            this.carousel.coverImage = file.response.url + window.fixImg + 'm_fixed,w_450,h_250';
            // this.carousel.coverImage = file.response.url
        },
      
        handleBefore(){
            this.loading = true
        },
         handleSuccessNewPicUrl: function (res, file) {
             vm.brand.newPicUrl = file.response.url;
         },
         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 + ' 太大。'
             });
         },
         eyeImageListPicUrl: function () {
             var url = vm.brand.listPicUrl;
             eyeImage(url);
         },
         eyeImagePicUrl: function () {
             var url = vm.brand.picUrl;
             eyeImage(url);
         },
         eyeImageAppListPicUrl: function () {
             var url = vm.brand.appListPicUrl;
             eyeImage(url);
         },
         eyeImageNewPicUrl: function () {
             var url = vm.brand.newPicUrl;
             eyeImage(url);
         },
         handleSubmit: function (name) {
             console.log('====================================');
             console.log(name);
             console.log('====================================');
             handleSubmitValidate(this, name, function () {
                 vm.saveOrUpdate()
             });
         },
         handleReset: function (name) {
             handleResetForm(this, name);
         }
     },
    mounted () {
      
    }
  });