// packageH/circleCommunity/circleIssue/circlelssue.js
var WxParse = require('../../../wxParse/wxParse');
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    payType:0,

    explainFlag:false,
    agreement:false,
		
    categorys:[],
    categoryIndex:null,

    circleFace:"https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/circleMoRenFace.png",	//头像
    circleBg:"https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/circleMoRenBg.png",		//背景
    name:'',			//圈子名称
    synopsis:'',		//简介
    price:0,			//付费金额

    circleId:0,
    circle_control:''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    this.getCategorys();
    this.getCircleSet();
    if(options.circleId && options.circleId!=0){
      this.setData({
        circleId:options.circleId
      });
    }

  },

  setPayType(evt){
    let type = evt.currentTarget.dataset.type;
    this.setData({
      payType:type
    });
  },
  onChangeAgreement(evt){
    this.setData({
      agreement: evt.detail,
    });
  },
  setExplainFlag(){
    this.setData({
      explainFlag:!this.data.explainFlag
    });
  },
  bindPickerChange(evt){
    this.setData({
      categoryIndex:evt.detail.value
    });
    console.log(evt.detail.value);
  },
	
  getCategorys(){
    let urlStr = app.getNetAddresss('plugin.circle.frontend.circle.get-category-list');
    app._postNetWork({
      url: urlStr,
      data:{},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          categorys : res.data
        });
        if(this.data.circleId!=0)this.getCircleInfo();
      }
    });
  },

  getCircleInfo(){
    let urlStr = app.getNetAddresss('plugin.circle.frontend.circle.get-circle');
    app._postNetWork({
      url: urlStr,
      data:{circle_id:this.data.circleId},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
				
        let categoryIndex=0;
        this.data.categorys.forEach((item,index)=>{
          if(item.id==res.data.category_id){
            categoryIndex = index;
          }
        });
        console.log(res.data);
        this.setData({
          name:res.data.name,
          categoryIndex,
          synopsis:res.data.synopsis,
          payType:res.data.is_toll,
          price:res.data.price,
          circleFace:res.data.circle_img,
          circleBg:res.data.bg_img
        });			
      }
    });
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

  submit(){
    let {agreement,name,categorys,categoryIndex,synopsis,payType,price,circleFace,circleBg} = this.data;
    if(agreement==false) return app.tips("请阅读用户协议");
    if(app._isTextEmpty(name)) return app.tips("请填写圈子名称");
    if(categoryIndex==null) return app.tips("请选择圈子分类");
    if(app._isTextEmpty(synopsis)) return app.tips("请填写圈子简介");
    if(payType==1 && price==0) return app.tips("请输入付费金额");

    let json = {
      is_toll:payType,
      name,
      category_id:categorys[categoryIndex].id,
      price,
      synopsis,
      bg_img:circleBg,
      circle_img:circleFace
    };

    let urlStr = app.getNetAddresss('plugin.circle.frontend.circle.create-circle');

    //修改
    if(this.data.circleId!=0){
      json.circle_id = this.data.circleId;
      urlStr = app.getNetAddresss('plugin.circle.frontend.circle.edit-circle');
    }


    app._postNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return app.tips(res.msg);
        wx.navigateBack();
      }
    });
  },

  //获取圈子设置
  getCircleSet() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle.get-set");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let article = res.data.circle_control;
        try{
          if(article.indexOf('&#') <= -1){
            WxParse.wxParse('article','html',article,this);
          }
        }catch(e){
          console.log(e);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  editImage(evt){
    let type = evt.currentTarget.dataset.type;
    this.upImage((res)=>{
      if(type=="face"){
        this.setData({
          circleFace:res.data.img_url
        });
      }else if(type=="bg"){
        this.setData({
          circleBg:res.data.img_url
        });
      }
    });
  },
	
  upImage(fn) {
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success:(res)=> {
        const tempFilePaths = res.tempFilePaths;
        let photourl = tempFilePaths[0]; //e.tempFilePath
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: 'file',
          success(resdata) {
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              fn(res);
            } else {
              app.tips(res.msg);
            }
          }
        });
      }
    });
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareTimeline(){},
  onShareAppMessage: function () {

  }
});