<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.article_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="scope">
        <el-button type="text"
                   size="small"
                   icon="el-icon-view"
                   @click="preview(scope.row)">预览
        </el-button>
      </template>
      <template slot="image" slot-scope="scope">
        <img style="width: 140px;height: 80px;" :src="scope.row.image" @click="openView(scope.row.image)">
      </template>
    </avue-crud>
    <el-dialog :modal-append-to-body="false" title = "文章预览" :visible.sync = "previewVisiable" width="55%"
               :close-on-click-modal="false">
      <div>
        <div class="borderline">
          <div class="header">
            <el-header>{{ previewForm.title }}</el-header>
          </div>
          <hr>
          <!--<el-scrollbar style="height:100%">-->
          <div>
            <div id="updatetime">更新时间：{{ previewForm.updateTime }}</div>
            <img :lazy='true' :src="previewForm.image" style="width:300px; height:200px; margin-left: 8px;" align="right" ></img>
            <span id="main" v-html="previewForm.content"></span>
          </div>
          <hr>
          <!--</el-scrollbar>-->
        </div>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/estate/article";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        previewVisiable: false,
        previewForm: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 50,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: false,
          index: false,
          viewBtn: false,
          selection: true,
          dialogClickModal: false,
          menuWidth:350,
          column: [
            {
              label: "文章标题",
              prop: "title",
              search:true,
              rules: [{
                required: true,
                message: "请输入文章标题",
                trigger: "blur"
              }]
            },
            {
              label: "通知内容",
              prop: "content",
              hide:true,
              component: 'AvueUeditor',
              options: {
                action: '/api/blade-resource/oss/endpoint/put-file',
                props: {
                  res: "data",
                  url: "link",
                }
              },
              minRows: 6,
              span: 24,
            },
            {
              label: "阅读数量",
              prop: "reader",
              rules: [{
                required: false,
                message: "请输入阅读数量",
                trigger: "blur"
              }]
            },
            {
              label: "封面图",
              prop: "image",
              type:"upload",
              slot: true,
              width:200,
              span: 24,
              listType: 'picture-img',
              propsHttp: {
                url: 'data'
              },
              action: "/api/blade-resource/oss/endpoint/put-file",
              tip: "只能上传jpg/png图片，且不超过500kb",
              rules: [{
                required: false,
                message: "请输入图片",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.article_add, false),
          viewBtn: this.vaildData(this.permission.article_view, false),
          delBtn: this.vaildData(this.permission.article_delete, false),
          editBtn: this.vaildData(this.permission.article_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        let Base64 = require('js-base64').Base64;
        row.article =Base64.encode(row.article);
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        let Base64 = require('js-base64').Base64;
        row.article =Base64.encode(row.article);
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      preview(row) {
        this.previewForm = row;
        this.previewVisiable = true;
      },
      openView(url) {
        var data = [{
          thumbUrl: url,
          url: url
        }];
        this.$ImagePreview(data, 0);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
  .header {
    background: #2B6695;
    background-size:cover;
    border-radius: 6px 6px 6px 6px;
    box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5);
    color: #FFFFFF;
    font-family:"微软雅黑", "宋体", "黑体", Arial;
    font-size: 18px;
    font-weight: bold;
    height: 40px;
    line-height: 25px;
    text-align: center;
    margin: 18px 0 !important;
    padding: 8px 0 5px 5px;
    text-shadow: 2px 2px 3px #222222;
  }
  .borderline {
    padding: 10px;
  }
  #main p{
    line-height: 26px;
    text-indent: 2em;
  }
  #updatetime{
    font-weight: bold;
    text-align: right;
    margin-top: 20px;
    margin-bottom: 15px;
  }
</style>
