<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="title">
        <el-input  v-model="queryParams.title" placeholder="请输入文章标题" clearable size="small"/>
      </el-form-item>
      <el-form-item label="文章类型" prop="type">
        <el-select v-model="queryParams.navId" placeholder="请选择文章类型" clearable size="small">
          <el-option
            v-for="item in articleTypeOptions"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </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.type" placeholder="请选择是否显示" clearable size="small">
          <el-option
            v-for="item in putIndexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </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="isIndexFormat"/>
      <el-table-column label="是否英文" align="center" prop="isEnglish" :formatter="isEnglishFormat"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <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>
          <span v-show="(scope.row.approvalUserId===userId || isAdmin) && scope.row.status === '3'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePutIndex(scope.row)"
          >推首页
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOffline(scope.row)"
          >下线
          </el-button>
          </span>
        </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"
    />

    <el-dialog title="编辑详情" :visible="putIndexShow" width="1100px" :close-on-click-modal="false" @close="detailsClose()"
               append-to-body
    >
      <el-form :model="formData" size="medium" label-width="100px">
            <!--            <el-form-item label="详情标题" prop="title">-->
            <el-form-item label="文章标题">
              <el-input disabled v-model="formData.title" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>

        <el-form-item label="文章标题">
            <el-select v-model="formData.type" placeholder="推送模块" clearable :style="{width: '100%'}">
              <el-option
                v-for="item in putIndexOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
      </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="doPutIndex">确 定</el-button>
          <el-button @click="detailsClose">取 消</el-button>
        </div>
    </el-dialog>


  </div>
</template>

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

export default {
  name: 'Article',
  components: {
    Editor
  },
  data() {
    return {
      userId: store.getters.userId,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      putIndexShow: false,
      formData: {},
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      putIndexOptions: [
        {
          value: "1",
          label: '新闻中心'
        },
        {
          value: "2",
          label: '科学研究'
        },
        {
          value: "3",
          label: '学术交流'
        },
        {
          value: "4",
          label: '党史教育'
        },
        {
          value: "5",
          label: '教学工作'
        }
      ],
      // 弹出层标题
      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' }
        ]
      }
    }
  },
  mounted() {

    this.getList()
  },
  created() {


    let roles = JSON.parse(sessionStorage.getItem("roles"))
    console.log(roles,"roles")
    for (const role of roles) {
      console.log(role)
      if (role.roleName == 'admin'){
        this.isAdmin = true
      }
    }



    getArticleNav().then(res=>{
      let data = res.data
      this.articleTypeOptions = data
    })
    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) {
      for (let articleTypeOption of this.articleTypeOptions) {
        if (row.navId == articleTypeOption.id){
          return articleTypeOption.title
        }
      }



      // 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)
    },
    // 是否英文字典翻译
    isIndexFormat(row, column) {
      for (let putIndexOption of this.putIndexOptions) {
        if (row.type == putIndexOption.value){
          return putIndexOption.label
        }
      }
    },
    // 状态： 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)
    },
    //推首页
    handlePutIndex(row) {
      this.formData = {
        id: row.id,
        type: row.type == "0" ? null : row.type,
        title: row.title
      }
      console.log(this.formData)
      this.putIndexShow = true
    },
    doPutIndex() {
      console.log(this.formData)
      let data = this.formData
      putIndex(data).then(res=>{
        console.log(res)
      })
      this.putIndexShow = false

    },
    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)
      })
    },
    detailsClose() {
      this.putIndexShow = false
    }
  }
}
</script>
