<template>

  <div>
    <div class="aside">
      <div>
        <br>
        <br>
        <avue-tree :option="option" :data="caOptions" @node-click="handleNodeClick" >
           <span class="el-tree-node__label" slot-scope="{ node, data }">
             <span>
      <i class="el-icon-folder"></i>
      {{ (node || {}).label }}
            </span>
          </span>
        </avue-tree>
      </div>
    </div>
    <div style="float:left;width:83%">
      <el-tabs  type="card" v-model="activeTabs" @tab-click="handleClick">
        <el-tab-pane  name="1">
          <span slot="label"><i class="el-icon-folder-checked"></i> 预立卷</span>
        </el-tab-pane>
        <el-tab-pane name="2">
          <span slot="label"><i class="el-icon-thumb"></i> 待审核</span>
        </el-tab-pane  >
        <el-tab-pane name="3">
          <span slot="label"><i class="el-icon-refresh-left"></i> 已退回</span>
        </el-tab-pane  >
        <br>
        <storage-time  @storageTime="getStorageTimeValue" @changeArchiveScopeValue="changeArchiveScopeValue"  :archiveScopeValue="code"></storage-time>

        <avue-crud ref="crud" style="padding-left:8px"
                   :page="page"
                   :data="tableData"
                   :table-loading="tableLoading"
                   @on-load="getPage"
                   @refresh-change="refreshChange"
                   :option="getOption"
                   v-model="form"
                   @row-update="handleUpdate"
                   @row-save="handleSave"
                   @row-del="handleDel"
                   @search-change="searchChange"
                   @selection-change="selectionChange" class="mainCrud"
        >
<!--          左边按钮组-->
          <template slot="menuLeft">
            <el-button type="primary" size="small" icon="el-icon-plus"  @click="insertRecord()">新增</el-button>
            <el-button type="warning" size="small" icon="el-icon-s-promotion"  @click="transfer()">移交</el-button>
            <router-link to="/archiveturnoverinfo"><el-button
              type="success" size="small" icon="el-icon-s-claim">移交清单</el-button></router-link>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDel()">删除</el-button>
          </template>
<!--          行内按钮组-->
          <template slot="menu" slot-scope="scope">
            <el-dropdown ><span class="el-dropdown-link"><el-button type="text" size="small">操作列表<i class="el-icon-arrow-down el-icon--right"></i></el-button>
             </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item  ><span  @click="showFileList(scope.row,scope.index)"><i class="el-icon-document"></i> 卷内文件</span>
                </el-dropdown-item>
                <el-dropdown-item  v-if='scope.row.status == "1" || scope.row.status == "3"' ><span @click="updateRecordInfo(scope.row)"><i class="el-icon-edit"></i> 编辑</span>
                </el-dropdown-item>

                <el-dropdown-item  v-if='scope.row.status == 1 || scope.row.status == 3'><span @click="electronicFileUpload(scope.row)"><i class="el-icon-upload"></i> 上传</span>
                </el-dropdown-item>
                <el-dropdown-item ><span @click="showFileUploadList(scope.row)"><i class="el-icon-tickets"></i>上传列表</span>
                </el-dropdown-item>
                <el-dropdown-item  v-if='scope.row.status == 1'><span @click="copy(scope.row)"><i class="el-icon-document-copy"></i> 复制</span>
                </el-dropdown-item>
                <el-dropdown-item  ><span @click="view(scope.row)"><i class="el-icon-view"></i> 查看</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </avue-crud>

      </el-tabs>
    </div>
<!--卷内文件-->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      width="70%"
   >
    <archiveFileinfo  :arId="arId"  :archiveCategory="code" :status="status" ref="archiveFileinfo" :archiveCategoryName="archiveCategory" :archiveScope="archiveScope" :storageTime="storageTime"></archiveFileinfo>

    </el-dialog>
<!--   上传组件-->
   <file-upload ref="fileUploadRef" @closeFlag="closeFlag"></file-upload>
<!--   文件列表组件-->
    <show-file-list ref="showFileUploadRef"></show-file-list>
  </div>
</template>

<script>

import {listInfo, addInfo, downLoadUtil} from '@/api/archives/archiveRecord.js'
import {delInfo, transfer, updateInfo} from "@/api/archives/archiveRecord";
import archiveFileinfo from '@/views/archives/archiveFileInfo';
import {treeselectAndYear} from "@/api/archives/categoryInfo";
import {getOptionUtils} from "@/utils/util";
import fileUpload from '@/views/archives/fileUpload';
import ShowFileList from "@/views/archives/showFileList/index";
import StorageTime from "@/views/archives/commonComponent/storageTime";
export default {
  components: {StorageTime, ShowFileList, archiveFileinfo,fileUpload},
  data() {
    return {
      title:'',
      status:'',
      year:null,
      dialogVisible:false,
      tableLoading:false,
      storageTime:'',
      selectDate: [],
      activeTabs:'1',
      arId:"",
      option: {
        filter: false,
        defaultExpandAll: true,
        props: {
          labelText:'标题',
          label:'label',
          value:'code',
          children:'children'
        }
      },
      form: {},
      tableData: [],
      code:'WS',
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      caOptions: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      paramsSearch:{
        archiveCategory:'WS'
      },
      archiveCategory:'',
      archiveScope:'',
      storageTimeValue:null,
      archiveScopeValue:null,
    }
  },
  created() {
    this.getTreeselect();

  },
  watch:{
      'form.archiveCategory'(value){
        this.form.archiveCategory=this.code
      },
    getOption(value){
      if (this.activeTabs=='3'){
        value.column[1].hide=false
      }else{
        value.column[1].hide=true
      }
    },
    'activeTabs'(val){
       if (val=='3'){
         this.$refs.crud.option.column[1].hide=false
       }else{
         this.$refs.crud.option.column[1].hide=true
       }
    }
  },
  computed: {
    getOption() {
        return getOptionUtils(this.code)
    }
  },
  mounted() {
  },
  methods: {
    changeArchiveScopeValue(val){
      if (val!=null&&val!='-1'){
        this.archiveScopeValue=val;
      }else{
        this.archiveScopeValue=null;
      }
    },
    getStorageTimeValue(val){
      if (val!=null&&val!='0'){
        this.storageTimeValue=val;
      }else{
        this.storageTimeValue=null;
      }
    },
    showFileUploadList(row){
      this.$refs.showFileUploadRef.fileUpParamsSearch.fileId=row.id
      this.$refs.showFileUploadRef.fileUpParamsSearch.fileType='record'
      this.$refs.showFileUploadRef.fileUploadPage.currentPage=1
      this.$refs.showFileUploadRef.getUpPage(this.$refs.showFileUploadRef.fileUploadPage)
      this.$refs.showFileUploadRef.drawer=true
    },
    closeFlag(){
      this.getPage(this.page)
    },
    insertRecord(){
      this.$refs.crud.rowAdd()
    },
    copy(row){
      let obj={
        address: row.address,
        archiveCategory: row.archiveCategory,
        archiveScope: row.archiveScope,
        buildingNumber: row.buildingNumber,
        cabinetNumber: row.cabinetNumber,
        country: row.country,
        department: row.department,
        issuingUnit: row.issuingUnit,
        major: row.major,
        partner: row.partner,
        projectNumber: row.projectNumber,
        project_name: row.project_name,
        secretLevel: row.secretLevel,
        storageTime: row.storageTime,
        winnersUnit: row.winnersUnit,
        startDate:row.startDate,
        endDate:row.endDate,
        descriptionTime:row.descriptionTime,
        recordType:row.recordType,
        year: row.year,
        ownerName:row.ownerName,
        provide_date:row.provide_date,
        location_num:row.location_num,
        original_path:row.original_path,
        see_no:row.see_no,
        disc_number:row.disc_number,
        number:row.number
      }
      this.$refs.crud.rowCopy(obj)
    },
    view(row){
      this.$refs.crud.rowView(row)
    },
    electronicFileUpload(row){
      this.$refs.fileUploadRef.options.query.fileId = row.id;
      this.$refs.fileUploadRef.options.query.archiveCategory=row.$archiveCategory
      if (row.$archiveScope!=null&&row.archiveScope!=undefined&&''!=row.$archiveScope){
        this.$refs.fileUploadRef.options.query.archiveScope=row.$archiveScope
      }
      this.$refs.fileUploadRef.options.query.storageTime=row.$storageTime
      this.$refs.fileUploadRef.dsBox=true;
    },
    updateRecordInfo(row,index){
      this.$refs.crud.rowEdit(row, index);
    },
    //移交
    transfer(){
      let _this = this
      if(this.selectDate.length==0){
        _this.$message({
          showClose: true,
          message: '请先选择需要移交的数据',
          type: 'warning'
        })
        return;
      }
      let ids = [];
      for (var i=0;i<this.selectDate.length;i++){
        let row = this.selectDate[i];
        if(row.status=='2'){
          _this.$message({
            showClose: true,
            message: '选中档案包含已经移交过的档案，请勿重复操作',
            type: 'warning'
          })
          return;
        }
        ids.push(row.id);
      }
      this.$confirm('是否确认移交此数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return transfer(ids)
      }).then(data => {
        _this.$notify({
          title:'成功',
          showClose: true,
          message: '移交成功',
          type: 'success'
        })

        this.getPage(this.page)
      }).catch(function (err) {

      })
    },

    showFileList(row,index){
        this.title=row.boxName+"》卷内文件"
        this.arId=row.id
        this.status=row.status
        this.archiveCategory=row.$archiveCategory
        if (row.$archiveScope!=undefined&&row.$archiveScope!=null&&""!=row.$archiveScope){
          this.archiveScope=row.$archiveScope
        }else{
          this.archiveScope=null
        }
        this.storageTime=row.$storageTime
        this.dialogVisible=true
      setTimeout(()=>{
        let  page={
            currentPage: 1, // 当前页数
            pageSize: 10, // 每页显示多少条
          };
        this.$refs.archiveFileinfo.getPage(page)
      },)
    },
    getTreeselect() {
      let obj={
        parentId:1
      }
      treeselectAndYear(obj).then((response) => {
        this.caOptions = response.data;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (data.code.indexOf("-")!=-1){
        let  split= data.code.split("-");
        this.code=split[0];
        this.year=split[1]
      }else{
        if (data.code!=this.code){
          this.archiveScopeValue=null;
          this.storageTimeValue=null;
          this.paramsSearch={};
          this.$refs.crud.searchReset();
        }
        this.code=data.code;
        this.year=null
      }
      this.page.currentPage=1
      this.getPage(this.page)
      setTimeout(n=>{
        this.form.archiveCategory=this.code;
      },1000)

    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    searchChange(params, done) {
      if(params.searchDate &&　params.searchDate.length > 1){
        params.searchDate = params.searchDate[0]+","+params.searchDate[1];
      }else {
        delete(params.searchDate);
      }
      this.paramsSearch = params
      this.page.currentPage = 1
      this.getPage(this.page, params)
      done()
    },

    getPage(page, params) {
      this.paramsSearch.status=this.activeTabs;
      this.paramsSearch.archiveCategory=this.code;
      this.paramsSearch.year=this.year;
      this.paramsSearch.storageTime=this.storageTimeValue;
      this.paramsSearch.archiveScope=this.archiveScopeValue;
      this.tableLoading = true;
      listInfo(Object.assign({
        pageNum: page.currentPage,
        pageSize: page.pageSize,
      }, params, this.paramsSearch)).then(response => {
        this.tableData = response.rows
        this.page.total = response.total.toString()
        this.page.currentPage=page.currentPage
        this.page.pageSize=page.pageSize
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    /**
     * @title 数据删除
     * @param row 为当前的数据
     * @param index 为当前删除数据的行数
     *
     **/
    /**
     * @title 数据删除
     *
     **/
    handleDel: function () {
      if (this.selectDate.length == 0) {
        this.$notify({
          showClose: true,
          message: '请先选择需要删除的数据',
          type: 'warning'
        })
      } else {
        let ids = new Array();
        let length = this.selectDate.length;
        for (let i = 0; i < length; i++) {
          if (this.selectDate[i].status=='2'){
            this.$notify({
              showClose: true,
              message: '错误,不能删除待审核中的档案',
              type: 'warning'
            })
            return;
          }
          ids.push(this.selectDate[i].id)
        }
        this.$confirm('是否确认删除此数据', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delInfo(ids);
        }).then(data => {
          this.$notify({
            title: '成功',
            showClose: true,
            message: '删除成功',
            type: 'success'
          });
          this.getPage(this.page);
        }).catch(function (err) {
        })
      }
    },
    /**
     * @title 数据更新
     * @param row 为当前的数据
     * @param index 为当前更新数据的行数
     * @param done 为表单关闭函数
     *
     **/
    handleUpdate: function (row, index, done, loading) {
      updateInfo(row).then(response => {
        this.$notify({
          title: '成功',
          showClose: true,
          message: '修改成功',
          type: 'success'
        })
        done()
        this.getPage(this.page)
      }).catch(() => {
        loading()
      })
    },
    /**
     * @title 数据添加
     * @param row 为当前的数据
     * @param done 为表单关闭函数
     *
     **/
    handleSave: function (row, done, loading) {
      //预立卷
       row.status='1';
      addInfo(row).then(response => {
        this.$notify({
          title: '成功',
          showClose: true,
          message: '添加成功',
          type: 'success'
        })
        done()
        this.getPage(this.page)
      }).catch((res) => {
        done()
      })
    },

    /**
     * 刷新回调
     */
    refreshChange(page) {
      this.getPage(this.page)
    },
    handleClick(tab, event) {

      this.getPage(this.page);
    },


    //多选
    selectionChange(list) {
      this.selectDate = list;
    },
  }
}
</script>

<style>
.aside {
  width: 17%;
  height: 100%;
  margin-bottom: 50px;
  text-align: center;
  float: left;
  padding-left: 35px;
  padding-right: 10px;

}
</style>
