<template>
  <div>
    <div class="app-container">
      <div class="filter-container">
        <el-row :gutter="20">
          <el-form label-width="80px">
            <el-col :span="4">
              <el-form-item label="标准类别">
                <el-select v-model="searchForm.CHECK_CATEGORY" placeholder="标准类别" clearable style="width: 150px" class="filter-item">
                  <el-option v-for="item in checkCategoryList" :key="item.DICTIONARY_ID" :label="item.DICTIONARY_NAME" :value="item.DICTIONARY_ID" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="标准项目">
                <el-select v-model="searchForm.CHECK_ITEM" placeholder="标准项目" clearable style="width: 200px" class="filter-item">
                  <el-option v-for="item in checkItemList" :key="item.DICTIONARY_ID" :label="item.DICTIONARY_NAME" :value="item.DICTIONARY_ID" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="检查内容">
                <el-input v-model="searchForm.CHECK_CONTENT" placeholder="搜索检查内容..." class="filter-item" />
              </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-left" @click="resetting">
                  重置
                </el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <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="CHECK_CATEGORY_NAME" label="检查类别" width="100" />
        <el-table-column prop="CHECK_ITEM_NAME" label="检查项目" width="150" />
        <el-table-column prop="CHECK_CONTENT" label="检查内容" />
        <el-table-column prop="CHECK_STANDARD" label="检查标准" />
        <el-table-column prop="REFERENCE_BASIS" label="参考依据" />
        <el-table-column prop="CHECK_QUALIFIED" label="检查合格项" />
        <el-table-column prop="CHECK_UNQUALIFIED" label="检查不合格项" />
        <el-table-column prop="OPERATION_TYPE" label="操作类型" width="70" align="center" >
          <template slot-scope="{row}">
            {{ row.OPERATION_TYPE ? (row.OPERATION_TYPE === 1 ? '选择' : '填写') : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="COMMON_ITEM_STATUS" label="状态" width="60" align="center" >
          <template slot-scope="{row}">
            {{ row.COMMON_ITEM_STATUS ? (row.COMMON_ITEM_STATUS === 1 ? '有效' : '停用') : '' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="{row}">
            <el-row :gutter="20">
              <el-button type="success" icon="el-icon-view" size="mini" @click="getInfo(row)">查看</el-button>
              <el-button v-show="edit" :disabled="row.COUNT_CUSTOM !== 0" 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_ITEM_ID)">删除</el-button>
            </el-row>
          </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>
          <el-button v-show="false" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
        </div>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      </div>
      <item-edit ref="edit" :title="title" href="edit" @beforeClose="refreshDict"/>
      <item-info ref="info" :title="title"/>
    </div>
    <div class="ui-height" />
    <div class="ui-foot">
      <el-button plain type="info" @click="goBack">返 回</el-button>
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
import ItemEdit from './itemEdit.vue'
import ItemInfo from './itemInfo.vue'
export default {
  components: { Pagination, ItemEdit, ItemInfo },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      title: '新增',
      checkCategoryAllList: [], // 标准类别
      checkCategoryList: [], // 标准类别
      checkItemAllList: [], // 标准项目
      checkItemList: [], // 标准项目
      commonStatusList: [
        { id: 1, name: '有效' },
        { id: 2, name: '停用' }
      ],
      searchForm: {
        CHECK_CATEGORY: '',
        CHECK_ITEM: '',
        CHECK_CONTENT: ''
      }
    }
  },
  watch: {
    'searchForm.CHECK_CATEGORY': {
      handler() {
        this.changeCategory(this.searchForm.CHECK_CATEGORY)
      }
    }
  },
  created() {
    this.getDictCheckCategory()
    this.getDictCheckItem()
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.COMMON_ITEM_ID
    },
    changeCategory(CHECK_CATEGORY) {
      this.searchForm.CHECK_ITEM = ''
      this.checkItemList = []
      this.checkItemAllList.forEach((item) => {
        if (item.PARENT_IDS.includes(CHECK_CATEGORY)) {
          this.checkItemList.push(item)
        }
      })
      this.$forceUpdate()
    },
    getQuery() {
      this.getList()
    },
    resetting() {
      this.searchForm = {
        CHECK_CATEGORY: '',
        CHECK_ITEM: '',
        CHECK_CONTENT: ''
      }
      this.getList()
    },
    refreshDict() {
      this.getDictCheckCategory()
      this.getDictCheckItem()
      this.getList()
    },
    getList() {
      this.listLoading = true
      requestFN(
        '/hiddenDangerCheckStandardCommon/pageItem?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          COMMON_ID: this.$parent.COMMON_ID,
          CHECK_CATEGORY: this.searchForm.CHECK_CATEGORY,
          CHECK_ITEM: this.searchForm.CHECK_ITEM,
          CHECK_CONTENT: this.searchForm.CHECK_CONTENT
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    handleAdd() {
      const data = {}
      data.form = JSON.parse(JSON.stringify({ COMMON_ID: this.$parent.COMMON_ID, COMMON_ITEM_SORT: this.total + 1 }))
      data.checkCategoryList = this.checkCategoryList
      data.checkItemAllList = this.checkItemAllList
      this.$refs.edit.init(data)
      this.title = '新增'
    },
    handleEdit(row) {
      const data = {}
      data.form = JSON.parse(JSON.stringify(row))
      data.checkCategoryList = this.checkCategoryList
      data.checkItemAllList = this.checkItemAllList
      this.$refs.edit.init(data)
      this.title = '编辑'
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/hiddenDangerCheckStandardCommon/deleteItem',
          { COMMON_ITEM_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_ITEM_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
      })
    },
    async getDictCheckCategory() {
      const { list } = await requestFN(
        '/hiddenDangerCheckStandardDictionary/listSelect',
        {
          COMMON_ID: this.COMMON_ID, // 标准ID
          DICTIONARY_LEVEL: 1 // 标准类别
        }
      )
      this.checkCategoryList = list
    },
    async getDictCheckItem() {
      const { list } = await requestFN(
        '/hiddenDangerCheckStandardDictionary/listSelect',
        {
          COMMON_ID: this.COMMON_ID, // 标准ID
          DICTIONARY_LEVEL: 2 // 标准类别
        }
      )
      this.checkItemAllList = list
    },
    getInfo(e) {
      this.$refs.info.init(e)
      this.title = '详情'
    },
    getResult(e) {
      this.getList()
    },
    goBack() {
      this.$parent.activeName = 'List'
      this.$parent.COMMON_ID = ''
    }
  }
}

</script>
