var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    param:{
      order_id:0
    },
    sub_param:{
      order_id: 0,
      scores:{},//评论星星个数
      dp_group:{}
    },
    dp_img:{},
    dp_group:{},//点评标签
    start_item:{}//点评星星
  },
  onLoad: function (options) {
    var self = this;
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
    if (parseInt(options.order_id)>0){
      var sub_param = this.data.sub_param;
      sub_param.order_id = options.order_id;
      var param = new Object;
      param.order_id = options.order_id;
      this.setData({
        sub_param: sub_param,
        param: param
      });
    }
  },
  onReady: function () {
    var self = this;
    if (self.data.param.order_id>0){
      app.getUserInfo(function (ret) {
          self.getData();
      });
    }else{
      app.HCerror('提示', '缺少必要参数（订单ID）', false, function () {
        app.HCnavigateBack();
      });
    }
  },
  getData: function () {
    var self = this;
    app.showLoading('加载中...');
    var action = 'c=Dp&a=index';
    var param = $.ObjectToStr(self.data.param);
    app.ajaxRequest(action, 'GET', param , function (ret) {
      console.log(ret);
      if (ret.status == 1) {
        var start_item = new Object;
        if (ret.data.list){
          for (var i in ret.data.list){
            start_item[ret.data.list[i].id] = new Object;
            start_item[ret.data.list[i].id][0] = true;
          }
        }
        var dp_group = new Array;
        if (ret.data.dp_group&&ret.data.dp_group.preset){
          dp_group = ret.data.dp_group.preset.split(" ");
        }
        if (ret.data && ret.data.list){
          var sub_param = self.data.sub_param;
          sub_param.goods_id = new Object;
          for (var i in ret.data.list){
            sub_param.goods_id[ret.data.list[i].id] = ret.data.list[i].goods_id;
          }
        }
        self.setData({
          list:ret.data.list,
          dp_group: dp_group,
          start_item: start_item,
          sub_param: sub_param
        });
      } else {
        if (!ret.msg) ret.msg = '参数错误';
        app.HCerror('提示',ret.msg,false);
      }
    }, true)
  },
  binddpgroup: function (event){
    var self = this;
    var id = event.currentTarget.dataset.id;
    var item = event.currentTarget.dataset.item;
    var dp_group = self.data.sub_param.dp_group;
    if (dp_group[id]){
      if (dp_group[id][item]){
        delete dp_group[id][item];
      }else{
        dp_group[id][item] = true;
      }
    }else{
      dp_group[id] = new Object;
      dp_group[id][item] = true;
    }
    var sub_param = self.data.sub_param;
    sub_param.dp_group = dp_group;
    self.setData({
      sub_param: sub_param
    });
  },
  bindstart:function(event){
    var self = this;
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    var goods_id = event.currentTarget.dataset.goods_id;
    var start_item = self.data.start_item;
    for (var i = 0; i < 5; i++){
      if (i<(parseInt(index) + 1)){
        start_item[id][i] = true;
      }else{
        delete start_item[id][i];
      }
    }
    var scores = new Object;
    var sub_param = self.data.sub_param;
    sub_param.scores[id] = (parseInt(index) + 1);
    self.setData({
      start_item: start_item,
      sub_param: sub_param
    });
  },
  binddesc:function(event){
    var self = this;
    var sub_param = self.data.sub_param;
    var value = event.detail.value;
    var id = event.currentTarget.dataset.id;
    if (!sub_param.content){
      sub_param.content = new Object;
    }
    sub_param.content[id] = value;
    self.setData({
      sub_param: sub_param
    });
  },
  addimg: function (event){
    var self = this;
    var dp_img = self.data.dp_img; 
    var sub_param = self.data.sub_param; 
    var id = event.currentTarget.dataset.id;
    
    app.HCaddimg(function(ret,res){
      if(ret.status==1){
        if(!dp_img[id]){
          dp_img[id] = new Array;
        }
        if (!sub_param.dp_img){
          sub_param.dp_img = new Object;
        }
        if (!sub_param.dp_img[id]) {
          sub_param.dp_img[id] = new Array;
        }
        dp_img[id].push(res.tempFilePaths[0]);
        sub_param.dp_img[id].push(ret.data);
      }
      self.setData({
        sub_param: sub_param,
        dp_img: dp_img
      });
    });
  },
  del_dp_img:function(event){
    var self = this;
    var dp_img = self.data.dp_img;
    var sub_param = self.data.sub_param;
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    sub_param.dp_img[id].splice(index,1);
    dp_img[id].splice(index, 1);
    self.setData({
      sub_param: sub_param,
      dp_img: dp_img
    });
  },
  sub_comment:function(){
    var self = this;
    app.showLoading('加载中...');
    var action = 'c=Dp&a=add_dp';
    var data = new Object;
    var sub_param = self.data.sub_param;
    data.content = JSON.stringify(sub_param.content);
    data.dp_group = JSON.stringify(sub_param.dp_group);
    data.scores = JSON.stringify(sub_param.scores);
    data.dp_img = JSON.stringify(sub_param.dp_img);
    data.goods_id = JSON.stringify(sub_param.goods_id);
    data.order_id = sub_param.order_id;
    app.ajaxRequest(action, 'POST', data, function (ret) {
      if (!ret.msg) ret.msg = '参数错误';
      app.HCerror('提示', ret.msg, false,function(){
        app.HCnavigateBack();
      });
    }, true)
  },
  LookBigimg:function(event){
    var imgsrc = event.currentTarget.dataset.item;
    app.HCpreviewImage(imgsrc);
  }
})