<template>
  <div class="col-sm-12" style="background-color: #FFFFFF">
    <div class="col-sm-12" style="padding:50px 100px;height: 25%">
      <div class="form-group col-sm-5" v-if="issearch">
<!--        <v-select-->
<!--          class="select select_list"-->
<!--          :value.sync="useType"-->
<!--          width="90%"-->
<!--          v-model="useType"-->
<!--          :options='typeOfUse'-->
<!--          :valueSingle="true"></v-select>-->
      </div>
      <div class="form-group col-sm-12" style="text-align: center" v-if="issearch">
        <button type="button" class="btn btn-primary item_btn" @click="load">搜索</button>
      </div>
      <div class="form-group col-sm-12" style="text-align: center" v-if="isupload">
        <button type="button" class="btn btn-primary item_btn" @click="showUpload = !showUpload">本地上传</button>
      </div>

    </div>
    <hr style="width: 90%;margin: 0px auto;border: #999999 1px solid;"/>
    <div class="col-sm-12" style="overflow: scroll;height: 75%">
      <div :class="bootstraped ? bootstraped + ' showData' : 'col-sm-15 showData'" v-for="row in fileInfoData">
        <div class="col-sm-12 item">
          <div class="left col-sm-5">
            <a href="#" class="thumbnail" style="width: 88%;height: 98%;margin-left: 16px">
              <img v-if="row.f_filetype === 'jpg'||row.f_filetype === 'png'||row.f_filetype === 'jpeg'||row.f_filetype === 'gif'||row.f_filetype === 'bmp'||row.f_filetype === 'MP3'" :src="row.f_downloadURL" alt="..."/>
              <img v-if="row.f_filetype === 'pdf'" src="../../assets/pdf.jpg" alt="" />
              <img v-if="row.f_filetype === 'xls'||row.f_filetype === 'xlsx'" src="../../assets/excel.jpg" alt=""  />
              <img v-if="row.f_filetype === 'doc'||row.f_filetype === 'docx'" src="../../assets/doc.jpg" alt=""  />
              <img v-if="row.f_filetype === 'mp3'" src="../../assets/mp3.png" alt=""  />
            </a>
          </div>
          <div class="right col-sm-15">
            <p :title="row.f_filename" class="clears"><strong>文件名：</strong>{{row.f_filename}}</p>
            <p :title="row.f_username" class="clears"><strong>操作员：</strong><span>{{row.f_username}}</span></p>
            <p :title="row.fusetype" class="clears"><strong>使用类型：</strong><span>{{row.fusetype}}</span></p>
            <p :title="row.f_uploaddate" class="clears"><strong>上传时间：</strong><span>{{row.f_uploaddate}}</span></p>
            <p :title="row.fremarks" class="clears"><strong>文件说明：</strong>{{row.fremarks}}</p>
            <p class="clears">
              <a v-if="row.f_filetype === 'jpg' || row.f_filetype==='png' || row.f_filetype==='gif' || row.f_filetype==='bmp'" :href="row.f_downloadURL" style="background:#6aa6e2" class="btn btn-primary" target="_blank" role="button">预览</a>
              <a v-else  @click="downloadfile(row.f_downloadURL)" style="background:#6aa6e2" class="btn btn-primary" role="button">下载</a>
              <a v-if="isdelete" @click="delet($index, row)" href="#" class="btn btn-default" role="button" role="button">删除</a>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
  <work-busy :is-busy="loading"></work-busy>
  <modal  :show.sync="showUpload" v-ref:modal backdrop="false">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="close">
        <img src="../../assets/x.png" style="margin: -4px;margin-right: -15px;height: 37px"/>
<!--      <span class="glyphicon glyphicon-remove"></span>--></button>
      <h4 class="modal-title" style="text-align: center" v-if="headers.fusetype=='MP3'">文件上传</h4>
    </header>
    <article slot="modal-body" class="modal-body form-horizontal" >
      <div class="form-group" >
       <!-blodid：在上传时候绑定用户标识-->
        <file-upload style="margin-top: -8px;margin-left: 45px" :name="'myFile' + blodid"
                     action="rs/file/uploadFile" tagname="文件上传" :headers="headers" multiple v-ref:file>
        </file-upload>
      </div>
      <div class="form-group" v-if="isusetype" style="padding-top: 15px">
        <label class="col-sm-2 control-label">文件类型：</label>
        <div class="col-sm-10">
          <input-select
            class="select select_list"
            :value.sync="headers.fusetype"
            v-model="headers.fusetype"
            :options="$appdata.getParam('使用类型')"
            :valueSingle="true"></input-select>
        </div>
      </div>
      <div class="form-group" v-if="isremark">
        <label class="col-sm-2 control-label">文件说明：</label>
        <div class="col-sm-10">
          <input class="form-control input_view" style=""
                 v-model="headers.fremarks"
                 :value="headers.fremarks"/>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>

<script>
import {HttpResetClass} from 'vue-client'
import {isEmpty} from "../Util";
import FileUpload from './FileUpload'

export default {
  title: '附件',
  props: ['blodid', 'businessid', 'isremark', 'isusetype', 'takeimg', 'defname', 'isupload', 'isdelete', 'bootstraped', 'issearch'],
  components: {
    FileUpload
  },
  data () {
    return {
      fileInfoData: [], // 数据库存储的文件记录对象数组
      headers: {'username': this.$login.f.name, 'blodid': '','businessid':'', 'fremarks': '','defname':'','fusetype':''}, // 调用rs/file/uploadFile 的参数
      showUpload: false, // 上传模态框控制
      useType: null,
      loading:false
    }
  },
  ready () {
    this.headers.blodid = this.blodid
    this.headers.businessid = this.businessid
    this.headers.defname = this.defname
    //tag;
    //tag;
    if (this.blodid || this.businessid) {
      //tag;
      //tag;
      this.load()
    }
  },
  methods: {
    delet(index, row) {
      this.$resetdelete('rs/entity/t_files', {id: row.id}, {resolveMsg: '删除成功', rejectMsg: '删除失败'}).then((res) => {
        this.$dispatch("delResid",row.id)
        this.load()
      })
    },
    // 下载
    downloadfile(filepath){
      var link = document.createElement('a');
      link.href = filepath;
      link.target='_blank'
      link.dispatchEvent(new MouseEvent('click'));
    },
    // 关闭文件上传对话框
    close () {
      this.showUpload = false
      // 将选的文件清空
      this.$refs.file.$el.querySelector('input').value = ''
      this.headers.fusetype = ''
      this.headers.fremarks = ''
      this.load()
    },
    // 查询
    async load() {
      let condition = ''
      if(this.businessid){
        condition = `f_businessid= '${this.businessid}' `
      }else{
        condition = `f_blobid= '${this.blodid}' `
      }
      if (!isEmpty(this.useType)) {
        condition += `and fusetype like '${this.useType}'`
      }
      let http = new HttpResetClass()
      let data = {
        tablename: 't_files',
        condition: condition + ` order by f_uploaddate desc `
      }
      let getFile = await http.load('POST', 'rs/sql/singleTable', {data: data}, {
        warnMsg: null,
        resolveMsg: null
      })

      for (var i = 0; i < getFile.data.length; i++) {
        // 如果使用类型包含导入字样,则是execl文件导入的,不做显示
        if(getFile.data[i].fusetype&&(getFile.data[i].fusetype.includes('execl导入'))){
          getFile.data.splice(i,1)
          i--
          continue
        }
        let temp = getFile.data[i].f_downloadpath
        let URL = temp.substring(temp.lastIndexOf(":\\") + 2)
        if (temp.startsWith('E:') || temp.startsWith('F:')){
          URL = `rs/image/file/${getFile.data[i].f_filename}`
        }
        console.log(URL)
        getFile.data[i].f_downloadURL = "http://" + location.host + "/" + URL
      }
      this.fileInfoData = []
      this.fileInfoData = getFile.data
    },
    convertBase64ToBlob(base64String) {
      // 将base64解码
      var bytes = atob(base64String);
      //var bytes = base64;
      var bytesCode = new ArrayBuffer(bytes.length);
      // 转换为类型化数组
      var byteArray = new Uint8Array(bytesCode);

      // 将base64转换为ascii码
      for (var i = 0; i < bytes.length; i++) {
        byteArray[i] = bytes.charCodeAt(i);
      }
      // 生成Blob对象（文件对象）
      return new Blob([bytesCode], {type: 'image/jpeg'});
    },
  },
  computed: {
    typeOfUse () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('使用类型')]
    }
  },
  events: {
    onFileUpload: function (file, res) {
      this.$dispatch("resid",res.id)
      this.headers.fremarks=''
      this.close()
    },
    beforeFileUpload:function (file) {
      this.loading = true
    },
    onAllFilesUploaded:function (file) {
      this.loading = false
    }

  },
  watch:{
    //监听blodid值的变化
    'blodid'(val){
      //tag;
      if (val) {
        //tag;
        this.load()
      }
    }
  },
}
</script>

<style scoped>
  .clears{
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
  }
  .showData {
    padding: 15px 10px 0px 10px;
    box-sizing: border-box;
    height: 230px;
    font-family: "微软雅黑";
  }
  .showData .item{
    padding-bottom: 10px;
    border-bottom: solid 1px #c1c1c1;
  }
  .left {
    padding-right: 10px;
  }
  .left img{
    height: 100%;
    width: 100%;
  }
  .right{
    height: 100%;
    /*display: -webkit-flex;*/
    /*display: flex;*/
    /*flex-direction: column;*/
    /*justify-content: space-around;*/
  }
  .item_btn{
    width: 80%;
    background-color: #6aa6e2;
    border-radius: 4px;
    color: #FFFFFF;
    font-family: PingFang;
  }
</style>
