<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="动态标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入动态标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['progress:realtimeNews:add']"
        >新增动态</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="realtimeNewsList">
      <el-table-column label="序号" align="center" prop="id" width="80" />
      <el-table-column label="封面图片" align="center" prop="coverImage" width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.coverImage"
            :src="scope.row.coverImage"
            :preview-src-list="[scope.row.coverImage]"
            style="width: 60px; height: 60px"
            fit="cover"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
        min-width="150"
      />
      <el-table-column label="简介" align="center" prop="summary" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="中文内容" align="center" prop="contentCn" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="英文内容" align="center" prop="contentEn" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="是否置顶" align="center" prop="isTop" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isTop === '1' ? 'warning' : 'info'">
            {{ scope.row.isTop === '1' ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['progress:realtimeNews:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.isTop === '1' ? 'el-icon-bottom' : 'el-icon-top'"
            @click="handleTop(scope.row)"
            v-hasPermi="['progress:realtimeNews:edit']"
          >{{ scope.row.isTop === '1' ? '取消置顶' : '置顶' }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['progress:realtimeNews:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入动态标题" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介" prop="summary">
              <el-input v-model="form.summary" type="textarea" placeholder="请输入简介" :rows="2" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="封面图片" prop="coverImage">
              <image-upload v-model="form.coverImage" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布时间" prop="publishTime">
              <el-date-picker
                v-model="form.publishTime"
                type="datetime"
                placeholder="选择发布时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶">
              <el-radio-group v-model="form.isTop">
                <el-radio label="0">否</el-radio>
                <el-radio label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="中文内容" prop="contentCn">
              <editor v-model="form.contentCn" :min-height="300"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="英文内容" prop="contentEn">
              <editor v-model="form.contentEn" :min-height="300"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="2" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!isView">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRealtimeNews, getRealtimeNews, delRealtimeNews, addRealtimeNews, updateRealtimeNews, updateTopStatus } from "@/api/realtimeNews"

export default {
  name: "RealtimeNews",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      realtimeNewsList: [],
      title: "",
      open: false,
      isView: false,
      dateRange: [],
      baseUrl: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        status: undefined
      },
      form: {},
      rules: {
        title: [
          { required: true, message: "动态标题不能为空", trigger: "blur" }
        ],
        publishTime: [
          { required: true, message: "发布时间不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.baseUrl = process.env.VUE_APP_BASE_API
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.params['beginTime'] = this.dateRange[0]
        this.queryParams.params['endTime'] = this.dateRange[1]
      }
      listRealtimeNews(this.queryParams).then(response => {
        this.realtimeNewsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        summary: undefined,
        coverImage: undefined,
        publishTime: undefined,
        contentCn: undefined,
        contentEn: undefined,
        isTop: "0",
        status: "0",
        remark: undefined
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.isView = false
      this.title = "新增实时动态"
    },
    handleView(row) {
      this.reset()
      const id = row.id
      getRealtimeNews(id).then(response => {
        this.form = response.data
        this.open = true
        this.isView = true
        this.title = "查看实时动态详情"
      })
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getRealtimeNews(id).then(response => {
        this.form = response.data
        this.open = true
        this.isView = false
        this.title = "修改实时动态"
      })
    },
    handleTop(row) {
      const isTop = row.isTop === '1' ? '0' : '1'
      const msg = isTop === '1' ? '置顶' : '取消置顶'
      this.$modal.confirm('是否确认' + msg + '该动态？').then(function() {
        return updateTopStatus(row.id, isTop)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(msg + "成功")
      }).catch(() => {})
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateRealtimeNews(this.form).then(() => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRealtimeNews(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除实时动态编号为"' + id + '"的数据项？').then(function() {
        return delRealtimeNews(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>
