<template>
  <div class="main-content m20">
    <!--头部-查询-->
    <div class="content-header">
      <searchHeader title="查询" />
      <div class="body search-body">
        <el-row>
          <el-col :span="12">
            <el-row class="mb10">
              <label class="label-name" style="width: 220px">统一社会信用代码/注册号：</label>
              <div class="search-input-box" style="width: calc(100% - 220px)">
                <el-input v-model="searchForm.searchCode" size="mini" clearable type="text" class="search-input" placeholder="请输入统一社会信用代码/注册号" @keyup.enter.native="search" />
              </div>
            </el-row>
            <!-- <el-row>
              <label class="label-name" style="width: 190px">名称（模糊）：</label>
              <div class="search-input-box" style="width: calc(100% - 200px)">
                <el-input v-model="searchForm.searchEntName" size="mini" clearable type="text" class="search-input" placeholder="请输入名称（模糊）" @keyup.enter.native="search" />
              </div>
            </el-row> -->
          </el-col>
          <el-col :span="12">
            <el-row class="mb10">
              <label class="label-name">主体名称（模糊）：</label>
              <div class="search-input-box">
                <el-input v-model="searchForm.searchEntName" size="mini" clearable type="text" class="search-input" placeholder="请输入名称（模糊）" @keyup.enter.native="search" />
              </div>
              <!-- <label class="label-name" style="width: 80px">锁定日期：</label>
              <el-date-picker
                v-model="searchForm.searchLockDate"
                size="mini"
                type="date"
                placeholder="选择日期"
                class="custom"
              /> -->
            </el-row>
            <!-- <el-row>
              <label class="label-name" style="width: 80px">惩戒来源：</label>
              <el-select v-model="searchForm.searchPunishFrom" size="mini" filterable clearable placeholder="请选择" class="custom">
                <el-option
                  v-for="item in punishFromList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-row> -->
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <!-- <label class="label-name" style="width: 80px">锁定类型：</label>
            <el-select v-model="searchForm.searchLockType" size="mini" filterable clearable placeholder="请选择" style="width: calc(100% - 100px);">
              <el-option
                v-for="item in lockTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              /> -->
            <!-- </el-select> -->
            <label class="label-name">文书号（模糊）：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.searchWritsNo" size="mini" clearable type="text" class="search-input" placeholder="请输入文书号（模糊）" @keyup.enter.native="search" />
            </div>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="4" :offset="1">
            <div class="btn-box">
              <el-button size="mini" type="primary" @click="search" :loading="loading">查询</el-button>
              <el-button size="mini" @click="searchForm.searchWritsNo='';searchForm.searchCode = '';searchForm.searchEntName='';">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!--body-查询结果-->
    <div class="content-body">
      <div class="body">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 95%;margin: 0 auto"
          :fit="true"
          :row-class-name="tableRowClassName"
          :header-cell-style="{background:'#8cc3fb',color:'#fff'}"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column class="no" type="selection" min-width="25" align="center" /> -->
          <el-table-column prop="index" label="序号" min-width="40" align="center" />
          <el-table-column prop="uscc" label="统一社会信用代码/注册号" min-width="140" align="left" />
          <el-table-column prop="entname" :show-overflow-tooltip="true" label="主体名称" min-width="200" align="left" />
          <el-table-column prop="writsno" :show-overflow-tooltip="true" label="文书号" min-width="200" align="left" :formatter="lockTypeFormat">
            <template slot-scope="scope">
              <!-- <el-link :href="scope.row.url" target="_blank">{{ scope.row.writsno }}</el-link> -->
              <el-link @click="toOnlinePdf(scope.row)">{{ scope.row.writsno }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="createtime" :show-overflow-tooltip="true" label="创建时间" min-width="90" align="left" :formatter="createTimeFormat" />
          <!-- <el-table-column label="操作" min-width="100" align="left">
            <template slot-scope="scope">
              <i style="margin-right: 5px" class="el-icon-view icon" />
              <i style="margin-right: 5px" class="el-icon-edit icon" />
              <i class="el-icon-lock icon" />
            </template>
          </el-table-column> -->
        </el-table>
        <div class="page-box">
          <el-row>
            <!-- <el-col :span="6">
              <el-button @click="selectAll">全选</el-button>
              <el-button @click="$util.toggleSelect($refs.multipleTable)">反选</el-button>
              <el-button type="primary" @click="startTask">办理</el-button>
            </el-col> -->
            <el-col :span="18" :offset="6">
              <el-pagination
                :disabled="loading"
                :page-size="pageParam.pageSize"
                :total="pageParam.total"
                :page-sizes="[10, 20, 30, 40,50,100,200]"
                layout="slot,total, sizes, prev, pager, next, jumper"
                @size-change="pageSizeChange"
                @current-change="pageIndexChange"
              >
                <!-- <batchFunction :options="options" style="float:left" /> -->
                <span>
                  第 {{ pageParam.curPage }} 页 /
                  共 {{ (pageParam.total !== 0 ? parseInt((pageParam.total + pageParam.pageSize - 1)/pageParam.pageSize) : 1) }} 页
                </span>
              </el-pagination>
            </el-col>
          </el-row>
        </div>
        <!--分页多选组件-->
        <pageSelector :selection="selection" :columns="selectionColumn" @removeCallback="removeCallback" />
      </div>
    </div>
  </div>
</template>

<script>
import searchHeader from '@/components/searchHeader'
import pageSelector from '@/components/PageSelector'
import { getEntWritsList } from '@/api/writs'
import { word2Pdf } from '@/api/system'

export default {
  components: {
    pageSelector: pageSelector,
    searchHeader
  },
  data() {
    return {
      punishFromList: [],
      lockTypeOptions: [],
      selection: [],
      selectionColumn: [{ label: '序号', type: 'index' }, { label: '统一社会信用代码/注册号', prop: 'uscc' }, { label: '主体名称', prop: 'entname' }],
      searchForm: {
        searchCode: '',
        searchUscc: '',
        searchName: ''
      },
      loading: false,
      tableData: [],
      pageParam: {
        customParamMap: {},
        curPage: 1,
        total: 0,
        pageSize: 10
      },
      exportQueryData: {}, // 导出excel的查询条件, 用于导出excel和分页, 每轮查询成功后赋值
    }
  },
  mounted() {
    // this.loadPage()
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      }
    },
    // pdf格式在线查看
    toOnlinePdf(row) {
      // row.url = `${process.env.VUE_APP_DOWNLOAD_URL}/` + row.url
      word2Pdf(this.$util.isEmpty(row.path) ? '' : row.path, row.bizseq, row.writstype).then(res => {
        if (res.code === 0) {
          window.open(`${process.env.VUE_APP_DOWNLOAD_URL}/` + res.data, '_blank')
        }
      })
    },
    createTimeFormat(row) {
      if (this.$util.isEmpty(row.createtime)) {
        return ''
      } else {
        return this.$util.formatDate.format(new Date(Date.parse(row.createtime)), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    // 搜索
    search() {
      this.pageParam.customParamMap = this.searchForm
      // 判断是否填写了条件
      let flag = false
      // obj的遍历
      Object.keys(this.pageParam.customParamMap).forEach(key => {
        if (this.pageParam.customParamMap[key] !== '' && this.pageParam.customParamMap[key] !== null) {
          flag = true
        }
      })
      if (!flag) {
        this.$message({
          message: '请至少填写一个查询条件！',
          type: 'warning'
        })
        return
      }
      this.exportQueryData = null
      this.pageParam.curPage = 1
      this.loadPage()
    },
    // 分页多选插件回调
    removeCallback(index, row) {
      this.$refs.multipleTable.toggleRowSelection(row, false)
    },
    // 全选按钮事件
    selectAll() {
      const table = this.$refs.multipleTable
      for (const i in table.data) {
        const each = table.data[i]
        table.toggleRowSelection(each, true)
      }
    },
    // 选项变更事件
    handleSelectionChange(selectData) {
      const cancel = []
      if (selectData.length !== this.tableData.length) {
        // 找出没有选中的内容
        this.tableData.forEach(each => {
          for (const i in selectData) {
            const atom = selectData[i]
            if (atom.pripid === each.pripid) {
              return
            }
          }
          cancel.push(each)
        })
      }
      // 取消选中的内容
      for (const i in cancel) {
        const each = cancel[i]
        let index
        for (const j in this.selection) {
          const select = this.selection[j]
          if (select.pripid === each.pripid) {
            index = parseInt(j)
            break
          }
        }
        if (index !== undefined) {
          this.selection.splice(index, 1)
        }
      }
      // 增加选中内容
      for (const i in selectData) {
        const each = selectData[i]
        let exist = false
        for (const j in this.selection) {
          const select = this.selection[j]
          if (select.pripid === each.pripid) {
            exist = true
            break
          }
        }
        if (!exist) {
          this.selection.push(each)
        }
      }
    },
    // 当前分页改变
    pageIndexChange(curPage) {
      this.pageParam.curPage = curPage
      this.loadPage()
    },
    // 分页大小改变
    pageSizeChange(pageSize) {
      this.pageParam.pageSize = pageSize
      this.loadPage()
    },
    // 加载分页
    loadPage() {
      if (this.loading) return
      let data = {}
      if(this.exportQueryData && JSON.stringify(this.exportQueryData) != "{}"){
        this.exportQueryData.curPage = this.pageParam.curPage
        this.exportQueryData.pageSize = this.pageParam.pageSize
        data = JSON.parse(JSON.stringify(this.exportQueryData))
      }else{
        data = JSON.parse(JSON.stringify(this.pageParam))
      }
      this.loading = true
      getEntWritsList(data).then(res => {
        this.loading = false
        this.exportQueryData = JSON.parse(JSON.stringify(data))
        const list = []
        let i = 1
        const pageStart = (this.pageParam.curPage - 1) * this.pageParam.pageSize
        res.data.records.forEach(each => {
          each.index = pageStart + i++
          each.uscc = each.uniscid || each.regno
          each.url = `${process.env.VUE_APP_DOWNLOAD_URL}/` + each.path
          list.push(each)
        })
        this.tableData = list
        this.pageParam.total = res.data.total
        const selection = JSON.parse(JSON.stringify(this.selection))
        this.$nextTick(() => {
          for (const i in list) {
            const each = list[i]
            for (const j in selection) {
              if (each.pripid === selection[j].pripid) {
                this.$refs.multipleTable.toggleRowSelection(each, true)
              }
            }
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.main-content{
  background: #fff;
  padding: 10pt;
  .content-header{
    background: white;
    .label-name {
      text-align: right;
      display: inline-block;
      width: 220px;
      font-size: $table-content-font-size;
    }
    .search-input-box{
      display: inline-block;
      width: calc(100% - 220px);
    }
    .custom{
      width: calc(100% - 220px);
    }
    .search-input{
      cursor: pointer;
    }
    .body{
      padding:10PX;
      border-top:none;
      margin-bottom: 40PX;
    }
  }
  .content-body{
    background:white;
  }
  .header{
    .title{
      font-weight: 600;
      font-size: 1.2rem;
    }
  }
    .icon{
    font-size: 50px;
    color: rgb(24, 144, 255);
  }
  .page-box{
    margin:10PX;
    .el-pagination{
      text-align: right;
    }
  }
  .mb10{
    margin-bottom: 10px;
  }
}
</style>
