<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类" prop="cateId">
          <el-select   v-model="queryParams.cateId" placeholder="请选择分类" clearable size="small" >
            <el-option
                    v-for="item in cates"
                    :key="item.cateId"
                    :label="item.cateName"
                    :value="item.cateId"
                  />
            </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:article:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  height="500" border v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="articleId" />
      <el-table-column label="分类" align="center" prop="cateName" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="封面图片" align="center" width="120">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.imageUrl" width="100">
            <img :src="scope.row.imageUrl" style="max-width: 150px;">
          </el-popover>
        </template>
      </el-table-column>
       <el-table-column label="是否推荐" align="center" prop="isTui" >
          <template slot-scope="scope">
              <el-tag prop="isTui" v-for="(item, index) in tuiOptions"    v-if="scope.row.isTui==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
        </el-table-column>
      <el-table-column label="浏览数" align="center" prop="views" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="['store:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:article: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="80px">
        <el-form-item label="分类" prop="cateId">
           <el-select style="width: 200px" v-model="form.cateId" placeholder="请选择分类" clearable size="small" >
            <el-option
                    v-for="item in cates"
                    :key="item.cateId"
                    :label="item.cateName"
                    :value="item.cateId"
                  />
            </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="封面图片" prop="imageUrl">
          <Material v-model="imageArr" type="image" :num="1" :width="150" :height="150" />
        </el-form-item>
         <el-form-item label="是否推荐">
           <el-radio-group v-model="form.isTui">
            <el-radio :label="item.dictValue" v-for="item in tuiOptions" >{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="内容" prop="contents">
          <editor ref="myeditor"   @on-text-change="updateText" />
        </el-form-item>
        <el-form-item label="浏览数" prop="views">
           <el-input-number v-model="form.views"  :min="1"  label="请输入浏览数"></el-input-number>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
           <el-input-number v-model="form.sort"  :min="1" :max="1000" label="请输入排序"></el-input-number>
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.publishTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发布时间">
          </el-date-picker>
        </el-form-item>
      </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 { listArticle, getArticle, delArticle, addArticle, updateArticle, exportArticle } from "@/api/store/article";
import { getArticleCates  } from "@/api/store/articleCate";

import Editor from '@/components/Editor/wang';
import Material from '@/components/Material'
export default {
  name: "Article",
  components: {
    Editor,
    Material
  },
  watch: {
    imageArr: function(val) {
      this.form.imageUrl = val.join(',')
    },
  },
  data() {
    return {
      cates:[],
      imageArr:[],
      tuiOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cateId: null,
        title: null,
        imageUrl: null,
        isTui: null,
        isNews: null,
        contents: null,
        views: null,
        sort: null,
        publishTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cateId: [
          { required: true, message: "分类不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        imageUrl: [
          { required: true, message: "封面图不能为空", trigger: "blur" }
        ],
        publishTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    getArticleCates( ).then(response => {
        this.cates = response.data;
      });
    this.getDicts("article_is_tui").then((response) => {
      this.tuiOptions = response.data;
    });
    this.getList();
  },
  methods: {
    updateText(text){
      this.form.contents=text
    },
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: null,
        cateId: null,
        title: null,
        imageUrl: null,
        isTui: "1",
        isNews: null,
        contents: null,
        views: null,
        sort: null,
        createTime: null,
        updateTime: null,
        publishTime: null
      };
      this.imageArr=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.articleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      setTimeout(() => {
        this.$refs.myeditor.setText("");
      }, 200);
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids
      getArticle(articleId).then(response => {
        this.form = response.data;
        this.form.isTui = response.data.isTui.toString();
        if(this.form.imageUrl!=null){
          this.imageArr=this.form.imageUrl.split(",");
        }
        this.open = true;
        this.title = "修改文章";
        setTimeout(() => {
          if(this.form.contents==null){
            this.$refs.myeditor.setText("");
          }
          else{
            this.$refs.myeditor.setText(this.form.contents);
          }
          
        }, 200);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.articleId != null) {
            updateArticle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addArticle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$confirm('是否确认删除文章编号为"' + articleIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delArticle(articleIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有文章数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportArticle(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
