<template>
  <div>
    <el-drawer class="custom-drawer-form" title="专题库清单" size="800px" :visible.sync="drawer" direction="rtl" :before-close="close" destroy-on-close append-to-body>
      <el-container class="visual-project-list-drawer">
        <el-header class="visual-project-list-header" v-if=" $permission('/workspace/project/add')">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增专题库</el-button>
        </el-header>
        <el-main v-loading="loading">
          <el-table
              class="project-table"
              :data="tableData"
              header-row-class-name="custom-table-header"
              :row-class-name="tableRowClassName"
              row-key="id"
              :expand-row-keys="expands"
              @cell-click="cellClick"
          >
            <el-table-column label="序号" type="index" align="center" width="50px" show-overflow-tooltip></el-table-column>
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-table v-loading="loading2" class="file-table" :data="reportList" stripe header-row-class-name="custom-table-header">
                  <el-table-column type="index" label="序号" align="center" width="50px" show-overflow-tooltip></el-table-column>
                  <el-table-column label="报告名称" align="center" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <span class="report-name" @click="handleDownload(scope.row)">{{ scope.row.name }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="文件大小" align="center" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <span>{{ renderSize(scope.row.size) }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column prop="createName" label="创建人" align="center" show-overflow-tooltip></el-table-column>
                  <el-table-column label="创建时间" align="center" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <span>{{ $d(scope.row.createTime, 'YYYY.MM.DD') }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>
            <el-table-column label="报告数量" align="center">
              <template slot-scope="scope">
                <span class="report-num">{{ scope.row.reportNum }}</span>
              </template>
            </el-table-column>
            <el-table-column label="名称(专题库)" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-link @click.native="handleClick(scope.row)" type="primary">
                  <span v-if="scope.row.innerFile">{{ scope.row.innerFile }}- </span>
                  <span>{{ scope.row.name }}</span>
                </el-link>
              </template>
            </el-table-column>
            <el-table-column prop="clientName" label="客户" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="createName" label="创建人" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="创建时间" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{ $d(scope.row.createTime, 'YYYY.MM.DD') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span class="project-status">{{ scope.row.status === '0' ? '未完成' : '已完成' }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer>
          <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="prev, pager, next, jumper" background></el-pagination>
        </el-footer>
      </el-container>
    </el-drawer>

    <project-form-drawer ref="projectFormDrawer" @submit="submitProjectForm" :common-data="commonData" />
  </div>
</template>

<script>
import { downLoad2, renderSize } from '@/utils'
import ProjectFormDrawer from '../../workspace/components/drawer/Form'
import { mapGetters } from "vuex";

export default {
  components: {
    ProjectFormDrawer
  },
  data() {
    return {
      drawer: false,
      commonData: {},
      expands: [],
      queryParams: {
        group: [],
        type: [],
        scenario: [],
        name: '',
        technicalTheme: '',
        clientName: '',
        size: 15,
        current: 1,
        sort: {
          order: 'desc',
          prop: 'create_time'
        },
        myself: true
      },
      loading: false,
      loading2: false,
      tableData: [],
      reportList: [],
      total: 0
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  mounted() {
  },
  methods: {
    renderSize,
    handleClick(row) {
      const router = this.$router.resolve({
        path: '/workspace/folder',
        query: {
          id: row.id,
        }
      })
      this.$s.setSession('projectName',row.name)
      window.open(router.href, '_blank');
    },
    handleAdd() {
      this.$refs.projectFormDrawer.open({
        update: 0,
        updateTime: '一天',
        status: '0',
        sort: 1,
        eventId:this.queryParams.eventId,
        eventName:this.queryParams.eventName,
        typeList: this.queryParams.type.map(item => parseInt(item)),
        scenarioList: this.queryParams.scenario.map(item => parseInt(item))
      }, '新增专题库')
    },
    handleDownload(row) {
      var FileUrl =  `http://${this.$c.hostname}:8012/onlinePreview?url=`+ btoa(encodeURIComponent(this.$p + row.url))
      // var FileUrl =  `http://192.168.1.24:8012/onlinePreview?url=`+ btoa(encodeURIComponent(this.$p + row.url))
      const router = this.$router.resolve({
              path: '/checkFile',
              query: {
                row: JSON.stringify(row),
                FileUrl: FileUrl,
                isPicture:0
            }
        })
        window.open(router.href, '_blank');
    },
    async getCommonData() {
      const response = await this.$api.getCommonData({ keys: 'ENTERPRISE_APPLICATION_SCENARIO,INVESTIGATION_TYPE' })
      this.commonData = response.data
    },
    async open(scenarioName, typeName, question) {
      await this.getCommonData()
      const scenario = this.commonData.ENTERPRISE_APPLICATION_SCENARIO.find(item => item.label === scenarioName)
      if (scenario) {
        this.queryParams.scenario = [scenario.value]
      } else {
        return false
      }
      if (typeName) {
        const type = this.commonData.INVESTIGATION_TYPE.find(item => item.label === typeName)
        if (type) {
          this.queryParams.type = [type.value]
        } else {
          return false
        }
      } else {
        this.queryParams.type = []
      }
      if (question) {
        this.queryParams.eventId = question.questionId
        this.queryParams.eventName = question.questionName
      }
      this.queryParams.current = 1
      this.expands = []
      this.drawer = true
      this.getList()
    },
    submitProjectForm() {
      this.getList()
    },
    close() {
      this.$emit('close')
      this.drawer = false
    },
    getList() {
      this.loading = true
      this.$api.getProjectListV2(this.queryParams).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'height-row'
      }
    },
    getProjectReportList(id) {
      let params = {
        projectId: id,
        current: 1,
        size: 99999
      }
      this.loading2 = true
      this.$api.getProjectFileList(params).then(response => {
        this.reportList = response.data.records
        this.loading2 = false
      })
    },
    cellClick(row, column, cell, event) {
      if (column.label !== '报告数量' || row.reportNum === 0) {
        return false
      }
      const index = this.expands.indexOf(row.id)
      if (index === -1) {
        this.expands = []
        this.expands.push(row.id)
        this.getProjectReportList(row.id)
      } else {
        this.expands.splice(index, 1)
      }
    }
  }
}
</script>

<style lang="scss">
.visual-project-list-drawer {
  .visual-project-list-header {
    padding: 0 20px !important;
  }
  .custom-table-header > th {
    background: #deedff !important;
    color: #7ba6d5 !important;
    font-weight: 400 !important;
  }
  .project-table {
    color: #000000;
  }
  .file-table .custom-table-header > th {
    background: #ebf4ff !important;
  }
  .project-status {
    font-weight: 400;
    font-size: 14px;
    text-align: center;
    color: #3084e5;
  }
  .height-row {
    background: #f5faff !important;
  }
  .report-num {
    display: inline-block;
    width: 32px;
    height: 32px;
    border-radius: 16px;
    background: #deedff;
    font-weight: bold;
    font-size: 18px;
    text-align: center;
    color: #3084e5;
    line-height: 32px;
    cursor: pointer;
  }
  .report-name {
    cursor: pointer;
  }
  .report-name:hover {
    text-decoration: underline;
    color: #1e9fff;
  }
  .el-table__expand-icon {
    display: none;
  }
}
</style>