<template>
  <div class="main-content m20">
    <!--头部-查询-->
    <div class="content-header">
      <searchHeader title="查询" />
      <div class="body search-body">
        <el-row class="mb10">
          <el-col :span="12">
            <label class="label-name">统一社会信用代码/注册号：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.searchCode" size="mini" clearable type="text" class="search-input" placeholder="请输入统一社会信用代码/注册号" @keyup.enter.native="search" />
            </div>
            <!-- <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-row class="mb10">
              <label class="label-name" style="width: 80px">锁定日期：</label>
              <el-date-picker
                v-model="value2"
                size="mini"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
              />
            </el-row> -->
          </el-col>
          <el-col :span="12">
            <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>

            <!-- <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 v-if="showMore" class="mb10">
          <el-col :span="12">
            <label class="label-name">法定代表人：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.representative" size="mini" clearable 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-select
                v-model="searchForm.measure"
                size="mini"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in punishmeasures"
                  :key="item.measureId"
                  :label="item.measure"
                  :value="item.measure"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>-->
        <el-row v-if="showMore" class="mb10">
          <el-col :span="12">
            <label class="label-name">锁定原因：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.lockreason" size="mini" clearable 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-select
                v-model="searchForm.lockdepartment"
                size="mini"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in departmentList"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row v-if="showMore" class="mb10">
          <el-col :span="12">
            <label class="label-name">锁定/解锁人员：</label>
            <div class="search-input-box">
              <el-input v-model="searchForm.lockperson" size="mini" clearable 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-select
                v-model="searchForm.punishstatus"
                size="mini"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row v-if="showMore" class="mb10">
          <el-col :span="12">
            <label class="label-name">锁定日期：</label>
            <div class="search-input-box">
              <el-date-picker
                v-model="lockRangeTime"
                size="mini"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </div>
          </el-col>
          <el-col :span="12">
            <label class="label-name">解锁日期：</label>
            <div class="search-input-box">
              <el-date-picker
                v-model="unlockRangeTime"
                size="mini"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </div>
          </el-col>
        </el-row>
        <el-row v-if="showMore" class="mb10">
          <el-col :span="12">
            <label class="label-name">锁定/解锁方式：</label>
            <div class="search-input-box">
              <el-select
                v-model="searchForm.method"
                size="mini"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in List"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="1" :span="6">
            <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 type="primary" size="mini" @click="search">查询</el-button>
              <el-button size="mini" @click="resetSearchForm">重置</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">
            <template slot-scope="scope">
              <el-link type="primary" @click="rowClick(scope.row)">{{ scope.row.uscc }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="entname" :show-overflow-tooltip="true" label="主体名称" min-width="250" align="left" />
          <!--<el-table-column prop="locktype" :show-overflow-tooltip="true" label="锁定类型" min-width="80" align="left" :formatter="lockTypeFormat" />-->
          <!-- <el-table-column :show-overflow-tooltip="true" label="锁定状态" min-width="80" align="left" /> -->
          <el-table-column prop="method" :show-overflow-tooltip="true" label="锁定/解锁方式" min-width="100" align="left" />
          <el-table-column prop="lockreason" :show-overflow-tooltip="true" label="锁定原因" min-width="200" align="left" />
          <el-table-column prop="punishfrom" :show-overflow-tooltip="true" label="惩戒来源" min-width="100" align="left" :formatter="punishFromFormat" />
          <el-table-column :show-overflow-tooltip="true" label="锁定/解锁人员" min-width="100" align="left">
            <template v-if="scope.row.state==='0'" slot-scope="scope">{{ scope.row.lockusername }}</template>
            <template v-if="scope.row.state==='3'" slot-scope="scope">{{ scope.row.unlusername }}</template>
          </el-table-column>
          <el-table-column prop="lockTime" :show-overflow-tooltip="true" label="锁定时间" min-width="150" align="left" :formatter="lockTimeFormat" />
          <!--<el-table-column prop="unlockreason" :show-overflow-tooltip="true" label="解锁原因" min-width="190" align="left" />-->
          <el-table-column prop="unlockTime" :show-overflow-tooltip="true" label="解锁时间" min-width="150" align="left" :formatter="unlockTimeFormat" />
          <el-table-column prop="state" :show-overflow-tooltip="true" label="惩戒状态" min-width="120" align="left" :formatter="stateFormat" />
          <el-table-column label="操作" min-width="100" align="left">
            <template slot-scope="scope">
              <el-link type="primary" @click=" detail(scope.row)">查看</el-link>
            </template>
          </el-table-column>
          <!-- <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 size="mini" type="primary" @click="exportList">下载</el-button>
            </el-col>
            <!-- <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-ol>*/
             -->
            <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 { getPunishFromList, saveUnlockAccept, entlockListPage, getLockTypeList, getDepartmentList, queryAllPunishMeasures, getulockspecificreason, entLockExcel, toDetail } from '@/api/punishment'
export default {
  components: {
    pageSelector: pageSelector,
    searchHeader
  },
  data() {
    return {
      showMore: '',
      beginUnlockDate: '',
      endUnlockDate: '',
      endLockDate: '',
      beginLockDate: '',
      lockRangeTime: [],
      unlockRangeTime: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          ext: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 300 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      uLockSpecificReasons: [],
      punishmeasures: [],
      departmentList: [],
      punishFromList: [],
      lockTypeOptions: [],
      selection: [],
      selectionColumn: [{ label: '序号', type: 'index' }, { label: '统一社会信用代码/注册号', prop: 'uscc' }, { label: '主体名称', prop: 'entname' }],
      searchForm: {
        representative: '',
        uLSRCode: '',
        measure: '',
        searchUscc: '',
        serchName: '',
        beginUnlockDate: '',
        endUnlockDate: '',
        endLockDate: '',
        beginLockDate: '',
        lockreason: '',
        lockdepartment: '',
        lockperson: '',
        punishstatus: '',
        method: ''
      },
      loading: false,
      tableData: [],
      pageParam: {
        customParamMap: {},
        curPage: 1,
        total: 0,
        pageSize: 10
      },
      List: [{ value: '0', label: '个案' }, { value: '1', label: '自动' }],
      statusList: [{ value: '0', label: '解锁' }, { value: '1', label: '锁定' }]
    }
  },
  watch: {
    lockRangeTime() {
      if (this.$util.isNotEmpty(this.lockRangeTime)) {
        this.searchForm.beginLockDate = this.lockRangeTime[0]
        this.searchForm.endLockDate = this.lockRangeTime[1]
      } else {
        this.searchForm.beginLockDate = null
        this.searchForm.endLockDate = null
      }
    },
    unlockRangeTime() {
      if (this.$util.isNotEmpty(this.unlockRangeTime)) {
        this.searchForm.beginUnlockDate = this.unlockRangeTime[0]
        this.searchForm.endUnlockDate = this.unlockRangeTime[1]
      } else {
        this.searchForm.beginUnlockDate = null
        this.searchForm.endUnlockDate = null
      }
    }

  },
  mounted() {
    // 加载惩戒措施
    queryAllPunishMeasures().then(res => {
      this.punishmeasures = res.data
    })
    this.uLockSpecificReasons = getulockspecificreason()
    // 加载锁定类型
    this.lockTypeOptions = getLockTypeList()
    // 加载惩戒来源
    this.punishFromList = getPunishFromList()
    // 加载认定部门
    this.departmentList = getDepartmentList()
    this.loadPage()
  },
  methods: {
    showMoreCriteria() {
      this.showMore = !this.showMore
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      }
    },
    // 重置搜索表单
    resetSearchForm() {
      this.searchForm = {
        searchUscc: '',
        searchName: '',
        searchDepart: ''
      }
      this.unlockRangeTime = []
      this.lockRangeTime = []
    },
    rowClick(row, column, event) {
      this.$router.push({ path: '/punish/oneDetails', query: { data: row }})
    },
    detail(row) {
      toDetail(row).then(res => {
        if (row.state === '0') {
          this.$router.push({ path: '/punish/inclusion/info', query: { bizseq: res.data.bizseqid, workflowId: res.data.workflowid, tasklistid: res.data.tasklistid }})
        } else if (row.state === '3') {
          this.$router.push({
            path: '/punish/remove/info',
            query: { bizseq: res.data.bizseqid, workflowId: res.data.workflowid, tasklistid: res.data.tasklistid }
          })
        }
      })
    },
    exportList() {
      this.pageParam.customParamMap = this.searchForm
      entLockExcel(this.pageParam).then(res => {
        this.download2(res, '联合惩戒数据.xls')
      }).catch(req => {
        console.log('下载失败', req)
      })
    },
    download2(data, fileName) {
      if (!data) {
        return
      }
      console.log(data)
      const url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel;charset=utf-8' }))
      // window.open(url)
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      // document.body.appendChild(link)
      link.click()
    },
    unlockTimeFormat(row) {
      if (this.$util.isEmpty(row.unlockTime)) {
        return ''
      } else {
        return this.$util.formatDate.format(new Date(Date.parse(row.unlockTime)), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    lockTimeFormat(row) {
      if (this.$util.isEmpty(row.lockTime)) {
        return ''
      } else {
        return this.$util.formatDate.format(new Date(Date.parse(row.lockTime)), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    lockTypeFormat(row) {
      let name = ''
      this.lockTypeOptions.forEach(each => {
        if (each.value === row.locktype) {
          name = each.label
        }
      })
      return name
    },
    punishFromFormat(row) {
      let name = ''
      this.punishFromList.forEach(each => {
        if (each.value === row.punishFrom) {
          name = each.label
        }
      })
      return name
    },
    departmentFormat(row) {
      return this.$util.enumFormatter(this.departmentList, row.department)
    },
    stateFormat(row) {
      let name = ''
      switch (row.state) {
        case '0': name = '锁定'
          break
        case '3': name = '解锁'
      }
      return name
    },
    // 启动待办
    startTask() {
      if (this.selection.length === 0) return this.$message.error('请至少选择一个企业以开启待办')
      this.$confirm('是否确认启动待办', '提示').then(() => {
        let ids = ''
        this.selection.forEach(each => {
          ids += (each.elid + ';')
        })
        saveUnlockAccept(ids.substring(0, ids.length - 1)).then(res => {
          if (res.code === 0) {
            this.$router.push({ path: '/punish/remove/accept', query: { tasklistid: res.data.tasklistid, bizseq: res.data.bizseq, workflowId: res.data.workflowId }})
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    // 搜索
    search() {
      this.pageParam.customParamMap = this.searchForm
      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
      entlockListPage(this.pageParam).then(res => {
        debugger
        this.loading = false
        const list = []
        let i = 1
        const pageStart = (this.pageParam.curPage - 1) * this.pageParam.pageSize
        res.data.records.forEach(each => {
          debugger
          each.index = pageStart + i++
          each.uscc = each.uniscid || each.regno

          if (each.lockusername === '信用风险监管处') {
            each.method = '自动'
          } else { each.method = '个案' }
          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: 10px;
  .content-header{
    background: white;
    .search-body{
      /deep/ .el-date-editor *{
        font-size: 16px!important;
      }
      /deep/ .el-date-editor .el-range-separator{
        width: 8%!important;
      }
    }
    .label-name {
      text-align: right;
      display: inline-block;
      width: 220px;
      padding: 0 10px;
      font-size: $table-content-font-size;
    }
    .search-input-box{
      display: inline-block;
      width: calc(100% - 220px);
      & > *{
        width: 100%!important;
      }
    }
    .el-select{

    }
    .custom{
      width: calc(100% - 220px);
    }
    .search-input{
      cursor: pointer;
    }
    .body{
      padding: 20px 0 ;
      border-top:none;
      margin-bottom: 40PX;
    }
  }
  .content-body{
    background:white;
  }
  .header{
    height:50PX;
    padding:15PX 10PX;
    .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>
