<!-- 订单 -->
<template>
  <div class="order">
    <template v-if="!addOrUpdateVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
        <el-form-item label="通知类型">
          <el-select
            v-model="dataForm.type"
            placeholder="请选择">
            <el-option
              v-for="item in optionsType"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="dataForm.title" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryData()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="title"
          header-align="center"
          align="center"
          width="180"
          :show-tooltip-when-overflow="true"
          label="公告主题名称">
        </el-table-column>
        <el-table-column
          prop="levelName"
          header-align="center"
          align="center"
          label="级别">
        </el-table-column>
        <el-table-column
          prop="content"
          header-align="center"
          align="center"
          width="200"
          :show-tooltip-when-overflow="true"
          label="公告内容">
        </el-table-column>
        <el-table-column
          prop="attachList"
          header-align="center"
          align="center"
          min-width="200"
          label="附件">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachList" style="display: inline">
              <span v-if="index > 0">,</span>
              <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="isRead"
          header-align="center"
          align="center"
          label="是否已读">
          <template slot-scope="scope">
            <span v-if="Number(scope.row.isRead) === 0" style="color: red">未读</span>
            <span v-else>已读</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="publisherName"
          header-align="center"
          align="center"
          label="发布人">
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          width="160"
          label="发布时间">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          width="200"
          :show-tooltip-when-overflow="true"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="detailHandle(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗,详情 -->
    <detail v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getAnnouncementList" @onChose="onChose"></detail>
    <!-- 文件预览 -->
    <preview-component v-if="previewVisible" ref="preview"/>
  </div>
</template>

<script>
  import Detail from './announcement-detail'
  import { getAnnouncementList } from '@/api/msg'
  import { getCusList } from '@/api/cus'
  import { downloadUrl } from '@/api/file'
  import {readNotice} from '@/utils/msg'
  import PreviewComponent from '../common/preview-component'
  export default {
    name: 'announcement',
    components: {
      PreviewComponent,
      Detail
    },
    created () {
      this.optionsState = this.$store.state.common.approveStates
      this.queryData()
    },
    computed: {
      // 监听消息ID
      listenNotice () {
        return this.$route.params.notice
      }
    },
    watch: {
      listenNotice: function (notice) {
        console.log('notice = ' + JSON.stringify(notice))
        if (!notice) return
        this.dataForm.noticeId = notice.noticeId
        this.dataForm.businessId = notice.businessId
        this.queryData()
      }
    },
    data () {
      return {
        addOrUpdateVisible: false,
        previewVisible: false,
        dataForm: {type: 1},
        optionsType: [
          {
            code: 1, value: '公告'
          },
          {
            code: 3, value: '业务更改通知'
          }
        ],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        downloadUrl: downloadUrl,
        dataListLoading: false,
        dataListSelections: [],
        optionsState: [],
        optionsCustomer: []
      }
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
      },
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getAnnouncementList()
      },
      // 获取数据列表
      getAnnouncementList () {
        this.dataList = []
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'createTime': this.dataForm.createTime ? this.dataForm.createTime : null,
          'state': this.dataForm.state ? this.dataForm.state : null,
          'businessId': this.dataForm.businessId ? this.dataForm.businessId : null,
          'type': this.dataForm.type ? this.dataForm.type : 1
        }
        getAnnouncementList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getAnnouncementList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getAnnouncementList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 远程方法：获取客户列表
      async remoteCustomer (query) {
        if (!query) {
          query = ''
        }
        await getCusList({'customerName': query}).then(({data}) => {
          if (data && data.code === '200') {
            this.optionsCustomer = []
            data.data.records.forEach((item) => {
              this.optionsCustomer.push({
                code: item.customerId,
                value: item.customerName
              })
            })
          }
        })
      },
      // 详情
      detailHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.noticeId)
        })
        if (row.noticeId) {
          readNotice(this, row.noticeId)
        }
      },
      // 预览
      previewFile (fileName, url) {
        this.previewVisible = true
        this.$nextTick(() => {
          this.$refs.preview.init(fileName, url)
        })
      }
    }
  }
</script>

<style scoped>
</style>
