<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-form v-model="queryParam" :inline="true">
        <el-form-item label="公告标题：">
          <el-input
            v-model="querys[0].value"
            clearable
            maxlength="128"
            placeholder="请输入公告标题"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="公告类型：">
          <el-select
            v-model="querys[1].value"
            clearable
            placeholder="请选择公告类型"
          >
            <el-option
              v-for="item in NoticeTypeList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公告状态：">
          <el-select
            v-model="querys[2].value"
            clearable
            placeholder="请选择公告状态"
          >
            <el-option
              v-for="item in NoticeStatusList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch"
            >搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="handleReset"
            >重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div ref="contentPanel" class="bbsl-table-content">
      <div ref="btnPanel" class="bbsl-btn-content">
        <el-button icon="el-icon-plus" type="success" @click="handleAdd"
          >新增
        </el-button>
        <el-popconfirm title="确定删除选中数据吗？" @confirm="handleDelete">
          <el-button
            slot="reference"
            :disabled="!multipleSelection.length"
            icon="el-icon-delete"
            style="margin-left: 10px"
            type="danger"
            >删除
          </el-button>
        </el-popconfirm>
        <el-button
          icon="el-icon-sort"
          plain
          style="margin-left: 10px"
          type="primary"
          @click="savePropertyf"
        >
          保存排序号
        </el-button>
      </div>
      <el-table
        ref="multipleSelection"
        v-loading="loading"
        :cell-style="{'text-align': 'center'}"
        :data="tableData"
        :default-sort="{prop: 'endTime', order: 'ascending'}"
        :header-cell-style="{'text-align': 'center'}"
        :max-height="tableMaxHeight"
        :style="{width: '100%'}"
        size="mini"
        @row-dblclick="handleDbClick"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          label="公告标题"
          min-width="150"
          prop="title"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="公告类型"
          min-width="100"
          prop="typeName"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="截至日期"
          min-width="100"
          prop="endTime"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="公告状态"
          min-width="70"
          prop="status"
          show-overflow-tooltip
          sortable
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              active-value="0"
              inactive-color="#85868a"
              inactive-value="1"
              @change="handleStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="是否置顶"
          min-width="70"
          prop="topStatus"
          show-overflow-tooltip
          sortable
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.topStatus"
              active-color="#13ce66"
              active-value="0"
              inactive-color="#85868a"
              inactive-value="1"
              @change="handleTopStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          min-width="150"
          prop="createTime"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          align="center"
          label="排序"
          min-width="70"
          prop="sn"
          show-overflow-tooltip
          sortable
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.sn"
              min="1"
              size="small"
              style="width:70px"
              type="number"
              @input="insort(scope.row)"
            >
            </el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="paginationPanel"
        :current-page="queryParam.pageBean.page"
        :page-size="queryParam.pageBean.pageSize"
        :page-sizes="[20, 30, 40, 50]"
        :total="total"
        align="right"
        background
        class="bbsl-pagination"
        layout="prev, pager, next,total, sizes, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <noticeMenuDialog ref="formModal" @loadData="loadData"></noticeMenuDialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import noticeMenuDialog from './components/noticeMenuDialog.vue'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import noticeMenu from '@/api/examination/noticeMenu.js'

export default {
  name: 'noticeMenu',
  components: {
    noticeMenuDialog
  },
  mixins: [TableMixin],
  data() {
    return {
      url: {
        delete: '/exam/notice/'
      },
      multipleSelection: [],
      tableData: [],
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'exam_notice.title_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'type',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: ''
        }
      ],
      // 公告类型
      NoticeTypeList: [],
      // 公告状态
      NoticeStatusList: [],
      sortList: {}
    }
  },
  created() {
    this.NoticeTypeList = dictUtils.getDictItemsFromCache('gglx')
    this.NoticeStatusList = dictUtils.getDictItemsFromCache('ggzt')
  },
  mounted() {},
  methods: {
    insort(val) {
      let id = val.id
      let value = val.sn
      this.sortList[id] = value
    },
    // 保存排序
    savePropertyf() {
      noticeMenu.saveSort(this.sortList, res => {
        if (res.state) {
          this.$notify({
            type: 'success',
            message: res.message,
            duration: 2000
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    // 是否置顶
    handleTopStatus(val) {
      let title
      title = val.topStatus == 1 ? '取消置顶' : '置顶'
      console.log(val)
      let data = {
        id: val.id
      }
      noticeMenu.Noticeflag(data, res => {
        if (res.state) {
          this.$message({
            message: title + '成功',
            type: 'success'
          })
        }
      })
    },
    // 公告状态
    handleStatus(val) {
      let title
      title = val.status == 1 ? '公告状态关闭' : '公告状态开启'
      let data = {
        id: val.id
      }
      noticeMenu.NoticeStatus(data, res => {
        if (res.state) {
          this.$message({
            message: title + '成功',
            type: 'success'
          })
          // this.loadData()
        }
      })
    },
    // 重置
    clearQuerys() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.loadData(1)
    },
    // 获取list
    loadData(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.loading = true
      this.tableData = []
      this.queryParam.querys = []
      this.queryParam.sorter = [
        {
          direction: 'DESC',
          property: 'createTime'
        }
      ]
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      noticeMenu.getNotice(this.queryParam, res => {
        this.total = res.total
        this.tableData = res.rows
        this.tableData.forEach(item => {
          this.NoticeTypeList.forEach(ee => {
            if (item.type == ee.value) {
              item['typeName'] = ee.name
            }
          })
        })
        this.$set(this.tableData)
        this.loading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.type-tag {
  margin: 0 2px;
}
</style>
