text: /*
  * @description: 产品列表js
  * @author: will
  * @update: will (2017-02-22 11:10)
  * @version: v1.0
  */
  var app = angular.module('myApp', ['tm.pagination', 'ngCookies']);
  app.controller('listController', function ($scope, $http, $cookies) {
  	
  	//post请求的地址
  	var url = window.location.href,
  	//将参数传递的方式改成form
  	postCfg = {
  		headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  		transformRequest: function (data) {
  			return $.param(data);
  		}
  	};
      if($cookies.get("realName")!=''){
          $scope.realName=$cookies.get("realName");
      }
      if($cookies.get("pno")!=''){
          $scope.pno=$cookies.get("pno");
      }
      layui.use(['form'], function(){
  	    var form = layui.form;
  		form.on('select(isverify)', function(data){
  			  $scope.isverify = data.value;
  		}); 
  		form.on('select(status)', function(data){
  			  $scope.status = data.value;
  		}); 
  	});
      
      if($cookies.get("status")!=''){
          $scope.status=$cookies.get("status");
      }
      if($cookies.get("pName")!=''){
          $scope.pName=$cookies.get("pName");
      }
      if($cookies.get("currentPage")!=''){
          $scope.currentPage=$cookies.get("currentPage");
      }
      var reSearch = function () {
          var postData = {
              type: 'list',
              status: $scope.status,
              isverify: $scope.isverify,
              realName: $scope.realName,//供应商
              pno: $scope.pno,
              pName: $scope.pName,
              currentPage: $scope.paginationConf.currentPage,
              itemsPerPage: $scope.paginationConf.itemsPerPage,
          };
          $http.post(url, postData, postCfg).then(function (result) {  //正确请求成功时处理
          	$scope.paginationConf.totalItems = result.data.rowCount;
              $scope.list = result.data.data;
          }).catch(function () { //捕捉错误处理
              layer.msg('服务端请求错误！', {time: 3000});
          });
      }
      $scope.reSearch = reSearch;
      $scope.paginationConf = {//配置分页基本参数
          currentPage: $scope.currentPage ? $scope.currentPage : 1, //起始页
          itemsPerPage: 20, // 每页展示的数据条数
          perPageOptions: [20, 30, 50] //可选择显示条数的数组
      };
      $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', reSearch);
      
      $scope.checked = [];
      $scope.selectAll = function () {
          if ($scope.select_all) {
          	$scope.select_all = false;
          	$(".select_all").removeClass("layui-form-checked");
          	angular.forEach($scope.list, function (item) {
              	item.checked = false;
              	$(".selectOne").removeClass("layui-form-checked");
                  $scope.checked = [];
              })
          } else {
          	$scope.select_all = true;
          	$(".select_all").addClass("layui-form-checked");
          	$scope.checked = [];
              angular.forEach($scope.list, function (item) {
                  item.checked = true; 
                  $scope.checked.push(item.pId);
                  $(".selectOne").addClass("layui-form-checked");
              })
          }
      }
      //
      $scope.selectOne = function (value,evel) {
          angular.forEach($scope.list, function (item) {
              var index = $scope.checked.indexOf(item.pId);
              if(item.checked && item.pId === value){
              	item.checked = false;
              	event.currentTarget.className = "layui-unselect layui-form-checkbox selectOne";
              }else if(!item.checked && item.pId === value){
              	item.checked = true;
              	event.currentTarget.className = "layui-unselect layui-form-checkbox selectOne layui-form-checked";
              }
              if (item.checked && index === -1) {
                  $scope.checked.push(item.pId);
              } else if (!item.checked && index !== -1) {
                  $scope.checked.splice(index, 1);
              }
          })
          if ($scope.list.length === $scope.checked.length) {
              $scope.select_all = true;
              $(".select_all").addClass("layui-form-checked");
          } else {
          	$(".select_all").removeClass("layui-form-checked");
              $scope.select_all = false;
          }
      }
      
      $scope.audit = function (item) {
      	if(item != undefined){
      		$scope.pId = item.pId;
      	}else{
      		$scope.pId = $scope.checked;
      	}
      	if($scope.pId.length <= 0 || $scope.pId == ""){
      		layer.msg('请选择需要审核的产品！', {time: 1500});
      		return;
      	}
  	    layer.open({
  			type: 1,
  			title: "审核",
  			area: '500px',
  			shadeClose: true,
  			content: $('.popup-audit')
  		});
      }
      
      //审核
      $scope.rv_btn = function(flag){
          var pId = $scope.pId;
          var reason = $scope.reason;
          var pIds = '';
          if (angular.isUndefined(pId)) {
          	pIds = $scope.checked;
          } else {
          	pIds = pId;
          }
          if(flag == true){
          	isverify = 1;
          }else{
          	if (angular.isUndefined(reason)) {
          		layer.msg('请输入驳回原因！', {time: 1500});
                  return;
          	}
          	isverify = 2;
          }
          if (pIds == '') {
              layer.msg('请选择需要审核的产品！', {time: 1500});
              return;
          } else {
  	        //执行更新方法
  	        var postData = {
  	    		pIds: pIds ? pIds : '',
  				isverify: isverify,
  	            reason: reason ? reason : '',
  	            type: "shenhe"
  	        };
  	        $http.post(url, postData, postCfg).then(function (result) {
  	        	if (result.data=="true") { 
  	            	layer.close(layer.index); //关闭弹层
  	            	layer.msg("恭喜您，审核状态已经更新", {icon:1, time: 1500});
  	                return reSearch();
  	            }else {
  	            	layer.msg("抱歉，审核状态更新出错！", {icon: 2, time: 1500});
  	            }
  	        }).catch(function () { //捕捉错误处理
  	            layer.msg('服务端请求错误！', {time: 1500});
  	        });
          }
  	}
      
      $scope.addModal = function (id, menuid) {
      	layer.open({
              type: 2,
  			title: false,//标题
  			scrollbar: false,//关闭滚动条
  			resize: false,//禁止拉伸
  			area: ['100%', '100%'],//宽高
  			shadeClose: true,//点击遮罩是否可关闭
              content: detailuri +'&menuid=' + menuid,
          });
      }
      $scope.modifyModal=function (id,menuid){
      	var pno = $scope.pno ? $scope.pno : '';
      	var status = $scope.status ? $scope.status : '';
      	var isverify = $scope.isverify ? $scope.isverify : '';
      	var pName = $scope.pName ? $scope.pName : '';
      	var realName = $scope.realName ? $scope.realName : '';
          var currentPage = $scope.paginationConf.currentPage;
      	if (pno != '') {
              $cookies.put("pno", pno, {expires: new Date(new Date().getTime() + 36000)});
          }
      	if (realName != '') {
              $cookies.put("realName", realName, {expires: new Date(new Date().getTime() + 36000)});
          }
      	if (status != '') {
              $cookies.put("status", status, {expires: new Date(new Date().getTime() + 36000)});
          }
      	if (pName != '') {
              $cookies.put("pName", pName, {expires: new Date(new Date().getTime() + 36000)});
          }
      	if (isverify != '') {
              $cookies.put("isverify", isverify, {expires: new Date(new Date().getTime() + 36000)});
          }
          $cookies.put("currentPage", currentPage, {expires: new Date(new Date().getTime() + 36000)});
          layer.open({
              type: 2,
              title: false,//标题
  			scrollbar: false,//关闭滚动条
  			resize: false,//禁止拉伸
  			area: ['100%', '100%'],//宽高
  			shadeClose: true,//点击遮罩是否可关闭
              content: detailuri +'&id=' + id +'&menuid=' + menuid,
          });
      }
      $scope.delModel=function (id){
      	var ids = '';
          if (angular.isUndefined(id)) {
              ids = $scope.checked;
          } else {
              ids = id;
          }
          if (ids == '') {
              layer.msg('请选择需要删除的礼包！', {time: 1500});
          } else {
  	        var postData = {type: 'del', ids: ids};
  	        layer.alert('您确定要删除吗?', {icon: 5, title: "删除", resize: false,}, function(){
  	        	$http.post(url, postData, postCfg).then(function (result) {  //正确请求成功时处理
  	            	if (result.data=="true") { 
  	                    layer.msg('删除成功！',{icon: 6,time:1500});
  	                    return reSearch();
  	                } else {
  	                    layer.msg("删除失败！", {time: 1500});
  	                }
  	            }).catch(function () { //捕捉错误处理
  	                layer.msg('服务端请求错误！', {time: 1500});
  	            });
  	        });
          }
      }
      $scope.downModal=function (type,pId){
      	if(type=="down"){
      		var postData = {type: 'downOrUp', pId: pId,status:"down"};//下架
      			$http.post(url, postData, postCfg).then(function (result) {  //正确请求成功时处理
                  	if (result.data=="true") {
                          layer.msg('下架成功！',{icon: 6,time:1500});
                          return reSearch();
                      } else {
                          layer.msg("下架失败！", {time: 1500});
                      }
              }).catch(function () { //捕捉错误处理
                  layer.msg('服务端请求错误！', {time: 1500});
              });
      	}else{
      		var postData = {type: 'downOrUp', pId: pId,status:"up"};//上架
      			$http.post(url, postData, postCfg).then(function (result) {  //正确请求成功时处理
                  	if (result.data=="true") {
                          layer.msg('上架成功！',{icon: 6,time:1500});
                          return reSearch();
                      } else {
                          layer.msg("上架失败！", {time: 1500});
                      }
                  }).catch(function () { //捕捉错误处理
                      layer.msg('服务端请求错误！', {time: 1500});
                  });
      	}
      }
      $scope.resetSearch = function(){
          $scope.pno = '';
          $scope.currentPage = 0;
          $scope.pName = '';
          $scope.realName = '';
      }
  });