<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-input v-model="searchParam.nameLike" clearable size="small" placeholder="通知标题" style="width: 200px;" class="filter-item" />
        <el-select v-model="searchParam.hasRead" clearable size="small" placeholder="未读/已读" style="width: 200px;" class="filter-item">
          <el-option :key="1" label="未读" :value="false" />
          <el-option :key="2" label="已读" :value="true" />
        </el-select>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="searchSubmit">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="searchParamReset">重置</el-button>
      </div>
    </div>
    <!--弹出框1-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog1Show" :title="'查看通知'" width="640px">
      <el-form ref="dialog1Form" :model="dialog1Data" size="mini" label-width="180px" style="margin-right: 80px;">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="标题：" prop="name">
              <span>{{ dialog1Data.name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型：" prop="dict40Name">
              <span>{{ dialog1Data.dict40Name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述：" prop="detail">
              <span>{{ dialog1Data.detail }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件：" prop="filesPath">
              <el-link v-for="file in dialog1Data._fileList" :key="file.url" @click="openFile(file)">{{ file.name }}</el-link>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!--表格-->
    <el-table ref="table" v-loading="searching" :data="tableData" row-key="id">
      <el-table-column :show-overflow-tooltip="true" prop="id" label="编号" width="100px" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="name" label="标题" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="dict40Name" label="类型" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="createDatetime" label="时间" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="detail" label="描述" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="" label="已读" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.hasRead">已读</span>
          <span v-else style="color: #F56C6C;">未读</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="filesPath" label="附件" align="center">
        <template slot-scope="scope">
          <span v-if="!scope.row.filesPath || scope.row.filesPath==='[]'">无</span>
          <span v-else>有</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" round @click="doEdit(scope.row.id, scope.row.responseId)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 50, 100]"
      :current-page.sync="searchParam.page"
      :page-size.sync="searchParam.count"
      :total="searchResult.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="searchSubmit"
      @current-change="searchSubmit"
    />
  </div>
</template>

<script>
import noticeApi from '@/api/kd/notice'
import dictionaryApi from '@/api/kd/dictionary'
import { mapGetters } from 'vuex'
const searchParam = {// 默认搜索条件
  hasRead: false,
  page: 1,
  count: 10,
  searchCount: true
}
export default {
  name: 'CNoticeMix', // 本页面名
  components: {},
  data() {
    return {
      selectedMine: {},
      dict40List: [],
      searching: false,
      searchParam: {},
      searchResult: {
        total: 0,
        results: [] // 搜索结果
      },
      dialog1Show: false,
      dialog1Data: {}
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'user'
    ]),
    tableData() {
      // 此处对返回值进行算法重组
      return this.searchResult.results
    }
  },
  mounted() {
    // 矿山
    const selectedMineJson = window.localStorage.getItem('kd.selected.mine')
    let noMine = false
    if (!selectedMineJson) {
      noMine = true
    } else {
      const selectedMine = JSON.parse(selectedMineJson)
      if (!selectedMine || selectedMine.enterpriseId !== this.user.enterpriseId) {
        noMine = true
      } else {
        this.selectedMine = selectedMine
      }
    }
    if (noMine) {
      this.$message.error('当前未选择矿山')
      this.$router.replace('/dashboard')
    }
    // 字典
    const dictGroups = [40]
    dictGroups.forEach(i => {
      dictionaryApi.getList({ 'groupCode': i }).then(response => {
        this['dict' + i + 'List'] = response.results
      })
    })
    this.searchParamReset()
    this.searchSubmit()
  },
  methods: {
    searchParamReset() {
      this.searchParam = JSON.parse(JSON.stringify(searchParam))
    },
    searchSubmit() {
      this.searching = true
      this.searchParam.mineType = this.selectedMine.mineType
      this.searchParam.mineId = this.selectedMine.id
      noticeApi.getResponsePage(this.searchParam).then(response => {
        this.searching = false
        this.searchResult = response
      }).catch(() => {
        this.searching = false
      })
    },
    doEdit(id, responseId) {
      noticeApi.getById(id).then(response => {
        this.dialog1Data = response
        if (this.dialog1Data.filesPath) {
          this.dialog1Data._fileList = JSON.parse(this.dialog1Data.filesPath)
        }
        this.dialog1Show = true
        this.$nextTick(() => {
          this.$refs['dialog1Form'].clearValidate()
        })
        if (responseId) {
          noticeApi.read(responseId).then(() => {
            this.searchResult.results.forEach(x => {
              if (x.id === id) {
                x.hasRead = true
              }
            })
          })
        }
      }).catch(() => {
        this.searchSubmit()
      })
    },
    upSuccess(response, file, fileList) {
      if (!this.dialog1Data._fileList) {
        this.dialog1Data._fileList = []
      }
      this.dialog1Data._fileList.push({
        name: file.name,
        url: response.result
      })
    },
    upError() {
      this.$message.error('上传失败，请稍后再试')
    },
    upRemove() {
      this.$set(this.dialog1Data, '_fileList', [])
    },
    upBefore(file) {
      const isIMG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLtSize = file.size / 1024 / 1024 < 20
      if (!isIMG) {
        this.$message.error('上传图片只能是 JPG/JPEG/PNG 格式')
        return false
      }
      if (!isLtSize) {
        this.$message.error('上传图片大小不能超过 20MB')
        return false
      }
      return true
    },
    upExceed() {
      this.$message.error('您已经上传了一张图，请删除后重新上传')
    },
    openFile(file) {
      if (file.url) {
        window.open(this.baseApi + '/file' + file.url)
      } else if (file.response && file.response.result) {
        window.open(this.baseApi + '/file' + file.response.result)
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
  ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
