<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-news__notice}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model.trim="dataForm.noticeTitle"
                    placeholder="通知标题"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="所属模块:" prop="noticeCategory">
          <div class="block">
            <el-cascader v-model="noticeCategoryList" placeholder="试试搜索：一键服务" :props="optionProps" :options="moduleCategoryList" filterable clearable></el-cascader>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $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 type="selection"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column label="序号"
                         type="index"
                         show-overflow-tooltip
                         align="center"
                         width="50"></el-table-column>
        <el-table-column prop="noticeTitle"
                         label="通知标题"
                         header-align="center"
                         align="center"
                         show-overflow-tooltip
                         min-width="200"></el-table-column>
        <el-table-column prop="categoryName"
                         label="所属模块"
                         header-align="center"
                         align="center"
                         width="150"
                         ></el-table-column>
        <el-table-column prop="mobile"
                         label="联系电话"
                         header-align="center"
                         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="noticeReleaseState"
                         label="已发布"
                         header-align="center"
                         :formatter="showIsPublishFormatter"
                         align="center"
                         width="100"></el-table-column>
        <el-table-column prop="releaseTime"
                         label="发布时间"
                         header-align="center"
                         align="center"
                         width="180"></el-table-column>
        <el-table-column prop="sort"
                         label="排序"
                         header-align="center"
                         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)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('news:notice:publish') && scope.row.noticeReleaseState === '0'"
                       type="text"
                       size="small"
                       @click="noticePublishHandle(scope.row.id)">发布</el-button>
            <el-button v-if="$hasPermission('news:notice:delete')"
                       type="text"
                       size="small"
                       @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</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 './notice-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/news/notice/page',
        getDataListIsPage: true,
        deleteURL: '/news/notice',
        deleteIsBatch: true
      },
      dataForm: {
        noticeTitle: ''
      },
      optionNoticeCategoryCode: [],
      moduleCategoryList: [],
      noticeCategoryList: [],
      optionProps: {
        checkStrictly: true,
        value: 'categoryCode'
      }
    }
  },
  watch: {
    'noticeCategoryList': function (val) {
      if (val.length !== 0) {
        this.dataForm.noticeCategory = val[val.length - 1]
      } else {
        this.dataForm.noticeCategory = ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    // 根据数据字典类型notice_category 获取简版数据字典列表，用于页面下拉菜单
    this.getModuleCategoryList()
  },
  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.grid) {
        return row.grid
      }
      if (row.community) {
        return row.community
      }
      if (row.street) {
        return row.street
      }
    },
    getListSimpleByDictType () {
      this.$http.get(`/news/notice/listSimple/notice_category`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.optionNoticeCategoryCode = res.data
      }).catch(() => {})
    },
    getModuleCategoryList () {
      this.$http.get('/news/modulecategory/getListToShow').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.getSelectedNode(res.data, res.data)
        this.moduleCategoryList = res.data
      }).catch(() => { })
    },
    getSelectedNode (nodeData, parent) {
      nodeData.forEach(item => {
        if ('children' in item && item.children.length === 0) {
          delete item.children
        } else if ('children' in item && item.children.length) {
          this.getSelectedNode(item.children, item)
        }
      })
    }
  }
}
</script>
