<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="站点类型" prop="siteType">
        <el-select v-model="queryParams.siteType" placeholder="请选择站点类型" clearable size="small">
          <el-option
            v-for="dict in siteTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文章类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择文章类型" clearable size="small">
          <el-option
            v-for="dict in articleTypeOptions"
            :key="dict.dictCode"
            :label="dict.dictLabel"
            :value="dict.dictCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop" placeholder="请选择是否置顶" clearable size="small">
          <el-option
            v-for="dict in isTopOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="isView">
        <el-select v-model="queryParams.isView" placeholder="请选择是否显示" clearable size="small">
          <el-option
            v-for="dict in isViewOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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"
          v-hasPermi="['sist:article:add']"
          @click="handleAdd"
        >新增
        </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="['sist:article: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="['sist:article: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="['sist:article:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ ((queryParams.pageInfo.pageNum - 1) * queryParams.pageInfo.pageSize + scope.$index + 1) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章标题" align="center" prop="title"/>
      <el-table-column label="站点类型" align="center" prop="siteType" :formatter="siteTypeFormat"/>
      <el-table-column label="文章类型" align="center" prop="type" :formatter="articleTypeFormat"/>
      <el-table-column label="发布人姓名" align="center" prop="publishUserName"/>
      <el-table-column label="是否置顶" align="center" prop="isTop" :formatter="isTopFormat"/>
<!--      <el-table-column label="是否显示" align="center" prop="isView" :formatter="isViewFormat"/>-->
      <el-table-column label="查看次数" align="center" prop="viewCount"/>
      <el-table-column label="缩略图" align="center" prop="imgurl">
        <template slot-scope="scope">
          <img style="width: 100%;" :src="scope.row.imgurl" alt="">
        </template>
      </el-table-column>
      <el-table-column label="是否英文" align="center" prop="isEnglish" :formatter="isEnglishFormat"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <!--      <template slot-scope="scope">-->
      <!--        <el-tag-->
      <!--          v-if="scope.row.status==1">待取件-->
      <!--        </el-tag>-->
      <!--        <el-tag-->
      <!--          type="warning" v-if="scope.row.status==2"-->
      <!--        >运输中-->
      <!--        </el-tag>-->
      <!--        <el-tag-->
      <!--          type="danger" v-if="scope.row.status==3"-->
      <!--          disable-transitions>待收取-->
      <!--        </el-tag>-->
      <!--        <el-tag-->
      <!--          type="success" v-if="scope.row.status==4"-->
      <!--          disable-transitions>完成-->
      <!--        </el-tag>-->
      <!--      </template>-->
      <el-table-column label="修改时间 " align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间 " align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <span v-show="scope.row.status === '1'|| scope.row.status === '4'|| scope.row.status === '5'">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['sist:article:edit']"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sist:article:remove']"
          >删除
          </el-button>
          </span>
          <el-button v-show="scope.row.approvalUserId===userId && scope.row.status === '2'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleApprova(scope.row)"
          >审批
          </el-button>
          <el-button v-show="scope.row.approvalUserId===userId && scope.row.status === '3'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleOffline(scope.row)"
          >下线
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import {listArticle, delArticle, exportArticle, offline} from "@/api/sist/article";
import Editor from '@/components/Editor';
import store from "@/store";

export default {
  name: "Article",
  components: {
    Editor,
  },
  data() {
    return {
      userId: store.getters.userId,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 站点类型 1： sist    2: lab 3: 双边都发字典
      siteTypeOptions: [],
      // 1: 2: 3: 根据导航字典
      articleTypeOptions: [],
      // 是否置顶字典
      isTopOptions: [],
      // 是否显示字典
      isViewOptions: [],
      // 是否英文字典
      isEnglishOptions: [],
      // 状态： 1：编辑中 2：待审核 ，3: 审核完成字典
      statusOptions: [],

      // 查询参数
      queryParams: {
        pageInfo: {
          pageNum: 1,
          pageSize: 10,
        },
        params: {},
        siteType: null,
        type: null,
        title: null,
        isTop: null,
        isView: null,
        status: null

      },
      // 表单校验
      rules: {
        siteType: [
          {required: true, message: "站点类型", trigger: "change"},
        ],
        type: [
          {required: true, message: "", trigger: "change"},
        ],
        content: [
          {required: true, message: "文章内容不能为空", trigger: "blur"},
        ],
        title: [
          {required: true, message: "文章标题不能为空", trigger: "blur"},
        ],
        publishTime: [
          {required: true, message: "发布时间", trigger: "blur"},
        ],
        publishUserName: [
          {required: true, message: "发布人姓名不能为空", trigger: "blur"},
        ],
        isTop: [
          {required: true, message: "是否置顶不能为空", trigger: "change"},
        ],
        isView: [
          {required: true, message: "是否显示不能为空", trigger: "change"},
        ],
        isEnglish: [
          {required: true, message: "是否英文不能为空", trigger: "blur"},
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("article_site_type").then(response => {
      this.siteTypeOptions = response.data;
    });
    this.getDicts("article_type").then(response => {
      this.articleTypeOptions = response.data;
    });
    this.getDicts("is_top").then(response => {
      this.isTopOptions = response.data;
    });
    this.getDicts("is_view").then(response => {
      this.isViewOptions = response.data;
    });
    this.getDicts("is_english").then(response => {
      this.isEnglishOptions = response.data;
    });
    this.getDicts("article_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 站点类型 1： sist    2: lab 3: 双边都发字典翻译
    siteTypeFormat(row, column) {
      return this.selectDictLabel(this.siteTypeOptions, row.siteType);
    },
    // 1: 2: 3: 根据导航字典翻译
    articleTypeFormat(row, column) {
      return this.selectDictCode(this.articleTypeOptions, row.type);
    },
    // 是否置顶字典翻译
    isTopFormat(row, column) {
      return this.selectDictLabel(this.isTopOptions, row.isTop);
    },
    // 是否显示字典翻译
    isViewFormat(row, column) {
      return this.selectDictLabel(this.isViewOptions, row.isView);
    },
    // 是否英文字典翻译
    isEnglishFormat(row, column) {
      return this.selectDictLabel(this.isEnglishOptions, row.isEnglish);
    },
    // 状态： 1：编辑中 2：待审核 ，3: 审核完成字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/article/add");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push("/article/edit/" + row.id);
    },
    /** 审批按钮操作 */
    handleApprova(row) {
      this.$router.push("/article/approve/" + row.id);
    },
    handleOffline(row) {
      let that_ = this
      this.$confirm('是否确认下线标题为:"' + row.title + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        that_.loading = true;
        return offline(row.id);
      }).then(res => {
        if (res.code == 200) {
          this.msgSuccess("下线成功");
          this.getList()
        }
        that_.loading = false;
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该文章的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有文章数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportArticle(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
