<template>
  <div class="archive-administration">
    <el-backtop target=".archive-administration" :right="70" :bottom="70"></el-backtop>
    <div class="op">
      <div class="fileNumber">
        <el-input
          v-model="fileNumber"
          size="mini"
          placeholder="请输入案卷号"
          clearable></el-input>
      </div>
      <div class="createTime">
        <el-date-picker
          v-model="createTime"
          type="date"
          size="mini"
          placeholder="请选择归档日期">
        </el-date-picker>
      </div>
      <div class="ywtype">
        <el-cascader
          size="mini"
          :options="options"
          :props="{ checkStrictly: true }"
          placeholder="请选择业务类型"
          clearable
          @change="handleChange">
        </el-cascader>
      </div>
      <el-button
        type="primary"
        size="mini"
        style="background:#148A9B;margin-left:10px"
        icon="el-icon-search"
        @click="paramSearch">搜索
      </el-button>
      <el-button
        type="primary"
        size="mini"
        style="margin-left: 20px"
        @click="searchAllArchiveList">
        查看所有档案案卷
      </el-button>
      <el-button
        type="primary"
        size="mini"
        style="margin-left: 20px"
        @click="exportAllArchiveAllList">
        导出所有档案案卷
      </el-button>
    </div>
    <el-table
      v-loading.fullscreen.lock="loading"
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        align="center"
        label="序号"
        type="index"
        width="50"
        :index="this.indexStart">
      </el-table-column>
      <el-table-column
        align="center"
        prop="type"
        label="档案类别"
        width="100">
      </el-table-column>
      <el-table-column
        align="center"
        prop="volumeNumber"
        label="全宗号"
        width="70">
      </el-table-column>
      <el-table-column
        align="center"
        prop="number"
        label="档号"
        width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="getFileList(scope.row)">
            {{scope.row.number}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="year"
        label="年度"
        width="70">
      </el-table-column>
      <el-table-column
        align="center"
        prop="retentionPeriod"
        label="保管期限"
        width="60">
      </el-table-column>
      <el-table-column
        align="center"
        prop="institution"
        label="机构(问题)"
        width="70">
      </el-table-column>
      <el-table-column
        align="center"
        prop="responsiblePerson"
        label="责任者"
        width="130">
      </el-table-column>
      <el-table-column
        align="center"
        prop="title"
        label="正题名"
        width="330">
      </el-table-column>
      <el-table-column
        align="center"
        prop="startTimeStr"
        label="起时间"
        width="120">
      </el-table-column>
      <el-table-column
        align="center"
        prop="endTimeStr"
        label="止时间"
        width="120">
      </el-table-column>
      <el-table-column
        align="center"
        prop="pages"
        label="页数"
        width="60">
      </el-table-column>
      <el-table-column
        align="center"
        prop="files"
        label="件数"
        width="60">
      </el-table-column>
      <el-table-column
        align="center"
        prop="createTimeStr"
        label="归档时间"
        width="120">
      </el-table-column>
      <el-table-column
        align="center"
        prop="fileType"
        label="档案类别"
        width="100">
      </el-table-column>
      <el-table-column
        align="center"
        prop="secondaryCategory"
        label="二级类目"
        width="250">
      </el-table-column>
      <el-table-column
        align="center"
        prop="categoryType"
        label="类别代码"
        width="70">
      </el-table-column>
      <el-table-column
        align="center"
        prop="businessType"
        label="业务类别"
        width="250">
      </el-table-column>
      <el-table-column
        align="center"
        prop="fileNumber"
        label="案卷号"
        width="70">
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="exportArchive(scope.row)">
            导出
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button
      class="fileButton"
      type="primary"
      @click="exportOneArchiveList">
      导出当页案卷列表
    </el-button>
    <el-button
      class="fileButton"
      type="primary"
      @click="exportAllArchiveList">
      导出全部案卷列表
    </el-button>
    <el-pagination
      class="pone"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals"
      :current-page.sync="pageNum"
      @current-change="current_change">
    </el-pagination>
  </div>
</template>
<script>
  import CryptoJS from 'crypto-js'
  import {baseUrl} from "../../utils/global"
  import axios from "axios"
  import Cookies from "js-cookie";

  export default {
    data() {
      return {
        tableData: [],
        loading: false,
        totals: 0,
        pageNum: 1,
        pageSize: 10,
        indexStart: 1,
        options: [{
          value: {
            eventType: 1,
            eventTypeDetail: null,
          },
          label: '参保登记',
        }, {
          value: 2,
          label: '信息变更',
          disabled: true,
          children: [{
            value: {
              eventType: 2,
              eventTypeDetail: 1,
            },
            label: '档次变更',
          }, {
            value: {
              eventType: 2,
              eventTypeDetail: 2,
            },
            label: '银行卡变更',
          }, {
            value: {
              eventType: 2,
              eventTypeDetail: 3,
            },
            label: '手机号变更',
          }, {
            value: {
              eventType: 2,
              eventTypeDetail: 4,
            },
            label: '参保身份变更',
          }, {
            value: {
              eventType: 2,
              eventTypeDetail: 5,
            },
            label: '户籍地址变更',
          }, {
            value: {
              eventType: 2,
              eventTypeDetail: 6,
            },
            label: '姓名变更',
          }, {
            value: {
              eventType: 2,
              eventTypeDetail: 7,
            },
            label: '身份证变更',
          }]
        }, {
          value: {
            eventType: 3,
            eventTypeDetail: null,
          },
          label: '待遇申请',
        }, {
          value: {
            eventType: 4,
            eventTypeDetail: null,
          },
          label: '参保注销',
        }, {
          value: {
            eventType: 5,
            eventTypeDetail: null,
          },
          label: '补缴保费',
        }, {
          value: {
            eventType: 6,
            eventTypeDetail: null,
          },
          label: '升档改办',
        }, {
          value: {
            eventType: 7,
            eventTypeDetail: null,
          },
          label: '关系转移',
        }],
        fileNumber: '',
        createTime: '',
        eventType: null,
        eventTypeDetail: null,
      }
    },
    methods: {
      current_change(pageNum) {
        this.pageNum = pageNum;
        this.indexStart = (pageNum - 1) * this.pageSize + 1;
        const data = {
          pageNum: pageNum,
          pageSize: this.pageSize,
          fileNumber: this.fileNumber,
          createTime: this.createTime,
          eventType: this.eventType,
          eventTypeDetail: this.eventTypeDetail,
        };
        this.search(data);
      },
      search(data) {
        this.loading = true;
        this.$api.getlist.selectArchive(data)
          .then((response) => {
            if (response.code === 200) {
              this.tableData = response.data.content;
              this.totals = response.data.totalSize;
            }
          })
          .catch((response) => {
          });
        this.loading = false;
      },
      handleSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.pageNum = 1;
        this.indexStart = 1;
        const data = {
          pageNum: 1,
          pageSize: pageSize,
          fileNumber: this.fileNumber,
          createTime: this.createTime,
          eventType: this.eventType,
          eventTypeDetail: this.eventTypeDetail,
        };
        this.search(data);
      },
      getFileList(row) {
        this.$router.push({
          name: "档案文件",
          params: {
            archiveObject: row
          }
        });
      },
      decrypt(word) {
        var key = CryptoJS.enc.Utf8.parse("KkweInfo23255625");
        var decrypt = CryptoJS.AES.decrypt(word, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
        return CryptoJS.enc.Utf8.stringify(decrypt).toString();
      },
      decryptByDES(ciphertext) {
        var keyHex = CryptoJS.enc.Utf8.parse("KkweFace95271124");
        var decrypted = CryptoJS.DES.decrypt({
          ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
        }, keyHex, {
          mode: CryptoJS.mode.ECB,
          padding: CryptoJS.pad.Pkcs7
        });
        return decrypted.toString(CryptoJS.enc.Utf8);
      },
      exportArchive(archive) {
        this.loading = true;
        axios.get(baseUrl + '/archive/getArchivePdf', {
          headers: {
            "token": Cookies.get("token")
          },
          params: {
            archiveId: archive.id
          },
          responseType: 'blob'      //告诉服务器我们需要的响应格式
        })
          .then(res => {
            if (res.data === null || res.data.size === 0) {
              this.$message({
                type: 'success',
                message: "该表格没有内容"
              });
            } else {
              let a = document.createElement("a");
              let blob = new Blob([res.data], {
                type: 'application/vnd.ms-excel'      //将会被放入到blob中的数组内容的MIME类型
              });
              a.href = URL.createObjectURL(blob);  //生成一个url
              a.download = archive.number + "-" + archive.title + ".rar";
              a.click();
            }
            this.loading = false;
          })
          .catch(err => {
            console.log(err);
            this.loading = false;
          });
      },
      exportOneArchiveList() {
        const data = {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          fileNumber: this.fileNumber,
          createTime: this.createTime,
          eventType: this.eventType,
          eventTypeDetail: this.eventTypeDetail,
        }
        this.exportArchiveList(data);
      },
      exportAllArchiveList() {
        const data = {
          pageNum: null,
          pageSize: null,
          fileNumber: this.fileNumber,
          createTime: this.createTime,
          eventType: this.eventType,
          eventTypeDetail: this.eventTypeDetail,
        };
        this.exportArchiveList(data);
      },
      exportArchiveList(data) {
        this.loading = true;
        axios.post(baseUrl + '/exportExcel/archiveList', null, {
          headers: {
            "token": Cookies.get("token")
          },
          data: data,
          responseType: 'blob'      //告诉服务器我们需要的响应格式
        })
          .then(res => {
            let a = document.createElement("a");
            let blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'      //将会被放入到blob中的数组内容的MIME类型
            });
            a.href = URL.createObjectURL(blob);  //生成一个url
            a.download = "归档案卷列表" + Date.now() + ".xlsx";
            a.click();
            this.loading = false;
          })
          .catch(err => {
            console.log(err);
            this.loading = false;
          });
      },
      handleChange(value) {
        this.eventType = null;
        this.eventTypeDetail = null;
        for (const i in value) {
          if (typeof (value[i].eventType) !== "undefined") {
            this.eventType = value[i].eventType;
            this.eventTypeDetail = value[i].eventTypeDetail;
            break;
          }
        }
      },
      paramSearch() {
        const data = {
          pageNum: 1,
          pageSize: this.pageSize,
          fileNumber: this.fileNumber,
          createTime: this.createTime,
          eventType: this.eventType,
          eventTypeDetail: this.eventTypeDetail,
        };
        this.search(data);
      },
      searchAllArchiveList() {
        this.fileNumber = null;
        this.createTime = null;
        this.eventType = null;
        this.eventTypeDetail = null;
        const data = {
          pageNum: 1,
          pageSize: this.pageSize,
          fileNumber: null,
          createTime: null,
          eventType: null,
          eventTypeDetail: null,
        };
        this.search(data);
      },
      exportAllArchiveAllList() {
        const data = {
          pageNum: null,
          pageSize: null,
          fileNumber: null,
          createTime: null,
          eventType: null,
          eventTypeDetail: null,
        };
        this.exportArchiveList(data);
      },
    },
    created() {
      this.loading = true;
      const data = {
        pageNum: 1,
        pageSize: this.pageSize,
        fileNumber: null,
        createTime: null,
        eventType: null,
        eventTypeDetail: null,
      };
      this.$api.getlist.selectArchive(data)
        .then((response) => {
          if (response.code === 200) {
            this.tableData = response.data.content;
            this.totals = response.data.totalSize;
          }
        })
        .catch((response) => {
        });
      this.loading = false;
    }
  }
</script>
<style scoped>
  .pone {
    margin-top: 20px;
    margin-bottom: 20px;
  }

  .fileButton {
    width: 10%;
    margin-top: 10px;
  }

  .archive-administration {
    height: 100%;
    overflow-y: scroll;
  }

  .op {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    /*margin-top: 10px;*/
  }

  .createTime {
    /*width: 250px;*/
    margin-left: 20px;
  }

  .fileNumber {
    width: 200px;
    /*margin-left: 20px;*/
  }

  .ywtype {
    /*width: 200px;*/
    margin-left: 20px;
  }
</style>
