<template>
  <div class="common-project-table">
    <el-table class="project-table" :data="data" border header-row-class-name="custom-table-header" @sort-change="sortChange" row-key="id"
      :expand-row-keys="expands"
      @cell-click="cellClick">
      <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" width="50px">
        <template slot-scope="scope">
          <span>{{ (scope.$index + 1) + ((params.current - 1) * params.size) }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable="custom" prop="name" label="名称" align="center" min-width="150px" show-overflow-tooltip>
        <template slot-scope="scope" >
        <div >
          <el-link :disabled="!$permission('/workspace/folder')" :class="$permission('/workspace/folder')?'':'jinzhi'"  @click.native="handleClick(scope.row)" type="primary">
            <span v-if="scope.row.innerFile">{{ scope.row.innerFile }}- </span>
            <span>{{ scope.row.name }}</span>
          </el-link>
        </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="reportNum"
        label="报告数量"
        align="center"
        min-width="80px"
      >
      <template slot-scope="scope">
        <div>
            <el-link type="primary">{{ scope.row.reportNum }}</el-link>
        </div>
      </template>
      </el-table-column>
      <!-- <el-table-column
        prop="reportNum"
        label="报告文档数量"
        align="center"
        min-width="80px"
      >
      <template slot-scope="scope">
        <div>
            <el-link type="primary">{{ scope.row.reportNum }}</el-link>
        </div>
      </template>
      </el-table-column> -->
      <el-table-column
        prop="event"
        label="事件"
        align="center"
        min-width="80px"
      >
      <template slot-scope="scope">
        <div>
            <el-link type="primary" @click="checkVisual(scope.row.scenarioName,121,'事件一')">事件一</el-link>
        </div>
      </template>
      </el-table-column>
      <el-table-column v-for="column in columnList" :label="column.name" :sortable="column.name=='委托方'?false:'custom'" :prop="column.key" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="column.key === 'createTime'">{{ $d(scope.row.createTime) }}</span>
          <template v-else-if="column.key === 'scenarioid'">
            <span v-for="item in scope.row.scenarioName" :key="item" @click="checkVisual([item])" class="cursor_pointer">{{ item }} <br> </span>
          </template>
          <template v-else-if="column.key === 'typeid'">
            <span v-for="item in scope.row.typeName">{{ item }} <br> </span>
          </template>
          <span v-else>{{ scope.row[column.key] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="130px">
        <template slot-scope="scope">
          <el-dropdown trigger="click" @command="handleCommand($event, scope.row)" split-button :hide-on-click="false" type="primary" size="small"  @click="handleEdit(scope.row)">
            <p v-if="$permission('/workspace/project/modify') && ($r(scope.row.id,[2])||userinfo.id == scope.row.createBy)">编辑</p>
            <p v-else>查看</p>
            <el-dropdown-menu slot="dropdown" class="text-align_center">
              <el-dropdown-item  :disabled="!($permission('/workspace/project/patent_import')&& $r(scope.row.id,[1,2]))">
                <el-dropdown @command="handleCommand($event, scope.row)" trigger="hover" placement="right-start">
                    <span class="el-dropdown-link"> 专利导入 </span>
                    <el-dropdown-menu  class="children_item">
                        <el-dropdown-item command="7"> Excel导入 </el-dropdown-item>
                        <el-dropdown-item command="8">专利号导入 </el-dropdown-item>
                        <el-dropdown-item command="11" v-if="$permission('/workspace/common/retrieval')"> 检索条件导入 </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
              </el-dropdown-item>
              <el-dropdown-item command="12">任务清单</el-dropdown-item>
              <el-dropdown-item command="1">分配参与者</el-dropdown-item>
              <el-dropdown-item command="2" :disabled="!$permission('/workspace/project/report_manage')">报告管理</el-dropdown-item>
              <el-dropdown-item command="6" v-if="scope.row.isCategory == 1 && $permission('/workspace/product')">产品架构</el-dropdown-item>
              <el-dropdown-item command="10" v-if="$permission('/workspace/event')">事 件</el-dropdown-item>
              <el-dropdown-item command="5" :disabled="!$permission('/workspace/project/other_accessories')">其它附件</el-dropdown-item>
              <el-dropdown-item command="3" :disabled="!($permission('/workspace/project/project_share')&&$r(scope.row.id,[2]))">项目分享</el-dropdown-item>
              <el-dropdown-item command="4" divided class="color-red" :disabled="!($permission('/workspace/project/delete') && ($r(scope.row.id,[2])||userinfo.id == scope.row.createBy))">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { downLoad2, renderSize } from '@/utils'
import { workspaceOptions } from "./mixins";
import { mapGetters } from "vuex";
import { checkVisual } from "@/views/home/components/mixins";
export default {
  props: {
    params: Object,
    field: []
  },
  mixins: [workspaceOptions,checkVisual],
  data() {
    return {
      columnList: [],
      expands:[],
      reportList:[]
    }
  },
  watch: {
    field() {
      this.columnList = this.field.filter(item => !item.hidden)
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  mounted() {
    
    this.columnList = this.field.filter(item => !item.hidden)
  },
  methods: {
    //单元格点击
    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)
      }
    },
    //查询报告数量
    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
      })
    },
    handleEdit(row) {
      this.$emit('option', { option: 'e', row })
    },
    handleCommand(command, row) {
      this.$emit('option', { option: command, row })
    },
    sortChange({ column, prop, order }) {
      if (!order) {
        this.sortData('create_time', 'desc')
        return false
      }
      const o = {
        'descending': 'desc',
        'ascending': 'asc',
      }
      let key
      switch (prop) {
        case 'createTime':
          key = 'create_time'
          break
        case 'clientName':
          key = 'clientid'
          break
        case 'technicalTheme':
          key = 'technical_theme'
          break
        case 'createName':
          key = 'creat_id'
          break
        case 'contractNo':
          key = 'contract_no'
          break
        case 'caseDate':
          key = 'case'
          break
        case 'personnelName':
          key = 'personnel_id'
          break
        case 'departmentName':
          key = 'department_id'
          break
        default:
          key = prop
      }
      this.sortData(key, o[order])
    },
    sortData(prop, order) {
      this.$emit('sort', { prop, order })
    },
    t(key) {
      return this.field.filter(item => !item.hidden).map(item => item.key).indexOf(key) !== -1
    }
  }
}
</script>
<style lang="scss">

   .jinzhi{
    cursor: not-allowed;
    pointer-events:none;
  }
.common-project-table {
  padding-bottom: 10px;
  .el-card__body {
    padding: 10px;
  }
  .el-dropdown-link {
    cursor: pointer;
  }
  .edit {
    color: #1e9fff;
    cursor: pointer;
  }
}
</style>
