<template>
  <div class="app-container">
    <el-page-header content="查看详情" @back="goBack"/>
    <div class="level-title mt-20"/>
    <el-form label-width="100px">
      <!--      <el-row>-->
      <!--        <el-col :span="5">-->
      <!--          <el-form-item label="关键字搜索">-->
      <!--            <el-input v-model="KEYWORDS" placeholder="搜索关键字" />-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--        <el-col :span="6">-->
      <!--          <el-form-item label-width="10px">-->
      <!--            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">-->
      <!--              搜索-->
      <!--            </el-button>-->
      <!--            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">-->
      <!--              重置-->
      <!--            </el-button>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <el-row>
        <el-col :span="8">
          <el-form-item label="关键字搜索">
            <el-input v-model="KEYWORDS" placeholder="搜索关键字" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="集团单位">
            <el-select v-model="GROUP_UNIT" placeholder="请选择集团单位" style="width: 100%;">
              <el-option v-for="item in groupCorpList" :key="item.CORPINFO_ID" :label="item.CORP_NAME" :value="item.CORPINFO_ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主管部门">
            <SelectTree
              ref="deptTree"
              :clearable="false"
              :options="treeDataInspectDept"
              :props="defaultProps"
              v-model="COMPETENT_DEPT_ID"
              placeholder="请选择企业主管部门"
              style="width: 100%"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="施工相关方">
            <el-select v-model="UNITS_ID" filterable placeholder="请选择施工相关方" style="width: 100%">
              <el-option
                v-for="item in unitsList"
                :key="item.UNITS_ID"
                :label="item.UNITS_NAME"
                :value="item.UNITS_ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="监管部门">
            <SelectTree
              ref="deptTree1"
              :clearable="false"
              :options="treeDataInspectDept"
              :props="defaultProps"
              v-model="DEPARTMENT_ID"
              placeholder="请选择股份监管部门"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同起止时间">
            <el-date-picker
              v-model="CONTRACTTIME"
              type="daterange"
              unlink-panels
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              style="width: 100%;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="状态">
            <el-select ref="SELECT_STATE" v-model="STATE" style="width: 100%;">
              <el-option v-for="item in stateList" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label-width="10px">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="OUTSOURCED_NAME" label="重点工程名称" show-overflow-tooltip />
      <el-table-column prop="STARTTIME" label="工期" show-overflow-tooltip>
        <template slot-scope="{row}">
          {{ row.STARTTIME }} -- {{ row.ENDTIME }}
        </template>
      </el-table-column>
      <el-table-column prop="DEPARTMENT_NAME" label="监管部门" show-overflow-tooltip >
        <template slot-scope="{row}">
          <span v-if="row.Q_DEPARTMENT_NAME"> {{ row.Q_DEPARTMENT_NAME }} </span>
          <span v-else>{{ row.DEPARTMENT_NAME }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主管部门" show-overflow-tooltip >
        <template slot-scope="{row}">
          <span v-if="row.Q_COMPETENT_DEPT_NAME"> {{ row.Q_COMPETENT_DEPT_NAME }} </span>
          <span v-else>{{ row.MANAGER_DEPARTMENT_NAME }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="UNITS_NAME" label="施工相关方" width="250" show-overflow-tooltip />
      <el-table-column prop="VIDEO_COUNT" label="视频个数" />
      <el-table-column prop="STATE" label="状态">
        <template slot-scope="{row}">
          <span v-if="row.STATE == 1">进行中</span>
          <span v-if="row.STATE == 2">已结束</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-view" size="mini" @click="goDetail(row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
</div></template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import SelectTree from '@/components/SelectTree'

import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      CORPINFO_ID: '',
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      total: 0,
      form: {},
      KEYWORDS: '',
      GROUP_UNIT: '',
      DEPARTMENT_ID: '',
      COMPETENT_DEPT_ID: '',
      STATE: '',
      CONTRACTTIME: ['', ''], // 区间条件
      stateList: [
        { ID: '1', NAME: '进行中' },
        { ID: '2', NAME: '已结束' }
      ],
      UNITS_ID: '',
      groupCorpList: [],
      unitsList: [],
      treeDataInspectDept: [],
      defaultProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      varList: []
    }
  },
  watch: {
    CONTRACTTIME: function(newData, oldData) {
      if (!newData) {
        this.CONTRACTTIME = ['', '']
        this.getQuery()
      }
    }
  },
  created() {
    this.CORPINFO_ID = this.$parent.CORPINFO_ID
    this.getList()
    this.getUnitsList()
    this.getCorpList()
    this.selectDeptOption()
  },
  methods: {
    getCorpList() {
      requestFN(
        '/corpinfo/listAll',
        {}
      ).then((data) => {
        this.corpList = data.varList
        this.mangeList = data.varList
        if (!this.$parent.OUTSOURCED_ID) {
          this.form.MANAGE_NAME = ['133a982a3e93464a9b1de25fe6dd4948']
        }
        for (let i = 0; i < data.varList.length; i++) {
          const corpId = data.varList[i].CORPINFO_ID
          if (corpId === '1e6dbbe16004402f8d2c0e52afd9a676' ||
            corpId === '3a854eefa7894e06aaa1a2611bca80f6' ||
            corpId === '020578a4c1f04bc692ee25145c2efbe5' || corpId === '90966974de3c4b83aca6f8fd6432d5c2') {
            this.groupCorpList.push(data.varList[i])
          }
        }
      }).catch((e) => {
      })
    },
    selectDeptOption() {
      var url = ''
      if (this.CORPINFO_ID == '1') {
        url = '/department/listTree'
      } else {
        url = '/department/listTreeCorpInfo'
      }
      // 合并
      return new Promise((resolve) => {
        requestFN(
          url,
          {
            CORPINFO_ID: this.CORPINFO_ID
          }
        ).then((data) => {
          // this.treeDeptOptions = this.listTransTree(JSON.parse(data.zTreeNodes), 'id', 'pId', 'nodes')
          this.treeDataInspectDept = this.listTransTree(JSON.parse(data.zTreeNodes), 'id', 'pId', 'nodes')
          resolve()
        }).catch((e) => {
        })
      })
    },
    getUnitsList() {
      requestFN(
        '/units/listAll',
        { UNITS_ID: this.form.UNITS_ID }
      ).then((data) => {
        this.unitsList = data.varList
      }).catch((e) => {
      })
    },
    getRowKey(row) {
      return row.OUTSOURCED_ID
    },
    goBack() {
      this.$parent.activeName = 'OutSourced'
      this.$parent.OUTSOURCED_ID = ''
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.UNITS_ID = ''
      this.STATE = ''
      this.CONTRACT_STIME = ''
      this.CONTRACT_ETIME = ''
      this.CONTRACTTIME = ['', '']
      this.$refs.deptTree.clearHandle()
      this.$refs.deptTree1.clearHandle()
      this.INVOLVING_CORP = ''
      this.DEPARTMENT_ID = ''
      this.COMPETENT_DEPT_ID = ''
      this.GROUP_UNIT = ''
      this.CORP_NAME = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/outsourced/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          UNITS_ID: this.UNITS_ID,
          CONTRACT_STIME: this.CONTRACTTIME[0],
          CONTRACT_ETIME: this.CONTRACTTIME[1],
          STATE: this.STATE,
          INVOLVING_CORP: this.INVOLVING_CORP,
          DEPARTMENT_ID: this.DEPARTMENT_ID,
          COMPETENT_DEPT_ID: this.COMPETENT_DEPT_ID,
          GROUP_UNIT: this.GROUP_UNIT,
          CORP_NAME: this.CORP_NAME,
          CORPINFO_ID: this.CORPINFO_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 查看
    goDetail(row) {
      this.$parent.OUTSOURCED_ID = row.OUTSOURCED_ID
      this.$parent.UNITS_ID = row.UNITS_ID
      this.$parent.STATE = row.STATE
      this.$parent.activeName = 'videoList'
    }
  }
}

</script>
<style lang="sass" scoped>
  .table-qrcode
    text-align: center
    padding-top: 20px
    width: 100%
    .filter-container
      position: relative
      .filter-flot
        position: absolute
        right: 0
        top: 0
    .uploader
      width: 570px
      display: flex
      align-items: center
    .el-form-item__content
      line-height: 1
</style>

