<template>
  <div class="upload">
    <!--头部操作-->
    <Header>
      <save slot="save" @btnClick="save()"></save>
    </Header>
    <!--标题 分类-->
    <Set ref="set"></Set>
    <!--文件列表-->
    <file-list :attinfos="uploadCacheData.attinfos" v-if="uploadType == '1' || uploadType == '2'">
      <upload slot="upload" @onSuccess="onSuccess" target="archives" :classType="uploadType"></upload>
      <template v-slot:delete="scope">
        <delete1 :data="scope.data"></delete1>
      </template>
      <template v-slot:download="scope">
        <download :data="scope.data"></download>
      </template>
    </file-list>
    <!--富文本-->
    <file-text ref="text" v-if="uploadType == '3'">
      <Editor ref="editor" slot="editor" :content="cacheInfo.content.content" :id="uploadCacheData.sat_sharematerialid">
      </Editor>
    </file-text>
  </div>
</template>

<script>
import Header from '@/SManagement/archives_upload/components/Header'
import Set from '@/SManagement/archives_upload/components/Set'
import FileList from '@/SManagement/archives_upload/components/FileList'
import FileText from '@/SManagement/archives_upload/components/FileText'
import upload from '@/SManagement/archives_upload/components/upload'

import save from '@/SManagement/archives_upload/components/save'
import reset from '@/SManagement/archives_upload/components/reset'
import delete1 from '@/SManagement/archives_upload/components/delete'
import download from '@/SManagement/archives_upload/components/download'
import Editor from '@/components/my-editor/Editor'

import { weAtob, weBtoa } from "@/utils/weapp-jwt"
import removeFile from '@/utils/removeFile'
export default {
  name: 'index',
  data() {
    return {
      //新增请求配置
      cacheInfo: {
        "classname": "webmanage.saletool.sharematerial.sharematerial",
        "method": "insertOrUpdate",
        "content": {
          "title": "",
          "sat_sharematerial_classid": 9999,
          "notes": "",
          "tag": [],
          "canfiledownload": 1,
          "content": "",
          "sat_sharematerialid": 0,
          "type": ''
        }
      },
      //上传的初始数据
      uploadCacheData: '',
      //创建素材模板的id
      create_sat_sharematerialid: 0,
      uploadType: window.sessionStorage.getItem('uploadType')
    };
  },
  components: {
    Header,
    Set,
    FileList,
    FileText,
    upload,
    save,
    reset,
    delete1,
    download,
    Editor
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.getFileCache()
  },
  beforeDestroy() {
    this.removeAllData()
  },
  methods: {
    //保存
    save() {
      this.$refs.set.$refs.form.validate((val) => {
        var that = this
        if (val) {
          if (this.uploadType == '3') this.cacheInfo.content.content = weBtoa(this.$refs.editor.editor.getHtml())
          this.cacheInfo.content.type = this.uploadType
          this.$api.requested(this.cacheInfo).then(res => {
            let param = {
              "classname": "webmanage.saletool.sharematerial.sharematerial",
              "method": "audit",
              "content": {
                "sat_sharematerialid": res.data.sat_sharematerialid,
                "type": 1
              }
            }
            this.$api.requested(param).then(res => {
              if (res.code == 1) {
                this.uploadCacheData.status = '完成'
                this.$notify({
                  title: '提示',
                  message: '上传成功',
                  type: 'success'
                })
                this.$router.go(-1)
              }
            })
          })
        } else {
          this.$notify({
            title: '提示',
            message: '请输入标题或分类',
            type: 'error'
          })
        }

      })
    },
    //获取上传数据模板
    getFileCache() {
      this.$api.requested(this.cacheInfo).then(res => {
        this.uploadCacheData = res.data
        this.create_sat_sharematerialid = this.uploadCacheData.sat_sharematerialid
        this.cacheInfo.content.sat_sharematerialid = this.uploadCacheData.sat_sharematerialid

        this.isEditFile()
      })
    },
    //走编辑逻辑
    isEditFile() {
      if (this.$route.params.data) {
        let data = JSON.parse(this.$route.params.data)
        this.cacheInfo.content.sat_sharematerialid = data.sat_sharematerialid
        this.cacheInfo.content.title = data.title
        this.cacheInfo.content.sat_sharematerial_classid = data.sat_sharematerial_classid
        this.cacheInfo.content.content = data.content

        this.$refs.set.ruleForm.title = data.title
        this.$refs.set.ruleForm.category = data.sat_sharematerial_classid
        this.uploadCacheData.sat_sharematerialid = data.sat_sharematerialid
        this.uploadCacheData.attinfos = data.attinfos
        this.uploadCacheData.status = "完成"
      }
    },
    //文件上传成功
    onSuccess(id) {
      let param = {
        "classname": "system.attachment.Attachment",
        "method": "createFileLink",
        "content": {
          "ownertable": "SAT_SHAREMATERIAL",
          "ownerid": this.uploadCacheData.sat_sharematerialid,
          "usetype": "default",
          "attachmentids": [
            id
          ]
        }
      }
      this.$api.requested(param).then(res => {
        if (res.msg != '成功') {
          this.$notify({
            title: '提示',
            message: '上传失败',
            type: 'error'
          })
        } else {
          this.uploadCacheData.attinfos = this.uploadCacheData.attinfos.concat(res.data[0]);
        }
      })

    },
    //退出后对数据进行销毁
    async removeAllData() {
      if (this.$route.params.data) {
        this.uploadCacheData.status = "新建"
        this.uploadCacheData.sat_sharematerialid = this.create_sat_sharematerialid
      }
      if (this.uploadCacheData.status == '新建') {
        let attinfos = this.uploadCacheData.attinfos;
        //删除附件
        removeFile.removeFileList(attinfos)
        //删除素材
        let param = {
          "classname": "webmanage.saletool.sharematerial.sharematerial",
          "method": "delete",
          "content": {
            "sat_sharematerialid": this.uploadCacheData.sat_sharematerialid
          }
        }
        let result = await this.$api.requested(param)
      }
    },
    //重置所有数据
    reSetAllData() {
      this.cacheInfo.content.title = ''
      this.cacheInfo.content.sat_sharematerial_classid = ''
      this.$refs.editor.html = ''
      this.$refs.set.ruleForm.title = ''
      this.$refs.set.ruleForm.category = ''
      this.uploadCacheData.attinfos = []
    }
  },
};
</script>

<style scoped>
</style>
