<template>
  <div v-loading.fullscreen="fullscreenLoading">
    <div class="xml-read" v-if="show">
    <xml-digital-teaching
      v-if="digitalTeaching"
      ref="xmlDigitalTeaching"
      :textBookData="textBookData"
      :catalogList="catalogList"
      :looseLeafData="looseLeafData"
      :location="location"
      :notesList="data.notes.list"
      :bookmarkList="data.bookmark.list"
      :resourcesList="data.resources.list"
      :highlightList="data.highlight.list"
      :isTrial="isTrial"
      :isTrialIndex="isTrialIndex"
      :userType="userType"
      :setting="setting"
      @updateReadLocation="updateReadLocation"
      @addNote="addNote"
      @editNote="editNote"
      @deleteNote="deleteNote"
      @deleteHighlight = "deleteHighlight"
      @addTeacherNote="addTeacherNote"
      @editTeacherNote="editTeacherNote"
      @deleteTeacherNote="deleteTeacherNote"
      @addHighlight="addHighlight"
      @updateNotes="updateNotes"
      @addBookmark="addBookmark"
      @deleteBookmark="deleteBookmark"
      @downloadFile="downloadFile"
      @addResources="addResources"
      @deleteResource="deleteResource"
      @editResources="editResources"
      @settingChange="settingChange"
      :action = "action"
      :headers  = "headers"
      :data = "extradata"
      :isShowTeacherResource="true"
      :continueReading="true"
      :basisPath="basisPath"
      :pageType="pageType"
      officePreviewPath="https://idocview.xxxxxxx.com/view/url?url="
    />
    </div>
      <div v-show="showError">
      <no-data :msg="errormsg"></no-data>
    </div>
  </div>

</template>

<script>
import {LOCAL_VIEW_DETAIL,LOCAL_VIEW_CATALOG,LOCAL_VIEW_DATA} from "../seed/mock"
import NoData from "../components/noData.vue"
import {getStore,processReuestData} from '../utils/mUtils'
import {bookApi}  from "../service/getData"
export default {
  components:{
    NoData
  },
  data() {
    return {
      action:'http://60.xxxx.137.15:8081/admin-api/teachingMaterial/resource/upload/16550/6940601956829184',
      headers:{Authorization:`eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjYyMDFlYjA3LTY4ZWUtNGJhOC1iMjc3LTk4MjU0ZGI0MzUwMSJ9.zvW8Rr8ShOvwH5i7Cvl9bgp4MtznyLusrY86LMLW7MplNOjRp6j8lx6zkAD4_CvNXVF7v7MsYcRhN90DvhTq1g`},
      digitalTeaching: true,
      // textBookData: LOCAL_VIEW_DETAIL,
      // catalogList: LOCAL_VIEW_CATALOG,
      // looseLeafData: LOCAL_VIEW_DATA,
      textBookData: {},
      catalogList: [],
      looseLeafData: {},
      bookId: '',
      show:false,
      showError:false,
      fullscreenLoading:true,
      errormsg:'无数据',
      location: '',
      basisPath: 'https://smile-ebook.oss-cn-qingdao.aliyuncs.com/newjc/',
      pageType: 'pc',
      isTrial: false,
      isTrialIndex: 10,
      userType: 'teacher',
      extradata :{},
      data: {
        notes: {
          list: []
        },
        bookmark: {
          list: []
        },
        highlight: {
          list: []
        },
        resources: {
          list: []
        }
      },
      setting: {}
    }
  },

  created() {
    this.bookId = this.$route.query.bookId
    // this.userType = this.$route.query.userType || 'student'
    
     var userType = getStore("userType")
    
     this.extradata = processReuestData({})

    //  var uploadUrl = "https://local.xinsiketang.com"
    var uploadUrl = ""
     uploadUrl  = uploadUrl+'/api/smile/uploadResources'
     this.action = uploadUrl
     if (userType){
      this.userType = userType
     }
    
    if (this.$route.query.type) {
      this.pageType = this.$route.query.type
      // this.isTrialIndex = Number(this.$route.query.isTrialIndex)
      if (this.$route.query.isTrialIndex) {
        this.isTrial = true
      }
    }

    this.location = localStorage.getItem(this.textBookData.id)
    setTimeout(() => {
      // this.getData()
      this.getMineData()
      // this.getSetting()
    }, 800)
  },

  async mounted(){
    this.pageType = /Mobi|Android|iPhone/i.test(navigator.userAgent)?'h5':'pc'

    
    // resourcesList({})

    console.log(this.bookId)

  //   var locationData = await bookApi.userAction.getReadLocation({bookId:this.bookId})
  //  this.location = locationData.location
    
  try {
    this.textBookData = await bookApi.bookinfo.detail({bookId:this.bookId})

  } catch (error) {
    this.fullscreenLoading = false
    this.show= false
    this.showError = true
    this.errormsg = error
    this.$message.error(error);
  }
   this.basisPath = this.textBookData.basisPath
   this.catalogList = await bookApi.bookinfo.category({bookId:this.bookId})
   this.looseLeafData = await bookApi.bookinfo.data({bookId:this.bookId})
   this.data.resources.list = await bookApi.resource.list({bookId:this.bookId})
   this.show = true
   this.fullscreenLoading = false

  },
  methods: {

    // 学生笔记操作 start
    addNote(model) {
      bookApi.note.add({
        bookId:this.bookId,
        data:JSON.stringify(model)
      })
      this.addData('notes', model)
    },
    editNote(model) {
      bookApi.note.edit({
        bookId:this.bookId,
        data:JSON.stringify(model)
      }) 
      this.updateData('notes', model)
    },
    deleteNote(model,feedback) {
     
         bookApi.note.delete({
        bookId:this.bookId,
        data:JSON.stringify(model)
      }) 
      this.deleteData('notes', model)
      feedback()
    },

    // 教师笔记操作 start
    addTeacherNote(model, rollback) {
      bookApi.note.add({
        bookId:this.bookId,
        data:JSON.stringify(model)
      })  
          this.addData('resources', model)
    },
    editTeacherNote(model) {
      bookApi.note.edit({
        bookId:this.bookId,
        data:JSON.stringify(model)
      })  
      this.updateData('resources', model)
    },
    deleteTeacherNote(model, feedback) {
      bookApi.note.delete({
        bookId:this.bookId,
        data:JSON.stringify(model)
      }) 
      this.deleteData('resources', model)
      feedback()
    },

    // 高亮操作
    addHighlight(model, rollback) {

      bookApi.highlight.add({
        bookId:this.bookId,
        data:JSON.stringify(model)
      })
      this.addData('highlight', model)
    },
    // 高亮删除
    deleteHighlight(model, rollback) {

      bookApi.highlight.delete({
        bookId:this.bookId,
        data:JSON.stringify(model)
      })
      this.deleteData('highlight', model)
      console.log(model,"model")
    },

    settingChange(key, value) {
      // let newSetting = Object.assign({}, this.setting, { [key]: value })
      // localStorage.setItem('setting_#_' + this.textBookData.id, JSON.stringify(newSetting))
      // this.setting = JSON.parse(localStorage.getItem('setting_#_' + this.textBookData.id)) || {}
    },

    // 更新笔记
    updateNotes(model) {

      bookApi.note.edit({
        bookId:this.bookId,
        data:JSON.stringify(model)
      })
      this.updateData('notes', model)
    },

    // 添加书签
    addBookmark(e) {  
      bookApi.bookmark.add({
        bookId:this.bookId,
        data:JSON.stringify(e)
      })
      this.addData('bookmark', e)
    },

    // 删除书签
    deleteBookmark(e) {
      bookApi.bookmark.delete({
        bookId:this.bookId,
        pkid:e.pkid,
        data:JSON.stringify(e)
      })
      this.deleteData('bookmark', e)
    },

    // 添加数据
    addData(type, item) {
      this.data[type].list.push(item)
      let list = JSON.stringify(this.data)
      localStorage.setItem('simulated-data_' + this.textBookData.id, list)
      this.getData(type, item)
    },



    async getMineData(){


      this.data.notes.list = await bookApi.note.list({
        bookId:this.bookId,

      })
     
      this.data.bookmark.list = await bookApi.bookmark.list({
        bookId:this.bookId
      })

      this.data.highlight.list = await bookApi.highlight.list({
        bookId:this.bookId
      })
      this.data.resources.list = await bookApi.resource.list({
        bookId:this.bookId
      })
      

    },
    // 查询数据
    getData(type, item) {
      setTimeout(() => {
        // JSON.parse(localStorage.getItem('simulated-data_' + this.textBookData.id))
        
        this.data = 
        JSON.parse(localStorage.getItem('simulated-data_' + this.textBookData.id))
        
        || {
          notes: {
            list: []
          },
          bookmark: {
            list: []
          },
          highlight: {
            list: []
          },
          resources: {
            list: []
          }
        }
        // 
      }, 300)
    },
    getSetting() {
      this.setting = JSON.parse(localStorage.getItem('setting_#_' + this.textBookData.id)) || {}
    },

    // 更新数据
    updateData(type, item) {
      console.log(item)
      let i = this.data[type].list.findIndex((items) => items.id == item.id)
      this.data[type].list[i] = item
      let list = JSON.stringify(this.data)
      localStorage.setItem('simulated-data_' + this.textBookData.id, list)
      this.getData(type, item)
    },

    // 删除数据
    deleteData(type, item) {
      console.log(item,"deleteData")
      let i = this.data[type].list.findIndex((items) => items.id == item.id)
      this.data[type].list.splice(i, 1)
      let list = JSON.stringify(this.data)
      localStorage.setItem('simulated-data_' + this.textBookData.id, list)
      this.getData(type, item)
    },

    // 下载资源文件
    downloadFile(e) {
      console.log(e)
    },

    // 更新阅读进度
    updateReadLocation(newLocation) {

      console.log('我要更新阅读进度了'+newLocation)
      bookApi.userAction.updateReadLocation({
        bookId:this.bookId,
        data: newLocation.location
      })
      localStorage.setItem(this.textBookData.id, newLocation.location)
    },

    // 添加教师资源
    addResources(e) {
      bookApi.resource.add({
        bookId:this.bookId,
        data:JSON.stringify(e)
      })
      this.addData('resources', e)
    },
    // 删除教师资源
    deleteResource(e) {
      bookApi.resource.delete({
        bookId:this.bookId,
        data:JSON.stringify(e)
      })
      this.deleteData('resources', e)
    },
    // 编辑教师资源
    editResources(e) {
      bookApi.resource.edit({
        bookId:this.bookId,
        data:JSON.stringify(e)
      })
      this.updateData('resources', e)
    }
  }
}
</script>