<template>
  <div class="app-container">
    <el-form label-width="100px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="隐患描述">
            <el-input v-model="KEYWORDS" placeholder="请输入关键字"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患来源">
            <el-select v-model="SOURCE" placeholder="请选择" style="width: 100%;">
              <el-option v-for="item in sourceList" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患发现人">
            <el-input v-model="HIDDENFIND_NAME" placeholder="请输入关键字"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="发现日期">
            <el-date-picker v-model="dates" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="确认人">
            <el-input v-model="confirmUserName" placeholder="请输入关键字"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="整改人">
            <el-input v-model="RECTIFICATIONORNAME" placeholder="请输入关键字"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="验收人">
            <el-input v-model="CHECKNAME" placeholder="请输入关键字"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患级别">
            <SelectTree
              v-if="hiddenlevelList.length !== 0"
              ref="hlTree_Select"
              :clearable="false"
              :options="hiddenlevelList"
              :props="defaultProps"
              v-model="HIDDENLEVEL"
              :canparent="false"
              style="width: 100%;"
              children-name="nodes"
              placeholder="请选择" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患状态">
            <el-select v-model="STATE" placeholder="请选择" style="width: 100%;">
              <el-option v-for="item in statusList" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患部位">
            <SelectTree
              ref="hiddenPart_Select"
              :clearable="false"
              :options="hiddenPartList"
              :props="defaultProps2"
              v-model="keyHiddenPart"
              placeholder="请选择部门"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患类型：" prop="HIDDENTYPE">
            <SelectTree
              ref="deptTree_Select3"
              :clearable="false"
              :options="hiddenTypeList"
              :props="defaultProps"
              v-model="keyHiddenType"
              :canparent="false"
              children-name="nodes"
              style="width: 100%;"
              placeholder="请选择" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患发现部门" prop="keyFindUserDept">
            <SelectTree
              ref="deptTree_Select"
              :clearable="false"
              :options="hiddenCreatorDeptList"
              :props="defaultProps"
              v-model="keyFindUserDept"
              placeholder="请选择部门"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item style="text-align: right">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="text-align: right">
            <el-button type="info" icon="el-icon-printer" size="mini" plain @click="goPrint">打印</el-button>
            <el-button type="info" icon="el-icon-top-right" size="mini" plain @click="goExcel">导出</el-button>
            <el-button type="info" icon="el-icon-top-right" size="mini" plain @click="goExcelColumn">按列导出</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="listLoading" ref="multipleTable" :data="varList" :row-key="getRowKey" border tooltip-effect="dark" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="隐患来源" >
        <template slot-scope="{row}">
          <template v-if="row.SOURCE=='1'">隐患快报  </template>
          <template v-else-if="row.SOURCE=='2'">清单排查 </template>
          <template v-else-if="row.SOURCE=='3'">清单排查 </template>
          <template v-else-if="row.SOURCE=='4'">安全环保检查(监管端) </template>
          <template v-else-if="row.SOURCE=='5'">安全环保检查(企业端) </template>
          <template v-else-if="row.SOURCE=='6'">消防检查 </template>
        </template>
      </el-table-column>
      <el-table-column label="隐患描述" >
        <template slot-scope="{row}">
          <div class="link-type" @click="goView(row.HIDDEN_ID)">{{ row.HIDDENDESCR }}</div>
        </template>
      </el-table-column>
      <el-table-column label="隐患处置" show-overflow-tooltip width="120">
        <template slot-scope="{row}">
          <template v-if="row.RECTIFICATIONTYPE=='1'">
            <div>立即整改</div>
          </template>
          <template v-else-if="row.RECTIFICATIONTYPE=='2'">
            <div>限期整改</div>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="HIDDENLEVELNAME" label="隐患级别" width="120" align="center" />
      <el-table-column label="隐患发现人" prop="findUserNames" />
      <el-table-column label="隐患部位" prop="HREGIONNAME" >
        <template slot-scope="{row}">
          <template v-if="row.HREGIONNAME">{{ row.HREGIONNAME }}</template>
          <template v-else>{{ row.HIDDENPART }}</template>
        </template>
      </el-table-column>
      <el-table-column label="隐患发现时间" prop="DISCOVERYTIME">
        <template slot-scope="{row}">
          {{ formatDate(row.DISCOVERYTIME,'YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="隐患状态" width="100" align="center">
        <template slot-scope="{row}">
          <template v-if="row.STATE==0"><span>未提交</span></template>
          <template v-else-if="row.STATE==1"><span>未整改</span></template>
          <template v-else-if="row.STATE==2"><span>已整改</span></template>
          <template v-else-if="row.STATE==3"><span>已验收</span></template>
          <template v-else-if="row.STATE==4"><span>已验收</span></template>
          <template v-else-if="row.STATE==5"><span>忽略隐患</span></template>
          <template v-else-if="row.STATE==6"><span>重大隐患</span></template>
          <template v-else-if="row.STATE==7"><span>待处理的特殊隐患</span></template>
          <template v-else-if="row.STATE==8"><span>已处理的特殊隐患</span></template>
          <template v-else-if="row.STATE==-1"><span>已过期</span></template>
          <template v-else-if="row.STATE==-2"><span>待确认</span></template>
          <template v-else-if="row.STATE==16"><span>确认打回</span></template>
          <template v-else-if="row.STATE=='10'">验收打回</template>
          <template v-else-if="row.STATE=='11'">分公司安委会办公室副主任核定</template>
          <template v-else-if="row.STATE=='12'">分公司安委会办公室副主任核实</template>
          <template v-else-if="row.STATE=='13'">港股分公司安委会办公室副主任核定、重大隐患待整改</template>
          <template v-else-if="row.STATE=='14'">重大隐患待验收</template>
          <template v-else-if="row.STATE=='15'">重大隐患已归档</template>
          <template v-else-if="row.STATE=='16'">确认打回</template>
          <template v-else><span /></template>
        </template>
      </el-table-column>
      <el-table-column label="确认人" prop="confirmUserName" />
      <el-table-column prop="RECTIFICATIONTIME" label="整改时间" width="160" align="center" />
      <el-table-column prop="RECTIFICATIONORNAME" label="整改人" width="100" align="center" />
      <el-table-column prop="CHECKORNAME" label="验收人" width="100" align="center" >
        <template slot-scope="{row}">
          <template v-if="row.CHECKNAME != ''"><span>{{ row.CHECKNAME }}</span></template>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="300" align="center">
        <template slot-scope="{row}">
          <el-button icon="el-icon-view" size="mini" @click="goView(row.HIDDEN_ID)">查看</el-button>
          <el-button v-if="row.STATE =='16' && row.CREATOR === loginUserId" icon="el-icon-edit" size="mini" @click="goEdit(row.HIDDEN_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="goDel(row.HIDDEN_ID)">删除</el-button>
          <el-button v-if="row.STATE =='16' && row.CREATOR === loginUserId && ISMAIN !='1'" type="danger" icon="el-icon-delete" size="mini" @click="goDel(row.HIDDEN_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog v-if="dialogVisibleshow" :visible.sync="dialogVisibleshow" width="500px" title="请选择要导出列">
      <el-form label-width="80px">
        <el-form-item label="导出内容">
          <el-select v-model="exportSelection" multiple style="width: 100%;">
            <el-option v-for="item in hiddenlist" :key="item.key" :label="item.name" :value="item.key"/>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="exportsubmit">提交</el-button>
        <el-button @click="dialogVisibleshow = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
import SelectTree from '@/components/SelectTree'
import formatDate from '@/utils/dateformat'
export default {
  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      total: 0,
      dialogVisible: false,
      dialogVisibleshow: false,
      defaultProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      hidden: {},
      treeData: [],
      multipleSelection: [],
      varList: [],	// list
      hiddenlist: [],
      zhenggaiList: [],
      yanshouList: [],
      page: [],		// 分页类
      KEYWORDS: '',
      SOURCE: '',
      loginUserId: '', // 登录人ID
      keyFindUserDept: '',
      ISMAIN: '', // 是否主账号
      defaultProps2: { value: 'id', children: 'nodes', label: 'name' }, // 隐患部位
      keyHiddenPart: '',
      confirmUserName: '',
      keyHiddenType: '',
      HIDDENFIND_NAME: '',
      RECTIFICATIONORNAME: '',
      CHECKNAME: '',
      listQuery: {
        page: 1,
        limit: 20
      },
      add: false,		// 增
      del: false,		// 删
      edit: false,		// 改
      toExcel: false,	// 导出到excel权限
      loading: false,	// 加载状态
      hiddenPartList: [], // 隐患部位list
      hiddenTypeList: [], // 隐患类型list
      hiddenCreatorDeptList: [], // 隐患发现部门
      operationList: [
        { ID: '', NAME: '请选择' },
        /* {ID:'1',NAME:''}, */
        { ID: '2', NAME: '处理' },
        { ID: '3', NAME: '验收' }
      ],
      hiddenlevelList: [],
      qualifiedList: [
        { ID: '1', NAME: '是' },
        { ID: '0', NAME: '否' }
      ],
      sourceList: [
        { ID: '1', NAME: '隐患快报' },
        { ID: '2', NAME: '清单排查' },
        { ID: '4', NAME: '安全环保检查(监管端)' },
        { ID: '5', NAME: '安全环保检查(企业端)' },
        { ID: '6', NAME: '消防检查' }
      ],
      dialogType: 'add',
      dates: [], // 时间
      keyHiddenType2: '',
      STARTTIME: '',
      ENDTIME: '',
      HIDDENLEVEL: '',
      ISQUALIFIED: '',
      DISPOSESTATE: '',
      statusList: [
        { ID: '1', NAME: '未整改' },
        { ID: '2', NAME: '已整改' },
        { ID: '4', NAME: '已验收' },
        { ID: '5', NAME: '忽略隐患' },
        { ID: '6', NAME: '重大隐患' },
        { ID: '7', NAME: '待处理的特殊隐患' },
        { ID: '8', NAME: '已处理的特殊隐患' },
        { ID: '-1', NAME: '已过期' },
        { ID: '-2', NAME: '待确认' },
        { ID: '16', NAME: '确认打回' },
        { ID: '10', NAME: '验收打回' }
      ],
      STATE: '',
      DEPARTMENT_ID: '',
      IS_SELF: 0,
      checked: false, // 全选框
      checkList: [], // 选中的索引集合
      // BI页跳转过来参数
      IS_DAY: '',
      IS_WEEK: '',
      IS_XUN: '',
      IS_MONTH: '',
      IS_QUARTER: '',
      IS_YEAR: '',
      BISTATE: '',
      config: config,
      exportSelection: []
    }
  },
  created() {
    var state = this.$route.query.state
    console.info(state)
    if (state) {
      this.STATE = state
      this.IS_SELF = 1
    }

    this.getList()
    this.getTreeData()
    this.getTreeData1()
    this.getDict()
    this.getEditMyInfo()
    this.getHiddenNames()
  },
  methods: {
    getRowKey(row) {
      return row.HIDDEN_ID
    },
    getHiddenRowKey(row) {
      return row.key
    },
    // 查询
    getQuery() {
      // this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 重置
    goKeyReset() {
      // this.KEYWORDS = ''
      // this.STATE = ''
      // this.SOURCE = ''
      // this.RECTIFICATIONORNAME = ''
      // this.HIDDENFIND_NAME = ''
      // this.CHECKNAME = ''
      // this.confirmUserName = ''
      // this.keyFindUserDept = ''
      // this.keyHiddenPart = ''
      // this.DEPARTMENT_ID = ''
      // this.dates = []
      // this.DISPOSESTATE = ''
      // this.ISQUALIFIED = ''
      // this.$refs.deptTree_Select3.clearHandle()
      // this.$refs.deptTree_Select.clearHandle()
      // this.$refs.hlTree_Select.clearHandle()
      // this.HIDDENLEVEL = ''
      // this.$refs.hiddenPart_Select.clearHandle()
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获得部门树
    getTreeData1() {
      requestFN(
        '/department/listTree',
        {}
      ).then((data) => {
        this.hiddenCreatorDeptList = JSON.parse(data.zTreeNodes)
      }).catch((e) => {
      })
    },
    // 获得部门树
    getTreeData() {
      requestFN(
        '/department/listTree',
        {}
      ).then((data) => {
        this.treeData = JSON.parse(data.zTreeNodes)
      }).catch((e) => {
      })
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/hidden/getListForTianzhang' +
        '?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          keyFindUserDept: this.keyFindUserDept,
          keyHiddenType: this.keyHiddenType,
          HIDDENDESCR: this.KEYWORDS,
          keySource: this.SOURCE,
          HIDDENFIND_NAME: this.HIDDENFIND_NAME,
          RECTIFICATIONORNAME: this.RECTIFICATIONORNAME,
          CHECKNAME: this.CHECKNAME,
          STATE: this.STATE,
          confirmUserName: this.confirmUserName,
          keyHiddenPart: this.keyHiddenPart,
          STARTTIME: this.dates[0],
          ENDTIME: this.dates[1],
          ISQUALIFIED: this.ISQUALIFIED,
          DEPARTMENT_ID: this.DEPARTMENT_ID,
          HIDDENLEVEL: this.HIDDENLEVEL,
          DISPOSESTATE: '-1',
          // 下面的参数由BI页传过来，暂不处理
          IS_SELF: this.IS_SELF,
          IS_DAY: this.IS_DAY,
          IS_WEEK: this.IS_WEEK,
          IS_XUN: this.IS_XUN,
          IS_MONTH: this.IS_MONTH,
          IS_QUARTER: this.IS_QUARTER,
          IS_YEAR: this.IS_YEAR,
          BISTATE: this.BISTATE
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.getEditMyInfo()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.$parent.activeName = 'Edit'
      this.$parent.HIDDEN_ID = ''
    },

    getEditMyInfo() { // 获取登录人信息
      requestFN(
        '/user/goEditMyInfo',
        {}
      ).then((data) => {
        this.loginUserId = data.pd.USER_ID
        this.ISMAIN = data.pd.ISMAIN
        this.$forceUpdate()
      }).catch((e) => {

      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'hidden:add,hidden:del,hidden:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.hiddenfhadminadd		// 新增权限
        this.del = data.hiddenfhadmindel		// 删除权限
        this.edit = data.hiddenfhadminedit	// 修改权限
        this.toExcel = data.toExcel // 导出到excel权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
      requestFN(
        '/dictionaries/listSelectTree',
        {
          DICTIONARIES_ID: '5e7cf8620ba54ad89719d0be62133c7a'
        }
      ).then((data) => {
        this.hiddenlevelList = JSON.parse(data.zTreeNodes)
      })
      // 隐患类型
      requestFN(
        '/dictionaries/listSelectTree',
        {
          DICTIONARIES_ID: '3babc15144444bdc8d763d0af2bdfff6'
        }
      ).then((data) => {
        this.hiddenTypeList = JSON.parse(data.zTreeNodes)
      })
      // 隐患部位
      requestFN(
        '/hiddenRegion/listAll'
      ).then((data) => {
        this.hiddenPartList = JSON.parse(data.zTreeNodes)
      }).catch((e) => {
        this.listLoading = false
      })
    },
    goView(hiddenID) {
      this.$parent.activeName = 'Detail'
      this.$parent.HIDDEN_ID = hiddenID
    },
    goEdit(hiddenID) {
      this.$parent.activeName = 'Edit'
      this.$parent.HIDDEN_ID = hiddenID
    },
    goDel(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/hidden/delete',
          {
            HIDDEN_ID: id,
            tm: new Date().getTime()
          }
        ).then((data) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    goPrint() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中需要打印的数据...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.HIDDEN_ID
      }).join(',')
      this.$parent.activeName = 'Print'
      this.$parent.HIDDEN_IDS = ids
    },
    goExcelColumn() {
      if (this.dates.length == 0) {
        this.$message.error('请选择开始时间')
        return
      }
      if (this.dates.length == 1) {
        this.$message.error('请选择开始时间')
        return
      }
      this.exportSelection = []
      this.dialogVisibleshow = true
    },
    exportsubmit() {
      const _selectData = this.exportSelection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要导出的数据...',
          type: 'error'
        })
        return false
      }
      const rows = []
      for (let i = 0; i < this.exportSelection.length; i++) {
        for (let j = 0; j < this.hiddenlist.length; j++) {
          if (this.exportSelection[i] === this.hiddenlist[j].key) {
            rows.push({
              state: '1',
              name: this.hiddenlist[j].name,
              key: this.hiddenlist[j].key
            })
            break
          }
        }
      }
      window.open(this.config.httpurl + 'hidden/exportexcel?export=' + encodeURIComponent(JSON.stringify(rows)) + '&STARTTIME=' + this.dates[0] + '&ENDTIME=' + this.dates[1])
      this.dialogVisibleshow = false
    },
    getHiddenNames() {
      requestFN(
        '/hidden/getHiddenNames',
        {
        }
      ).then((data) => {
        this.hiddenlist = data.varList
        console.log(this.hiddenlist)
        this.zhenggaiList = data.zhenggaiList
        this.yanshouList = data.yanshouList
      }).catch((e) => {
        this.listLoading = false
      })
    },
    goExcel() {
      if (this.dates.length == 0) {
        this.$message.error('请选择开始时间')
        return
      }
      if (this.dates.length == 1) {
        this.$message.error('请选择开始时间')
        return
      }
      var data = '?KEYWORDS=' + this.KEYWORDS
      data += '&DISPOSESTATE=' + this.DISPOSESTATE
      data += '&STATE=' + this.STATE
      data += '&STARTTIME=' + this.dates[0]
      data += '&ENDTIME=' + this.dates[1]
      data += '&DEPARTMENT_ID=' + this.DEPARTMENT_ID
      data += '&ISQUALIFIED=' + this.ISQUALIFIED
      data += '&HIDDENLEVEL=' + this.HIDDENLEVEL
      data += '&DISPOSESTATE=-1'
      data += '&IS_SELF=' + this.IS_SELF
      data += '&tm=' + new Date().getTime()
      // var dsdfsd = [{ 'name': '隐患来源', 'key': 'SOURCE', 'state': '1' }, { 'name': '风险点（单元）', 'key': 'RISK_UNIT', 'state': '1' }, { 'name': '辨识部位', 'key': 'IDENTIFICATION', 'state': '1' }]
      // var sjson = JSON.stringify(dsdfsd)
      // console.info('222ddd ')
      //
      // data += '&xuanzhong=' + sjson
      // console.info('data')
      // console.info(data)
      window.location.href = config.httpurl + 'hidden/excel' + data
    },
    formatDate(date, format) {
      return formatDate(date, format)
    }
  }

}

</script>

<style lang="sass" scoped>
.filter-container
  position: relative
  .filter-flot
    position: absolute
    right: 0
    top: 0
</style>
