<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form label-width="80px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="行业类型">
                <cascader-dict
                  v-model = "searchForm.INDUSTRY_TYPE_ARR"
                  :form.sync="searchForm"
                  :dict-id = "dictId"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标准名称">
                <el-input v-model="searchForm.CHECK_STANDARD_NAME" placeholder="搜索标准名称..." class="filter-item" />
              </el-form-item>
            </el-col>
            <el-col v-show="!searchFromCollapse" :span="6">
              <el-form-item label="导入时间">
                <el-date-picker
                  v-model="searchForm.CREATE_TIME"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"/>
              </el-form-item>
            </el-col>
            <el-col v-show="!searchFromCollapse" :span="6">
              <el-form-item label="状态">
                <el-select v-model="searchForm.COMMON_STATUS" placeholder="状态" clearable style="width: 100%" class="filter-item">
                  <el-option v-for="item in commonStatusList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="!searchFromCollapse ? 24 : 12" >
              <el-form-item style="text-align: right">
                <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-left" @click="resetting">
                  重置
                </el-button>
                <el-button type="text" @click="searchFromCollapse = !searchFromCollapse">{{ searchFromCollapse ? '展开' : '合并' }} <i :class="searchFromCollapse ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"/></el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="app-tools">
        <div class="app-function">
          <el-button v-show="add" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          <el-button v-show="false" type="danger" icon="el-icon-delete" size="mini" plain @click="batchDel">删除</el-button>
          <el-button type="primary" icon="el-icon-document-add" size="mini" @click="importExcel('import')">
            导入标准表
          </el-button>
        </div>
      </div>
      <div class="app-container">
        <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="INDUSTRY_TYPE_NAMES" label="行业类型" >
            <template slot-scope="{row}">
              {{ row.INDUSTRY_TYPE_NAMES.replaceAll(',', ' / ') }}
            </template>
          </el-table-column>
          <el-table-column prop="CHECK_STANDARD_NAME" label="隐患排查标准名称" />
          <el-table-column prop="CREATE_TIME" label="导入时间" width="140" align="center" />
          <el-table-column prop="COMMON_STATUS" label="状态" width="60" align="center" >
            <template slot-scope="{row}">
              {{ row.COMMON_STATUS ? (row.COMMON_STATUS === 1 ? '有效' : '停用') : '' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="320">
            <template slot-scope="{row}">
              <el-row :gutter="20" style="margin-bottom: 5px">
                <el-button type="success" icon="el-icon-view" size="mini" @click="getInfo(row)">查看</el-button>
                <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row)">编辑</el-button>
                <el-button v-show="del" :disabled="row.COUNT_CUSTOM !== 0" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.COMMON_ID)">删除</el-button>
              </el-row>
              <el-row :gutter="20">
                <el-button type="success" icon="el-icon-tickets" size="mini" @click="getCommonItem(row)">清单详细</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="importExcel('reimport', row)">重新导入</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="importExcel('appendImport', row)">追加导入</el-button>
              </el-row>
            </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>
    </div>
    <edit ref="edit" :title="title" href="edit" @beforeClose="getList"/>
    <info ref="info" :title="title"/>
    <update-file ref="updateFile" @logical-end="getResult"/>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
import edit from './edit.vue'
import info from './info.vue'
import Info from '../../../onlinexxks/stage/exam/components/info.vue'
import updateFile from './updateFile.vue'
import CascaderDict from '../../../../components/CascaderDict'
export default {
  components: { updateFile, Info, edit, Pagination, info, CascaderDict },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      title: '新增',
      dictId: 'f2598ba72e864eadabf0ca4b664d26b9', // 行业类型
      commonStatusList: [
        { id: 1, name: '有效' },
        { id: 2, name: '停用' }
      ],
      searchFromCollapse: true,
      searchForm: {
        INDUSTRY_TYPE_ARR: [],
        INDUSTRY_TYPE_TREE: '',
        CHECK_STANDARD_NAME: '',
        COMMON_STATUS: '',
        CREATE_TIME: []
      }
    }
  },
  created() {
    this.getList()
    this.getDict()
  },
  methods: {
    getRowKey(row) {
      return row.COMMON_ID
    },
    getQuery() {
      this.getList()
    },
    resetting() {
      this.searchForm = {
        INDUSTRY_TYPE_ARR: [],
        INDUSTRY_TYPE_TREE: '',
        CHECK_STANDARD_NAME: '',
        COMMON_STATUS: '',
        CREATE_TIME: []
      }
      this.getList()
    },
    getList() {
      this.listLoading = true
      this.searchForm.INDUSTRY_TYPE_TREE = this.searchForm.INDUSTRY_TYPE_ARR.join(',')
      requestFN(
        '/hiddenDangerCheckStandardCommon/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          INDUSTRY_TYPE_TREE: this.searchForm.INDUSTRY_TYPE_TREE,
          CHECK_STANDARD_NAME: this.searchForm.CHECK_STANDARD_NAME,
          COMMON_STATUS: this.searchForm.COMMON_STATUS,
          CREATE_TIME_BEGIN: this.searchForm.CREATE_TIME[0] || '',
          CREATE_TIME_END: this.searchForm.CREATE_TIME[1] || ''
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    handleAdd() {
      this.$refs.edit.init()
      this.title = '新增'
    },
    handleEdit(row) {
      this.$refs.edit.init(JSON.parse(JSON.stringify(row)))
      this.title = '编辑'
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/hiddenDangerCheckStandardCommon/delete',
          { COMMON_ID: id }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    batchDel() {
      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.COMMON_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/hiddenDangerCheckStandardCommon/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    hasButton: function() {
      var keys = 'hiddenDangerCheckStandardCommon:add,hiddenDangerCheckStandardCommon:del,hiddenDangerCheckStandardCommon:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.hiddenDangerCheckStandardCommonfhadminadd		// 新增权限
        this.del = data.hiddenDangerCheckStandardCommonfhadmindel		// 删除权限
        this.edit = data.hiddenDangerCheckStandardCommonfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    getDict: function() {
      requestFN(
        'dictionaries/getLevels', { DICTIONARIES_ID: 'f2598ba72e864eadabf0ca4b664d26b9' }
      ).then((data) => {
        this.industry_type_list = data.list
      }).catch((e) => {
        this.loading = false
      })
      requestFN(
        '/dictionaries/getLevels', { DICTIONARIES_ID: 'b4a7325f96af40808ec64c85c162933d' }
      ).then((data) => {
        this.hidden_types_list = data.list
      }).catch((e) => { console.log(e) })
    },
    getInfo(row) {
      this.$refs.info.init(row.COMMON_ID)
      this.title = '详情'
    },
    getCommonItem(row) {
      this.$parent.activeName = 'ItemList'
      this.$parent.COMMON_ID = row.COMMON_ID
    },
    importExcel(importType, row) {
      const data = {}
      if (importType !== 'import') {
        data.form = JSON.parse(JSON.stringify(row))
      }
      data.importType = importType
      this.$refs.updateFile.init(data)
    },
    getResult(e) {
      this.getList()
    }
  }
}

</script>
<style lang="sass" scoped>
  .filter-container
    position: relative
    .filter-flot
      position: absolute
      padding-bottom: 33px
      right: 0
      bottom: 0
</style>
