<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-news__notice}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataListSearch()">
        <el-form-item label="所属机构">
          <el-cascader v-model="deptIdList"
                       :options="options"
                       :props="{ checkStrictly: true }"
                       clearable></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-form-item label="通知标题">
            <el-input v-model="dataForm.noticeTitle" @keyup.native="btKeyUpnoticeTitle"
                      placeholder="通知标题"
                      clearable></el-input>
          </el-form-item>
          </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataListSearch()">{{ $t('query') }}</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button v-if="$hasPermission('news:notice:save')"
                     type="primary"
                     @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('news:notice:delete')"
                     type="danger"
                     @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item> -->
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                @selection-change="dataListSelectionChangeHandle"
                style="width: 100%;">
        <el-table-column label="序号" header-align="center" align="center" width="50px">
            <template slot-scope="scope">
                {{scope.$index+1}}
            </template>
        </el-table-column>
        <el-table-column prop="noticeTitle"
                         label="通知标题"
                         header-align="center"
                         align="center"
                         min-width="400"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="deptName"
                         label="通知所属部门"
                         header-align="center"
                         min-width="200"
                         show-overflow-tooltip
                         align="center"></el-table-column>
        <el-table-column prop="readingAmount"
                         label="阅读量"
                         header-align="center"
                         align="center"
                         width="100"></el-table-column>
        <el-table-column prop="creatorName"
                         label="创建人"
                         header-align="center"
                         min-width="200"
                         show-overflow-tooltip
                         align="center"></el-table-column>
        <el-table-column prop="createdTime"
                         label="创建时间"
                         header-align="center"
                         width="180"
                         align="center"></el-table-column>
        <el-table-column prop="noticeReleaseState"
                         label="已发布"
                         header-align="center"
                         :formatter="showIsPublishFormatter"
                         align="center"
                         width="100"></el-table-column>
        <el-table-column prop="releaseTime"
                         label="发布时间"
                         header-align="center"
                         width="180"
                         align="center"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('news:notice:update')"
                       type="text"
                       size="small"
                       @click="addOrUpdateHandle(scope.row.id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :page-size="limit"
                     :total="total"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible"
                     ref="addOrUpdate"
                     @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './allnotice-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/news/notice/pageForReadOnly',
        getDataListIsPage: true,
        deleteURL: '/news/notice',
        deleteIsBatch: true
      },
      dataForm: {
        noticeTitle: '',
        deptId: ''
      },
      options: [],
      deptIdList: []
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    // 发布通知
    noticePublishHandle (id) {
      this.$confirm(this.$t('prompt.info', { 'handle': '发布' }), this.$t('发布'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post(`/news/notice/publish/${id}`).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.getDataList()
            }
          })
        }).catch(() => { })
      }).catch(() => { })
    },
    showIsPublishFormatter: function (row, column) {
      if (row.noticeReleaseState === '0') {
        return '否'
      }
      return '是'
    },
    showDeptNameFormatter: function (row, column) {
      if (row.dept) {
        return row.dept
      }
      if (row.community) {
        return row.community
      }
      if (row.street) {
        return row.street
      }
    },
    btKeyUpnoticeTitle(e){
      e.target.value = e.target.value.replace(/[`~!#$%^&*()_\+=<>?:"{}|~！#￥%……&*（）={}|《》？：“”【】\\[\]、；‘’，。、\s+]/g, '')
      this.dataForm.noticeTitle = e.target.value
    },
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length !== 0) {
        this.dataForm.deptId = val[val.length - 1]
      } else {
        this.dataForm.deptId = ''
      }
    }
  },
  created () {
    var paa = {deptType:'party'}
    this.$http
      .post(`/sys/user/deptOptions/getByLoginUserNew`,paa)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => { })
  }
}
</script>
