<template>
  <div class="app-container">
    <el-card>
      <div>
        <el-button
          v-if="checkPermission(['article_create'])"
          type="primary"
          icon="el-icon-plus"
          @click="handleCreate"
        >
          新增
        </el-button>
        <el-input
          v-model="listQuery.search"
          placeholder="文章名称"
          style="width: 300px"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >
          搜索
        </el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-refresh-left"
          @click="resetFilter"
        >
          重置
        </el-button>
      </div>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading"
        :data="articleList.results"
        border
        fit
        stripe
        highlight-current-row
        height="100"
        v-el-height-adaptive-table="{bottomOffset: 42}"
      >
        <el-table-column type="index" width="50"/>
        <el-table-column label="标题" prop="title">
        </el-table-column>
        <el-table-column label="作者" prop="author">
        </el-table-column>
        <el-table-column label="创建时间" prop="create_time" width="160">
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <el-link
              v-if="checkPermission(['article_update'])"
              type="primary"
              @click="handleEdit(scope)"
            >
              编辑
            </el-link>
            <el-link
              v-if="checkPermission(['article_delete'])"
              type="danger"
              @click="handleDelete(scope)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="articleList.count > 0"
        :total="articleList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :title="dialogType === 'edit' ? '编辑资讯' : '新增资讯'"
    >
      <el-form
        ref="Form"
        :model="params"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="params.title" placeholder="文章标题"/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="params.content" placeholder="文章内容"/>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="params.author" placeholder="作者"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否发布">
              <el-switch v-model="params.is_published"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶">
              <el-switch v-model="params.is_top"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {getArticles,getArticle, createArticle, updateArticle, deleteArticle} from "@/api/cms";
  import checkPermission from "@/utils/permission";
  import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
  const defaulteparams =
    {title:'',
    content:'',
    author:'',
    is_published:false,
    is_top:false
    };

  export default {
    name: "article",
    components: {Pagination},
    data() {
      return {
        defaulteparams: defaulteparams,
        articleList: {
          count: 0,
        },
        listQuery: {
          page: 1,
          page_size: 20,
        },
        params:{
          title:'',
          content:'',
          author:'',
          is_published:false,
          is_top:false,
        },
        listLoading: true,
        dialogVisible: false,
        dialogType: "new",
        rule1: {
          title: [{required: true, message: "请输入标题", trigger: "blur"}],
          contact: [{required: true, message: "请输入内容", trigger: "blur"}],
        },
      };
    },

    created() {
      this.getList();
    },

    methods: {
      checkPermission,

      //设备列表
      getList() {
        this.listLoading = true;
        getArticles(this.listQuery).then((response) => {
          if (response.data) {
            this.articleList = response.data;
          }
          this.listLoading = false;
        });
      },

      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },

      resetFilter() {
        this.listQuery = {
          page: 1,
          page_size: 20,
        };
        this.getList();
      },

      handleCreate() {
        this.params = Object.assign({}, defaulteparams);
        this.dialogType = "new";
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs["Form"].clearValidate();
        });
      },

      handleEdit(scope) {
        this.dialogType = "edit";
        getArticle(scope.row.id).then(res=>{
          if(res.code===200){
            this.params.id = res.data.id;
            this.params.title = res.data.title;
            this.params.content = res.data.content;
            this.params.author = res.data.author;
            this.params.is_published = res.data.is_published;
            this.params.is_top = res.data.is_top;
            this.dialogVisible = true;
          }
        })
      },

      handleDelete(scope) {
        this.$confirm("确认删除?", "警告", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "error",
        })
          .then( () => {
             deleteArticle(scope.row.id).then(res=>{
               if(res.code===200){
                 this.getList();
                 this.$message.success("删除成功");
               }
             });
          })
      },

      async confirm(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            const isEdit = this.dialogType === "edit";
            if (isEdit) {
              updateArticle(this.params.id, this.params).then((res) => {
                if (res.code >= 200) {
                  this.getList();
                  this.dialogVisible = false;
                  this.$message.success("成功");
                }
              });
            } else {
              createArticle(this.params).then((res) => {
                if (res.code >= 200) {
                  this.getList();
                  this.dialogVisible = false;
                  this.$message.success("成功");
                }
              });
            }
          } else {
            return false;
          }
        });
      },
    },
  };
</script>

