<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="title">
        <el-select v-model="queryParams.type" placeholder="请选择新闻状态" clearable filterable class="se">
          <el-option v-for="t in typeList" :key="t.value" :label="t.label" :value="t.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="新闻状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择新闻状态" clearable filterable class="se">
          <el-option v-for="status in statusOptionList" :key="status.value" :label="status.label" :value="status.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['business:NEWS:add']">新增新闻</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="NEWSList" @selection-change="handleSelectionChange">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="标题" align="left" prop="title" width="220">
        <template slot-scope="scope">
          <div class="omit">{{ scope.row.title }}</div>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="110">
        <template slot-scope="scope">
          <div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">
            {{ searchType(scope.row.type) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="scope">
          <div class="omit">{{ scope.row.source }}</div>
        </template>
      </el-table-column>
      <el-table-column label="编辑日期" align="center" width="170" prop="updateTime" />
      <el-table-column label="审核日期" align="center" width="170" prop="auditTime" />
      <el-table-column label="发布日期" align="center" width="170" prop="releaseDate" />
      <el-table-column label="新闻状态" align="center" width="120" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0"><el-tag>草稿</el-tag></span>
          <span v-if="scope.row.status == 1"><el-tag type="warning">审核中</el-tag></span>
          <span v-if="scope.row.status == 2"><el-tag type="danger">审核驳回</el-tag></span>
          <span v-if="scope.row.status == 3"><el-tag type="success">已审核</el-tag></span>
          <span v-if="scope.row.status == 4"><el-tag type="success">已发布</el-tag></span>
          <span v-if="scope.row.status == 5"><el-tag type="info">已下架</el-tag></span>
          <span v-if="scope.row.status == 6"><el-tag>已置顶</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="170">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 4" size="mini" type="text" @click="handleDownOrUp(scope.row)" v-hasPermi="['business:NEWS:remove']">下架</el-button>
          <el-button v-if="scope.row.status == 0 || scope.row.status == 2" size="mini" type="text" v-hasPermi="['business:NEWS:remove']" @click="handleCommit(scope.row)">提交审核</el-button>
          <el-button v-if="scope.row.status == 0 || scope.row.status == 2 || scope.row.status == 5" size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['business:NEWS:remove']">修改</el-button>
          <el-button v-if="scope.row.status == 5 || scope.row.status == 3" size="mini" type="text" @click="handleDownOrUp(scope.row)" v-hasPermi="['business:NEWS:remove']">发布</el-button>
          <el-button v-if="scope.row.status == 6" size="mini" type="text" @click="hanleTop(scope.row)" v-hasPermi="['business:NEWS:remove']">取消置顶</el-button>
          <el-button v-else-if="scope.row.status == 4" size="mini" type="text" @click="hanleTop(scope.row)" v-hasPermi="['business:NEWS:remove']">置顶</el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty></el-empty>
      </template>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { listNEWS, getNEWS, delNEWS, addNEWS, updateNEWS, upload, setTop, newCommit, updateAndex, downloadpic } from '@/api/portal/news/NEWS.js';
import edit from './edit';
export default {
  name: 'NEWS',
  data() {
    return {
      typeList: [
        { label: '要闻咨询', value: '1' },
        { label: '政策法规', value: '2' },
        { label: '通知公告', value: '3' },
        { label: '政策解读', value: '4' },
        { label: '投资导航', value: '5' }
      ],
      // 根路径
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网站新闻表格数据
      NEWSList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        type: null,
        typeName: null,
        status: null,
      },
      statusOptionList: [
        { label: '草稿', value: 0 },
        { label: '审核中', value: 1 },
        { label: '审核驳回', value: 2 },
        { label: '已审核', value: 3 },
        { label: '已发布', value: 4 },
        { label: '已下架', value: 5 },
        { label: '已置顶', value: 6 }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    hanleTop(row) {
      row = { ...row };
      // //判断是否置顶
      if (row.status == '6') {
        row.status = 4;
      } else {
        row.status = 6;
      }
      setTop(row).then(res => {
        this.getList();
      });
    },
    /** 查询网站新闻列表 */
    getList() {
      listNEWS(this.queryParams).then(response => {
        this.NEWSList = response.rows;
        this.total = response.total;
      });
    },
    handleDownOrUp(row) {
      //修改发布、下架状态
      if (row.status == '4') {
        //已发布 已审核
        row.status = '5';
        updateNEWS(row).then(response => {
          this.$modal.msgSuccess('下架成功');
          this.open = false;
          this.getList();
        });
      } else if (row.status == '5' || row.status == '3') {
        //已下架
        row.status = '4';
        updateNEWS(row).then(response => {
          this.$modal.msgSuccess('发布成功');
          this.open = false;
          this.getList();
        });
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.type = '';
      this.queryParams.title = '';
      this.queryParams.status = '';
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.iframe({ obj: edit, param: {}, title: '添加网站新闻', width: '1050px', height: '750px' });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.iframe({ obj: edit, param: { id: id }, title: '修改网站新闻', width: '1050px', height: '750px' });
    },
    handleCommit(row) {
      let CbNewsVo = row;
      newCommit(CbNewsVo).then(response => {
        this.$modal.msgSuccess('提交审核成功!');
        this.getList();
      });
    },
    searchType(id) {
      let fined = this.typeList.find(item => item.value == id);
      if (fined.label != undefined) {
        return fined.label;
      } else {
        return '未选择类型！';
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除网站新闻编号为"' + ids + '"的数据项？')
        .then(function () {
          return delNEWS(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    }
  }
};
</script>
