<template>
<!-- 案卷弹框 -->
  <el-dialog ref="diaFrom" v-dialogDrag title="查看详情" :close-on-click-modal="false" :visible.sync="visible" width="80%"
    top="10px" :before-close="cancel">
    <div class="dia-box">
      <div v-for="(item, key) in Alltable" :key="key" class="wholeTable">
        <el-table highlight-current-row v-loading="item.tableLoading" size="mini" :data="item.tableData" fit border
          ref="tableForEach" :max-height="key==0?360:510" @row-click="editCurrentApplicationApproval($event, item, key)"
          tooltip-effect="dark">
          <el-table-column label="原文数量" width="90" align="center"
            v-if="(Alltable.length===3 && key===2)||(Alltable.length===1)||(Alltable.length===2)">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleContents(scope.row, item, key)">{{
                    scope.row.archive_file_num ? scope.row.archive_file_num : 0
                }}</el-button>
            </template>
          </el-table-column>
          <el-table-column sortable v-for="(ele, index) in JSON.parse(item.tableShow)" :key="index"
            :prop="ele.columnName" :min-width="ele.width" :label="ele.columnChnName" align="center"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="ele.columnName === 'fonds_no'">
                {{ (fondsList.filter(f => f.fondscode === scope.row[ele.columnName])[0] || {}).codeName }}
              </span>
              <span v-if="ele.columnName !== 'fonds_no'">{{ scope.row[ele.columnName] }}</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination v-if="item.hasOwnProperty('pagination')" @current-change="handleCurrentChange($event, item, key)"
          @size-change="handleSizeChange($event, item, key)" layout="total, prev, pager, next, sizes"
          :page-size="item.pagination.pageSize" :page-sizes="[10, 20, 30, 50]" :current-page="item.pagination.pageNum"
          :total="item.pagination.total">
        </el-pagination>
      </div>
      <!-- 原文弹窗 -->
      <drawer-tem ref="original" :archive_no="archive_no" :tableName="tableName" :treeRow="treeRow" :drawer='drawer'
        :actions='actions' :catalogueType='catalogueType' @handleClose="cancelDrawer" />
    </div>
  </el-dialog>
</template>
<script>
import DrawerTem from "@/components/DrawerTem";
import localStronge from "@/api/localData";
import { getOneTableAuth } from "@/api/archives-management/fileManagement/notStoredArchives";
export default {
  props: {
    visible: {
      type: Boolean,
      default: () => false,
    },
    Alltable: {
      type: Array,
      default: () => [],
    },
    fondsList: {
      type: Array,
      default: () => [],
    },
    curRow: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      treeRow: {},
      drawer: false,
      actions: "",
      catalogueType: false,
      tableRow: {}, //电子借阅记录行
      tableOneClickRow: {},
      tableTwoClickRow: {},
      archive_no: "",
      tableName: "",
    };
  },
  components: {
    DrawerTem,
  },
  methods: {
    cancel() {
      this.$emit("cancel");
    },
    cancelDrawer() {
      this.drawer = false;
    },
    clickSearch(key, start) {
      // 三层recID: '7,11', 7表示项目id,11表示案卷id;
      // 二层 recID: '8', 8表示案卷id
      let isThree = this.tableRow.recId.indexOf(",") > -1 ? true : false; // 三层
      this.Alltable.forEach((item, index) => {
        if (key === index) {
          let params = {
            commonMap: {
              funcTypeCode: "",
              tableName: item.tableName,
            },
            conditionMap: {},
            limit: this.Alltable[key].hasOwnProperty("pagination")
              ? this.Alltable[key].pagination.pageSize
              : "10",
            page: this.Alltable[key].hasOwnProperty("pagination")
              ? this.Alltable[key].pagination.pageNum
              : "1",
            // limit: item.pagination.pageSize,
            // page: item.pagination.pageNum,
          };
          if (isThree) {
            if (key === 0) {
              params.conditionMap.id = this.tableRow.recId.split(",")[0];
            }
            if (key === 1) {
              params.conditionMap.pid = this.tableOneClickRow.id;
              params.conditionMap.id = this.tableRow.recId.split(",")[1];
            }
            if (key === 2) {
              params.conditionMap.pid = this.tableTwoClickRow.id;
            }
          } else {
            if (key === 0) {
              params.conditionMap.id = this.tableRow.recId;
            }
            if (key === 1) {
              params.conditionMap.pid = this.tableOneClickRow.id;
            }
          }
          item.tableLoading = true;
          getOneTableAuth(params)
            .then((res) => {
              item.tableData = res.body.list;
              if (this.Alltable[key].hasOwnProperty("pagination")) {
                this.Alltable[key].pagination.total = res.body.total;
              }
              // item.pagination.total = res.body.total;
              if (start && key === 0) {
                // this.tableRow =
                this.tableOneClickRow = res.body.list[0];
                this.clickSearch(1, true);
              }
              if (start && key === 1) {
                // this.tableRow = res.body.list[0]
                this.tableTwoClickRow = res.body.list[0];
                this.clickSearch(2, true);
              }
            })
            .finally((err) => {
              item.tableLoading = false;
            });
        }
      });
    },
    //表格点击每行事件
    editCurrentApplicationApproval(val, item, key) {
      if (key === 0) {
        this.tableOneClickRow = val;
        if (this.Alltable.length === 3) {
          this.Alltable[2].tableData = [];
        }
      } else if (key === 1) {
        this.tableTwoClickRow = val;
      }
      this.clickSearch(key + 1);
    },
    //点击原文
    handleContents(val, item, key) {
      console.log(val, item);
      this.tableName = item.tableName;
      this.archive_no = val.folder_no;
      console.log(this.archive_no);
      this.treeRow = val;
      this.$refs.original.archiveNum = val.archive_file_num
        ? val.archive_file_num
        : 0;
      this.$refs.original.oriTable = item;
      this.$refs.original.orirow = val;
      this.$refs.original.orikey = key;
      this.$refs.original.oriRowObj = this.tableRow;
      let params = {
        commonMap: {
          funcTypeCode: "file",
          tableName: item.tableName,
        },
        conditionMap: {
          rec_id: val.id,
          file_status: 1,
        },
        orderBy: "order by  page_no  asc",
        limit: "1000",
        page: "1",
      };
      this.$refs.original.params = params;
      this.actions =
        this.prefix +
        "/point-strategy/v/ajjnOriginBatch/uploadSimpleFilesAnjuan?recId=" +
        val.id +
        "&fondscode=" +
        val.fonds_no +
        "&tableName=" +
        item.tableName;
      getOneTableAuth(params)
        .then((res) => {
          //原文表
          this.$refs.original.tableDataOrigin = res.body.list;
        })
        .then((res) => {
          params.conditionMap.file_status = 2;
          getOneTableAuth(params).then((res) => {
            //原文回收站表
            this.$refs.original.backData = res.body.list;
          });
        });
      this.$refs.original.picUrl = "";
      this.$refs.original.videoSrc = "";
      this.$refs.original.activeName = "1";
      this.drawer = true;
    },
    //每页换个数
    handleSizeChange(val, item, key) {
      // console.log(item)
      item.pagination.pageSize = val;
      item.pagination.pageNum = 1;
      this.clickSearch(key);
    },
    //   //换页
    handleCurrentChange(val, item, key) {
      item.pagination.pageNum = val;
      this.clickSearch(key);
    },
  },
};
</script>
<style lang="scss" scoped>
.dia-box {
  .wholeTable {
    margin-bottom: 15px;
  }
}
</style>
