<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <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="状态 " prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态 " clearable>
          <el-option v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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-button type="primary" icon="el-icon-plus" :disabled="ids.length > 0" @click="handleOp" v-hasPermi="['system:news:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit" :disabled="ids.length != 1" @click="handleOp" v-hasPermi="['system:news:edit']">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" :disabled="ids.length == 0" @click="handleDelete" v-hasPermi="['system:news:remove']">批量删除</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" border :data="newsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资讯标题" align="center" prop="title" />
      <el-table-column label="封面图" align="center">
        <template slot-scope="scope">
          <el-image class="lsw_square" :fit="'contain'" style="max-width:80px;" :src="scope.row.pic" :preview-src-list="[scope.row.pic]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="阅读量" align="center" prop="views" />
      <el-table-column label="状态" align="center" prop="state" sortable width="120">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.state == 1">停用</el-tag>
          <el-tag type="success" v-if="scope.row.state == 0">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleOp(scope.row)" v-hasPermi="['system:news:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:news: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" />
    <!-- 添加或修改对话框 -->
    <edit v-if="dialogVisible" ref="dialog" @refresh="getList"></edit>
  </div>
</template>

<script>
import { listNews, getNews, delNews, addNews, updateNews, exportNews } from '@/api/system/news';
import edit from './edit';
export default {
  components: {
    edit
  },
  data() {
    return {
      //新增,修改弹出框
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资讯表格数据
      newsList: [],
      // 状态 字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        pic: null,
        contents: null,
        views: null,
        state: null,
        orderByColumn: 'id', //排序字段
        isAsc: 'desc' //排序方式
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('common_state').then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询资讯列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态 字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
    },
    /** 新增,修改按钮操作 */
    handleOp(row) {
      this.dialogVisible = true;
      const ids = row.id || this.ids;
      this.$nextTick(() => {
        this.$refs.dialog.init(ids);
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除资讯编号为"' + ids + '"的数据项?', '警告', { type: 'warning' })
        .then(() => {
          return delNews(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    }
  }
};
</script>
