<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="标题" prop="titleName">
        <el-input
          v-model="queryParams.titleName"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop" placeholder="是否置顶" clearable>
          <el-option
            v-for="dict in isTops"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否政策推荐" prop="isGovernment">
        <el-select v-model="queryParams.isGovernment" placeholder="是否政策推荐" clearable>
          <el-option
            v-for="dict in isGovernments"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['jnb:information:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jnb:information:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jnb:information:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jnb:information:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="标题" align="center" prop="titleName" show-overflow-tooltip/>
      <el-table-column label="是否政策推荐" align="center" prop="isGovernment">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isGovernment == '1'">是</el-tag>
          <el-tag type="warning" v-if="scope.row.isGovernment == '0'">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isTop == '1'">是</el-tag>
          <el-tag type="warning" v-if="scope.row.isTop == '0'">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="likeNum" show-overflow-tooltip/>
      <el-table-column label="浏览数" align="center" prop="watchNum" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jnb:information:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jnb:information: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标题" prop="titleName">
              <el-input v-model="form.titleName" placeholder="请输入标题" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否置顶" prop="isTop">
              <el-switch
                v-model="form.isTop"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否政策推荐" prop="isGovernment">
              <el-switch
                v-model="form.isGovernment"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="浏览数" prop="watchNum">
              <el-input-number v-model="form.watchNum" :min="0" :max="999999999" label="浏览数"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点赞数" prop="likeNum">
              <el-input-number v-model="form.likeNum" :min="0" :max="999999999" label="点赞数"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="正文" prop="textDetails">
              <editor v-model="form.textDetails" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="图片" prop="imgUrlList">
              <image-upload v-model="form.imgUrlList"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {listServer, getServer, delServer, addServer, updateServer} from "@/api/information/information";

export default {
  name: "information",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      isGovernments: [{label: '是', value: '1'}, {label: '否', value: '0'}],
      isTops: [{label: '是', value: '1'}, {label: '否', value: '0'}],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资讯表格数据
      serverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        titleName: null,
        isGovernment: null,
        isTop: null,
        type: this.$route.query.type,
        delFlag: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        titleName: [
          {required: true, message: "标题不能为空", trigger: "blur"},
        ],
        titleDetails: [
          {required: true, message: "正文不能为空", trigger: "blur"},
        ],
        isGovernment: [
          {required: true, message: "政策推荐不能为空", trigger: "change"},
        ],
        isTop: [
          {required: true, message: "置顶不能为空", trigger: "change"},
        ],
        likeNum: [
          {required: true, message: "点赞数不能为空", trigger: ["change",'blur']},
        ],
        watchNum: [
          {required: true, message: "浏览数不能为空", trigger: ["change",'blur']},
        ],
      }
    };
  },
  created() {
    this.queryParams.type = this.$route.query.type;
    this.getList();
  },
  methods: {
    /** 查询资讯列表 */
    getList() {
      this.loading = true;
      listServer(this.queryParams).then(response => {
        this.serverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        titleName: null,
        textDetails: null,
        type: this.$route.query.type,
        createTime: null,
        watchNum: 0,
        likeNum: 0,
        isTop: '0',
        isGovernment: '0',
        updateBy: null,
        updateTime: null,
        imgUrlList: '',
        delFlag: 0,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      await this.reset();
      this.open = true;
      let str = ''
      if (this.$route.query.type == 10) {
        str = '惠民通'
      } else if (this.$route.query.type == 11) {
        str = '法律下乡'
      } else if (this.$route.query.type == 12) {
        str = '资讯'
      }
      this.title = "新增" + str;
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      await this.reset();
      const id = row.id || this.ids
      getServer(id,row.type).then(response => {
        this.form = response.data;
        this.open = true;
        let str = ''
        if (this.$route.query.type == 10) {
          str = '惠民通'
        } else if (this.$route.query.type == 11) {
          str = '法律下乡'
        } else if (this.$route.query.type == 12) {
          str = '资讯'
        }
        this.title = "修改" + str;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateServer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addServer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const type = this.$route.query.type
      this.$modal.confirm('是否确认删除数据项？').then(function () {
        return delServer(ids,type);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jnb/information/export', {
        ...this.queryParams
      }, `Excel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
