<template>
  <div class="page">
      <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
		     <el-form-item label="标题" prop="title">
                <el-input size="small" v-model="searchForm.title" placeholder="标题" clearable></el-input>
		     </el-form-item>
        <el-form-item label="查阅状态" prop="readFlag">
          <el-select size="small" v-model="searchForm.readFlag" placeholder="查阅状态" clearable>
            <el-option
              v-for="item in $dictUtils.getDictList('oa_notify_read')"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select size="small" v-model="searchForm.type" placeholder="类型" clearable>
            <el-option
              v-for="item in $dictUtils.getDictList('oa_notify_type')"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布者" prop="createName">
          <UserSelect :limit='1' :userName="searchForm.createName" @getValue='(value, label) => {searchForm.createName = label}'></UserSelect>
        </el-form-item>
        <el-form-item label="创建时间" prop="createDates">
          <el-date-picker
            placement="bottom-start"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="searchForm.createDates"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
          <el-form-item>
            <el-button  type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
            <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
      </el-form>
       <div class="bg-white top">

         <vxe-toolbar :refresh="{query: refreshList}" custom>
           <template #buttons>
             <el-button type="primary" size="small" @click="readAll()">全部标记已读</el-button>
             <el-button :disabled="$refs.notifyTable && $refs.notifyTable.getCheckboxRecords().length === 0" type="warning" size="small" @click="portionRead()">标记为已读</el-button>
           </template>
         </vxe-toolbar>
         <el-tabs v-model="activeName" @tab-click="handleClick">
           <el-tab-pane label="全部" name="all"></el-tab-pane>
           <el-tab-pane label="未读" name="read"></el-tab-pane>
           <el-tab-pane label="已读" name="un_read"></el-tab-pane>
         </el-tabs>
     <div style="height: calc(100% - 80px);">
        <vxe-table
            border="inner"
            auto-resize
            resizable
            height="auto"
            :loading="loading"
            size="small"
            ref="notifyTable"
            show-header-overflow
            show-overflow
            highlight-hover-row
            :menu-config="{}"
            :print-config="{}"
            :import-config="{}"
            :export-config="{}"
            @sort-change="sortChangeHandle"
            :sort-config="{remote:true}"
            :data="dataList"
            :checkbox-config="{}">
            <vxe-column type="seq" width="60" title="序号"></vxe-column>
            <vxe-column type="checkbox"  width="40px"></vxe-column>
            <vxe-column min-width="180px" title="标题" field="title" align="center">
              <template slot-scope="scope">
                 <el-link  type="primary" :underline="false" @click="view(scope.row.id)">{{scope.row.title}}</el-link>
              </template>
            </vxe-column>
            <vxe-column min-width="180px" title="类型" field="type" align="center">
                <template slot-scope="scope">
                  {{ $dictUtils.getDictLabel("oa_notify_type", scope.row.type, '-') }}
                </template>
            </vxe-column>

            <vxe-column min-width="180px" title="查阅状态" field="status" align="center">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.readFlag === '1'"> {{ $dictUtils.getDictLabel("oa_notify_read", scope.row.readFlag, '-') }}</el-tag>
                <el-tag type="danger" v-if="scope.row.readFlag === '0'"> {{ $dictUtils.getDictLabel("oa_notify_read", scope.row.readFlag, '-') }}</el-tag>
              </template>
            </vxe-column>
             <vxe-column min-width="180px" title="发布者" field="createBy.name" align="center"></vxe-column>
          <vxe-column min-width="180px" title="通告时间" field="beginDate" align="center" >
            <template slot-scope="scope">
                 <span v-if="scope.row.notifyType === '0'">
                   {{commonJS.isNotEmpty(scope.row.beginDate) && commonJS.isNotEmpty(scope.row.endDate)
                   ?
                   moment(scope.row.beginDate).format('YYYY-MM-DD') + " 至 " + moment(scope.row.endDate).format('YYYY-MM-DD')
                   :
                   ''}}
                 </span>
              <span v-else-if="scope.row.notifyType === '1'">
                   长期通告
                 </span>
            </template>
          </vxe-column>
          <vxe-column  min-width="180px"title="创建时间" field="createDate" align="center" ></vxe-column>
            <vxe-column title="操作" width="80" fixed="right" align="center">
                <template  slot-scope="scope">
                    <el-button  type="text" size="mini" @click="view(scope.row.id)">查看</el-button>
                </template>
            </vxe-column>
        </vxe-table>
        <vxe-pager
          background
          size="small"
          :current-page="tablePage.currentPage"
          :page-size="tablePage.pageSize"
          :total="tablePage.total"
          :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="currentChangeHandle">
        </vxe-pager>
      </div>
        <!-- 弹窗, 新增 / 修改 -->
    <NotifyForm  ref="notifyForm" @refreshDataList="refreshList"></NotifyForm>
  </div>
  </div>
</template>

<script>
  import NotifyForm from './NotifyForm'
  import NotifyService from '@/api/notify/NotifyService'
  import UserSelect from '@/components/userSelect'
  export default {
    data () {
      return {
        searchForm: {
          title: '',
          type: '',
          createName: '',
          createDates: [],
          readFlag: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false,
        activeName: ''
      }
    },
    components: {
      NotifyForm,
      UserSelect
    },
    notifyService: null,
    created () {
      this.notifyService = new NotifyService()
      this.activeName = 'all'
    },
    activated () {
      this.refreshList()
      this.activeName = 'all'
    },

    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.notifyService.myNotifyList({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          isSelf: true,
          isClose: '0',
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
  // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      // 查看
      view (id) {
        this.$refs.notifyForm.init('read', id)
      },
      resetSearch () {
        this.activeName = 'all'
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },

      // 全部已读
      readAll () {
        this.$confirm(`确定将全部通告标记为已读吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.notifyService.readAll().then(({data}) => {
            this.loading = false
            this.$message({
              message: data,
              type: 'success',
              duration: 1500
            })
            this.refreshList()
          })
        })
      },

      // 部分已读
      portionRead (id) {
        let ids = id || this.$refs.notifyTable.getCheckboxRecords().map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定将选定通告标记为已读吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.notifyService.portionRead(ids).then(({data}) => {
            this.loading = false
            this.$message({
              message: data,
              type: 'success',
              duration: 1500
            })
            this.refreshList()
          })
        })
      },
      handleClick (event) {
        if (event.name === 'all') {
          this.searchForm.readFlag = ''
        }
        if (event.name === 'read') {
          this.searchForm.readFlag = '0'
        }
        if (event.name === 'un_read') {
          this.searchForm.readFlag = '1'
        }
        this.refreshList()
      }
    }
  }
</script>
