<template>
  <div class="main-content  m20">
    <DetailHead :result="headName" />
    <div>
      <!--body-查询结果-->
      <div class="content-body">
        <div class="body">
          <el-table
            :key="type"
            v-loading="loading"
            style="width:95%;margin:0  auto"
            :header-cell-style="{background:'#8cc3fb',color:'#fff'}"
            :data="list"
            tooltip-effect="dark"
            :fit="true"
            @selection-change="select"
          >
            <el-table-column v-if="type==='draft'" class="no" type="selection" min-width="25" align="center" :selectable="selectable" />
            <el-table-column
              prop="index"
              label="序号"
              width="60"
              align="center"
              type="index"
              :index="(index)=>{ return size * (current - 1) + index + 1}"
            />
            <el-table-column prop="" label="企业名称" min-width="120" align="center">
              <template slot-scope="scope">
                {{ scope.row.entName ||scope.row.entname }}
              </template>
            </el-table-column>
            <el-table-column prop="uniscid" label="统一码/注册号" width="200" align="center" />
            <el-table-column prop="punishFrom" label="惩戒来源" min-width="120" align="center" :formatter="punishFromFormat" />
            <!--<el-table-column prop="department" label="惩戒部门" min-width="120" align="center" :formatter="departmentFormat" />-->
<!--            <el-table-column prop="entname" label="企业名称" width="100" align="center" />-->
            <el-table-column prop="representative" label="法定代表人" width="100" align="center" />
            <el-table-column prop="state||regstate" label="惩戒状态" width="100" align="center" :formatter="statusFormat" />
              <el-table-column prop="unlockTime" label="解锁日期" min-width="120" align="center" :formatter="unlockTimeFormat" />
            <el-table-column prop="lockcount" label="被锁定次数" width="100" align="center" />
            <el-table-column v-if="type === 'locked'||'handled'" label="锁定时间" min-width="120" align="center">
              <template slot-scope="scope">
                {{ $moment(scope.row.lockTime).format('YYYY-MM-DD') }}
              </template>
            </el-table-column>
            <el-table-column v-if="type === 'unlocked'"  label="解锁时间" min-width="120" align="center">
              <template slot-scope="scope">
                {{ $moment(scope.row.unlockTime).format('YYYY-MM-DD') }}
              </template>
            </el-table-column>
            <el-table-column v-if="type==='draft'" prop="insertTime" label="录入时间" min-width="120" align="center">
              <template slot-scope="scope">
                {{ $moment(scope.row.insertTime).format('YYYY-MM-DD') }}
              </template>
            </el-table-column>
          </el-table>
          <br>
          <br>
          <el-row>
            <el-col>
              <el-button v-if="type==='draft'" size="mini" type="primary" @click="startTask">发起锁定</el-button>
            </el-col>
            <el-col style="text-align: right">
              <el-pagination
                :disabled="loading"
                :page-size="size"
                :total="total"
                :current-page.sync="current"
                :page-sizes="[10, 20, 30, 40,50,100,200]"
                layout="slot,total, sizes, prev, pager, next, jumper"
                @size-change="pageSizeChange"
                @current-change="pageIndexChange"
              />
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
  </div>

</template>

<script>
import {
  getDepartmentList,
  getPunishFromList,
  listTolockentlist,
  listEntlock,
  saveLockAccept,
  getStatusList
} from '@/api/punishment'
import DetailHead from '@/components/DetailHead'

export default {
  components: {
    DetailHead
  },
  data() {
    return {
      loading: false,
      list: [],
      type: '',
      departId: '',
      headName: '',
      countlist: [],
      departmentList: getDepartmentList(),
      punishFromList: getPunishFromList(),
      statusList: [],
      size: 10,
      current: 1,
      total: undefined,
      states: [],
      selection: [],
      beginDate: '',
      // endLockDate: '',
      // beginUnlockDate: '',
      endDate: ''
    }
  },
  mounted() {
    this.type = this.$route.query.type
    this.departId = this.$route.query.departId
    this.beginDate = this.$route.query.time.beginDate
    // this.endLockDate = this.$route.query.time.endLockDate,
    // this.beginUnlockDate = this.$route.query.time.beginUnlockDate,
    this.endDate = this.$route.query.time.endDate
    this.statusList = getStatusList()
    this.init()
  },
  methods: {
    selectable(row, index) {
      // 未在办才可以勾选发起在办
      return row.state === '0'
    },
    select(selection, row) {
      this.selection = selection
    },
    // 当前分页改变
    pageIndexChange(curPage) {
      this.current = curPage
      this.init()
    },
    // 分页大小改变
    pageSizeChange(pageSize) {
      this.size = pageSize
      this.init()
    },
    lockTimeFormat(row) {
      if (this.$util.isEmpty(row.insertTime)) {
        return ''
      } else {
        // return this.$util.formatDate.format(new Date(Date.parse(row.insertTime)), 'yyyy-MM-dd hh:mm:ss')
        return this.$util.formatDate.format(new Date(Date.parse(row.insertTime)), 'yyyy-MM-dd')
      }
    },
    unlockTimeFormat(row) {
      if (this.$util.isEmpty(row.unlockTime)) {
        return ''
      } else {
        // return this.$util.formatDate.format(new Date(Date.parse(row.insertTime)), 'yyyy-MM-dd hh:mm:ss')
        return this.$util.formatDate.format(new Date(Date.parse(row.unlockTime)), 'yyyy-MM-dd')
      }
    },
    statusFormat(row) {
      let name = ''
      if (row.elid !== null) {
        this.statusList.forEach(each => {
          if (each.value === row.state) {
            name = each.label
          }
        })
      } else {
        if (row.regstate === '0') {
          name = '解锁'
        } else {
          name = '锁定'
        }
      }

      return name
    },
    // 惩戒来源格式化
    punishFromFormat(row) {
      let name = ''
      this.punishFromList.forEach(each => {
        if (each.value === row.punishFrom) {
          name = each.label
        }
      })
      return name
    },
    // 启动锁定待办
    startTask() {
      if (this.selection.length === 0) return this.$message.error('请至少选择一个企业以开启待办')
      const punishFromSet = new Set()
      for (const item of this.selection) {
        punishFromSet.add(item.punishFrom)
      }
      if (punishFromSet.size > 1) {
        this.$message.warning('已选主体不属于同个惩戒来源，请检查！')
        return
      }
      this.$confirm('是否确认启动待办', '提示').then(() => {
        let ids = ''
        this.selection.forEach(each => {
          ids += (each.pripid + ';')
        })
        saveLockAccept(ids.substring(0, ids.length - 1), Array.from(punishFromSet)[0]).then(res => {
          if (res.code === 0) {
            this.$router.push({ path: '/punish/inclusion/accept', query: { tasklistid: res.data.tasklistid, bizseq: res.data.bizseq, workflowId: res.data.workflowId }})
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    init() {
      // 标题名称
      if (this.type === 'draft') {
        this.headName = '拟锁定企业列表'
        this.states = ['0', '1']
        // 查询拟锁定列表接口
        this.findListTolockent()
      } else if (this.type === 'locked') {
        this.headName = '已锁定企业信息'
        this.states = ['0', '2', '3']
        // this.states = ['0', '1']
        this.findListEntlock()
      } else if (this.type === 'autolocked') {
        this.headName = '已自动锁定企业信息'
        this.states = ['0', '2', '3']
        // this.states = ['0', '1']
        this.findListEntlock()
      } else if (this.type === 'manuallocked') {
        this.headName = '已个案锁定企业信息'
        this.states = ['0', '2', '3']
        // this.states = ['0', '1']
        this.findListEntlock()
      } else if (this.type === 'unlocked') {
        this.headName = '已解锁企业信息'
        this.states = ['3']
        this.findListEntlock()
      } else if (this.type === 'autounlocked') {
        this.headName = '已自动解锁企业信息'
        this.states = ['3']
        this.findListEntlock()
      } else if (this.type === 'manualunlocked') {
        this.headName = '已个案解锁企业信息'
        this.states = ['3']
        this.findListEntlock()
      } else if (this.type === 'handled') {
        this.headName = '历史锁定企业信息'
        this.findListEntlock()
      }
      this.headName = this.departmentFormat({ department: this.departId }) + ' - ' + this.headName
    },
    // 部门格式化
    departmentFormat(row) {
      return this.$util.enumFormatter(this.departmentList, row.department)
    },
    findListTolockent() {
      const data = {
        size: this.size,
        current: this.current,
        department: this.departId,
        states: this.states
      }
      this.loading = true
      listTolockentlist(data).then(res => {

        this.list = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },
    findListEntlock() {
      const data = {
        type: this.type,
        size: this.size,
        current: this.current,
        department: this.departId,
        states: this.states,
        beginDate: this.beginDate,
        // endLockDate: this.endLockDate,
        // beginUnlockDate: this.beginUnlockDate,
        endDate: this.endDate
      }
      this.loading = true
      listEntlock(data).then(res => {
        // this.countlist = res.data.count
        this.list = res.data.records
        // this.list.forEach(each => {
        //   for (var count in this.countlist) {
        //     for (var key in count) {
        //       if( key === 'entname') {
        //         if(count[key] ===   each.entname) {
        //           each.count = count['count(*)']
        //         }
        //       }
        //     }
        //   }
        // })
        this.total = res.data.total
        this.loading = false
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.main-content{
  background: #fff;
  padding: 10px;
  .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);
      .el-select{
        width: 100%;
      }

    }
    .search-input{
      cursor: pointer;
    }
    .body{
      padding: 20px 0 ;
      border-top:none;
      margin-bottom: 40PX;
    }
  }
  .el-select{
    width: 70%;
  }
  .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;
    }
  }
}
/deep/.el-input__inner{
  height: 28px;
}
/deep/.label-name{
  text-align: right;
}
/deep/.el-input__icon{
  line-height: 28px;
}
.content-header{
  border: none;
}
</style>
