<template>
  <div class="main-content m20">
    <!--头部-查询-->
    <div class="content-header">
      <div class="body search-body">
        <el-row style="margin-bottom:10px">
          <el-col :span="12">
            <label class="label-name" style="width: 220px">统一社会信用代码/注册号：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.searchUscc" clearable size="mini" type="text" class="search-input" placeholder="请输入统一社会信用代码/注册号" @keyup.enter.native="search" />
            </div>
          </el-col>
          <el-col :span="12">
            <label class="label-name">主体名称（模糊）：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.searchName" clearable size="mini" type="text" class="search-input" placeholder="请输入主体名称（模糊）" @keyup.enter.native="search" />
            </div>
          </el-col>
        </el-row>
        <el-row style="margin-bottom:10px">
          <el-col :span="12">
            <label class="label-name">受理号：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.acceptno" clearable size="mini" type="text" class="search-input" placeholder="请输入受理号" @keyup.enter.native="search" />
            </div>
          </el-col>
          <el-col :span="12">
            <label class="label-name" style="width: 220px">拟列入原因：</label>
            <el-select v-model="searchForm.serillrea" style="width: calc(100% - 220px)" clearable size="mini" placeholder="请选择拟列入原因" class="custom">
              <el-option
                v-for="item in reasonList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-row>
        <el-row style="margin-bottom:10px">
          <el-col :span="12">
            <label class="label-name">拟列入公示日期：</label>
            <div class="search-input-box">
              <el-date-picker
                v-model="searchForm.abntime"
                style="width:100%"
                size="small"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              />
            </div>
          </el-col>
          <el-col :span="11" :offset="1">
            <div class="btn-box">
              <!-- <span v-if="!showMore" style="margin-right:20px;color:#409EFF;cursor:pointer;font-size: 16px;" @click="showMoreCriteria">
                展开<i class="el-icon-d-arrow-right" />
              </span>
              <span v-if="showMore" style="margin-right:20px;color:#409EFF;cursor:pointer;font-size: 16px;" @click="showMoreCriteria">
                <i class="el-icon-d-arrow-left" />隐藏
              </span> -->
              <el-button size="mini" type="primary" @click="search">查询</el-button>
              <el-button size="mini" @click="searchForm={},pageParam.customParamMap={}">重置</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" width="30" />
          <el-table-column prop="index" label="序号" min-width="50" align="center" />
          <el-table-column prop="acceptno" label="拟列入业务受理号" min-width="150" align="left" />
          <el-table-column prop="uscc" label="统一社会信用代码/注册号" min-width="150" align="left" />

          <el-table-column prop="entname" :show-overflow-tooltip="true" label="主体名称" min-width="200" align="left" />
          <el-table-column prop="serillrea" :show-overflow-tooltip="true" label="拟列入原因" min-width="200" align="left" :formatter="reasonFormat" />
          <el-table-column prop="abntime" label="拟列入公示日期" min-width="100" align="left" />
          <!-- <el-table-column prop="dom" :show-overflow-tooltip="true" label="经营地址" min-width="200" align="left" /> -->
          <!-- <el-table-column prop="enttypeCn" :show-overflow-tooltip="true" label="主体类型" min-width="150" align="left" />
          <el-table-column prop="name" label="法定代表人" min-width="80" align="left" />
          <el-table-column prop="estdate" label="成立日期" min-width="120" align="left" /> -->
        </el-table>
        <div class="page-box">
          <el-row>
            <el-col :xs="24" :sm="24" :md="24" :lg="8" :xl="8">
              <el-button size="mini" @click="selectAll">全选</el-button>
              <el-button size="mini" @click="$util.toggleSelect($refs.multipleTable)">反选</el-button>
              <el-button size="mini" type="primary" @click="startTask">办理</el-button>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="16" :xl="16">
              <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>
      </div>
    </div>
  </div>
</template>

<script>
import { selectSerillegalPage, getNewCD73, startIncludeIllegal } from '@/api/illegal'
export default {
  data() {
    return {
      selection: [],
      selectionColumn: [{ label: '序号', type: 'index', minWidth: '30px' }, { label: '统一社会信用代码/注册号', prop: 'uniscid', minWidth: '100px' }, { label: '主体名称', prop: 'entname', minWidth: '100px' }, { label: '法定代表人', prop: 'name', minWidth: '50px' }],
      searchForm: {
        searchUscc: '',
        searchName: '',
        acceptno: '',
        serillrea: '',
        abntime: ''
      },
      loading: false,
      showFixed: false,
      showMore: false,
      tableData: [],
      pageParam: {
        customParamMap: {},
        likeParamMap: {},
        curPage: 1,
        total: 0,
        pageSize: 10
      },
      reasonList: []
    }
  },
  mounted() {
    // 加载企业列入原因
    getNewCD73().then(res => {
      if (res.code === 0) {
        const list = []
        res.data.forEach(each => {
          list.push({
            value: each.code,
            label: each.name
          })
        })
        this.reasonList = list
      }
    })
    this.loadPage()
    window.addEventListener('scroll', this.handleScroll, true)
  },
  methods: {
    showMoreCriteria() {
      this.showMore = !this.showMore
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      }
    },
    // 列入原因
    reasonFormat(row) {
      let name = ''
      this.reasonList.forEach(each => {
        if (each.value === row.serillrea) {
          name = each.label
        }
      })
      return name
    },
    handleScroll() {
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop
      if (scrollTop > 80) {
        this.showFixed = true
      } else {
        this.showFixed = false
      }
    },
    // 启动待办
    startTask() {
      if (this.selection.length === 0) return this.$message.error('请至少选择一个企业以开启待办')
      if (this.selection.length > 1) return this.$message.error('您选择了多条数据进行业务办理，该业务不能批量处理，请重新选择！')
      this.$confirm('是否确认启动待办', '提示').then(() => {
        const params = {
          pripid: this.selection[0].pripid,
          sourceType: '1',
          illoperatingid: this.selection[0].illoperatingid
        }
        startIncludeIllegal(params).then(res => {
          if (res.code === 0) {
            this.$router.push({ path: '/illegal/listed/accept', query: { tasklistid: res.data.tasklistid, bizseq: res.data.bizseqid, workflowId: res.data.workflowid }})
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    // 搜索
    search() {
      for (const key in this.searchForm) {
        this.pageParam.customParamMap[key] = this.searchForm[key]
      }
      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
      this.loading = true
      selectSerillegalPage(this.pageParam).then(res => {
        this.loading = false
        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.abntime = each.abntime ? this.$util.formatDate.format(new Date(Date.parse(each.abntime)), 'yyyy-MM-dd') : ''
          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: 0;
  .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);
      // width: 300px;
    }
    .search-input{
      cursor: pointer;

    }
    .body{
      padding: 20px 0 ;
      border-top:none;
      margin-bottom: 40PX;
    }
  }
  .content-body{
    background:white;
    margin-bottom: 100px;
  }
  .header{
    /* height:50PX;
    padding:15PX 10PX;
    border:1PX solid #eee; */
    .title{
      font-weight: 600;
      font-size: 1.2rem;
    }
  }
  .page-box{
    margin:10PX;
    .el-pagination{
      text-align: right;
    }
  }
}
.fixed-bottom {
  position: fixed;
  width: 100%;
  bottom: -10px;
  right: 0;
  z-index: 99;
  padding: 10px;
  border-radius: 5px 5px 0 0;
  background: linear-gradient(to bottom, #fff 0%,#8bbef6 100%);
  box-shadow:  1px -4px 4px rgba(0, 21, 41, 0.08);
}
.content-body{
  background: #fff;
  /deep/.el-table .warning-row {
    background: #eaf4fe!important;
  }
}
</style>
<style lang="scss">
.content-body .el-table-column--selection .cell{
  padding-right: 0px;
}
</style>
