import { config } from "../../config/config";
import { user_data } from "../../data/data";
import { http } from "../../framework/http";
import { log } from "../../framework/log";
import { tools } from "../../framework/tools";
import { util } from "../../framework/util";
import { UserData } from "../../stores/userDataManager";
import { UserStatus } from "../../stores/userStatusManager";

const novelPlugin = requirePlugin('novel-plugin');

Component({
  properties: {
    novelManagerId: {
      type: Number,
      value: -1
    },
    bookId: {
      type: String,
      value: ''
    },
    chapterIndex: {
      type: Number,
      value: -1
    },
  },

  data: {
    loading: false,
	status_name: "解锁",
	chapter_data:null,
  },
 lifetimes:{
	 attached(){
		let chapterIndex = this.properties.chapterIndex
		setTimeout(()=>{
			UserStatus().getSelectBookDirectoryList((list)=>{
				if(list!=null){
					for (var i = 0; i < list.length; i++) {
						if((list[i].id-1) == chapterIndex){
							this.data.chapter_data = list[i]
							break
						}
					}
					console.log("chapter_data",this.data.chapter_data)
					if(this.data.chapter_data!=null){
						this.data.status_name = `解锁需要${this.data.chapter_data.coin}书币`
						this.setData({status_name:this.data.status_name})
					}
				}
			
			})
		},100)
	
	 }
 },
 pageLifetimes:{
	show() {
		
	},

 },
  methods: {
	get_status_name(){
		return  this.data.status_name
	},
	
	BuyBookChapter(chapter_id,coin){
		const novelManager = novelPlugin.getNovelManager(this.properties.novelManagerId);
		if(UserData().getData().coin>=coin){
			util.showLoading('正在购买...',true)
			let book_data = UserStatus().getUserSelectBook()
			http.DynamicRequest(config.url_confg.Dynamic.buy_chapter,{'book_id':book_data.book_id,'chapter_id':chapter_id},(err,d)=>{
				if(!err){
					log.Debug(d)
					if(d.code==config.url_confg.StatesCode.SUCCESS){
						// d.content.includes(chapter_id)
						UserData().update_user_info(()=>{
							util.hideLoading()
							util.showInfoToast('购买成功！')
							novelManager.paymentCompleted()
						})
					}else{
					
					}
				}else{
					log.Error(err)
				}
			
			})
		}else{
			util.showModal("书币不足","是否充值？",()=>{
				// novelManager.switchTab({url:'/pages/mine/mine'})
				novelManager.navigateTo({url:"/pagesA/recharge/rechargeCoin"})
			})
		}
	
	},
    // 这里实现自己的付费逻辑
    async pay() {
      // novelManagerId 句柄 用来获取novelManager实例
      const novelManager = novelPlugin.getNovelManager(this.properties.novelManagerId);

      // getId() 返回当前manager对应的Id(即句柄)
      console.log('id:', novelManager.getId());

      // getBookId() 返回当前manager对应的bookId
      console.log('bookId:', novelManager.getBookId());

			
		if(this.data.chapter_data!=null){
			this.BuyBookChapter(this.properties.chapterIndex+1,this.data.chapter_data.coin)
		}
      // wx.showModal({
      //   title: '付费逻辑',
      //   content: '请到components/charge-dialog/charge-dialog.js中完善付费逻辑',
      //   showCancel: false,
      // });
    },
  },
})
