<template>
  <div class="upload">
    <!--头部操作-->
<!--    <Header>
      <save type="save" @click.native="save()"></save>
    </Header>-->
    <el-row>
      <el-col :span="14" v-if="$route.path === '/upload_archives'">
        <!--标题 分类-->
        <Set ref="set" @setSelectChange="selectChange" @setTitleChange="titleChange" />
        <!--文件列表-->
        <file-list ref="attFileList" v-if="uploadType == '1' || uploadType == '2'">
          <upload slot="upload" @onSuccess="onSuccess" target="archives" :classType="uploadType"></upload>
          <template v-slot:delete="scope">
            <delete1 @deleteFile="deleteFile" :obj="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>
      </el-col>
      <el-col :span="24" v-else>
        <!--标题 分类-->
        <Set ref="set" @setSelectChange="selectChange" @setTitleChange="titleChange" />
        <!--文件列表-->
        <file-list ref="attFileList" v-if="uploadType == '1' || uploadType == '2'">
          <upload slot="upload" @onSuccess="onSuccess" target="archives" :classType="uploadType"></upload>
          <template v-slot:delete="scope">
            <delete1 @deleteFile="deleteFile" :obj="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>
      </el-col>
      <!-- 授权范围 -->
      <el-col :span="10" v-if="$route.path === '/upload_archives'">
        <scopeOfauth
          style="margin-left:10px;"
          :defaultData="defaultData"
          :dataid="authId"
          :disabled="false"
          obiectName="sat_sharematerialid"
          @onChecked="onChecked"
        />
      </el-col>
    </el-row>
    <div class="fixed__btn__panel">
      <el-button size="small" @click="$store.dispatch('DrawerShowChange',false)" class="normal-btn-width">取 消</el-button>
      <el-button size="small" :type="$route.query.type === '1' ?'warning':'primary'" @click="save" class="normal-btn-width">{{$route.query.type === '1' ?'保 存' : '确 定'}}</el-button>
    </div>
  </div>
</template>

<script>
import scopeOfauth from '@/components/scopeOfAuthority/index.vue'

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 removeFile from '@/utils/removeFile'
import { log } from '@antv/g2plot/lib/utils'
export default {
  name: 'index',
  data () {
    return {
      //新增请求配置
      cacheInfo: {
        "classname": "webmanage.saletool.sharematerial.sharematerial",
        "method": "insertOrUpdate",
        "content": {
          "title": "",
          "sat_sharematerial_classid": 9999,
          "notes": "",
          "tag": [],
          "isonlymanager_dept":0,
          "canfiledownload": 1,
          "content": "",
          "sat_sharematerialid": 0,
          "type": ''
        }
      },
      //上传的初始数据
      uploadCacheData: {},
      //创建素材模板的id
      create_sat_sharematerialid: 0,
      uploadType: null,
      /* 授权使用 */
      defaultData: {},
      authData: {},
      authId: 0,
    }
  },
  components: {
    Header,
    Set,
    FileList,
    FileText,
    upload,
    save,
    reset,
    delete1,
    download,
    Editor,
    scopeOfauth
  },
  computed: {
  },
  watch: {
    $route () {
      this.uploadData()
    }
  },
  mounted () {
    //获取上传模板
    this.uploadData()
  },
  beforeDestroy () {
    // this.removeAllData()
  },
  methods: {
    //获取素材模板
    uploadData () {
      setTimeout(() => {
        let data = JSON.parse(window.sessionStorage.getItem("newMaterial"))
        console.log(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.uploadType = data.type
        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.authId = this.$route.query.id

        setTimeout(this.randerData, 100)
        if (data.title != '') this.uploadCacheData.status = "完成"
        this.query_auth()
      },100)
    },
    /* 更新附件列表数据 */
    randerData () {
      if (this.uploadType != 3) this.$refs.attFileList.randerData(this.uploadCacheData.attinfos)
    },
    deleteFile (id) {
      this.uploadCacheData.attinfos = this.uploadCacheData.attinfos.filter(v => v.linksid != id)
      this.randerData()
    },
    //保存
    save () {
      this.$refs.set.$refs.form.validate((val) => {
        if (val) {
          //判断有无内容
          if (this.uploadType == '3') {
            const content = this.$refs.editor.editor.getHtml()
            if (content == "<p><br></p>") return this.$message.error('您还未编辑图文内容')
            this.cacheInfo.content.content = content
          } else {
            if (this.uploadCacheData.attinfos.length == 0) return this.$message.error('您还未上传附件')
          }
          this.cacheInfo.content.type = this.uploadType
          console.log(this.cacheInfo);

          //判断所属ID是否为初始ID
          if (this.cacheInfo.content.sat_sharematerial_classid == '9999' + JSON.parse(window.sessionStorage.getItem("active_account")).userid) return this.$message.error('请选择素材分类')
          this.$api.requested(this.cacheInfo).then(res => {
            this.tool.showMessage(res, () => {
              this.uploadCacheData.status = '完成'
              if (this.$route.path === '/upload_archives'){
                this.insertCoursewareauth()
              }

              this.$store.dispatch('DrawerShowChange',false)
              this.$router.back()
              this.$emit('onSuccess')
            })
          })
        } else {
          this.$notify({
            title: '提示',
            message: '请输入标题或分类',
            type: 'error'
          })
        }
      })
    },
    // 查询授权信息
    async query_auth () {
      const res = await this.$api.requested({
        "classname": "webmanage.saletool.sharematerial.sharematerialAuth",
        "method": "selectAuth",
        "content": {
          "sat_sharematerialid": this.uploadCacheData.sat_sharematerialid,
        }
      })
      this.defaultData = res.data
    },
    // 新增授权范围
    async insertCoursewareauth () {
      let p = {
        "classname": "webmanage.saletool.sharematerial.sharematerialAuth",
        "method": "insertOrUpdate",
        "content": this.authData
      }
      const res = await this.$api.requested(p)
      res.code === 0 ? this.$notify({
        title: '失败',
        message: res.data,
        type: 'error'
      }) : ''
    },
    // 获取保存授权范围数据
    onChecked (param) {
      this.authData = param
    },

    //文件上传成功
    async onSuccess (id) {
      console.log(id)

      let param = {
        "classname": "system.attachment.Attachment",
        "method": "createFileLink",
        "content": {
          "ownertable": "SAT_SHAREMATERIAL",
          "ownerid": this.uploadCacheData.sat_sharematerialid,
          "usetype": "default",
          "attachmentids": [id]
        }
      }
      let res = await this.$api.requested(param)
      if (res.msg != '成功') return this.$notify({
        title: '提示',
        message: '上传失败',
        type: 'error'
      })
      this.uploadCacheData.attinfos = this.uploadCacheData.attinfos.concat(res.data)
      this.randerData()
    },
    //退出后对数据进行销毁
    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 = []
    },
    /* set组件 分类选择回调 */
    selectChange (id) {
      this.cacheInfo.content.sat_sharematerial_classid = id
    },
    /* set组件 title填写回调 */
    titleChange (name) {
      this.cacheInfo.content.title = name
    },
  },
};
</script>

<style scoped>
.mandate {
  width: 732px;
  height: 500px;
  background: #fff;
  margin-left: 16px;
}
</style>
