<!--文章内容管理-->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--文章类型数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="typeName"
            clearable
            placeholder="请输入文章类型名称"
            prefix-icon="el-icon-search"
            size="small"
            style="margin-bottom: 20px"
            @change="getTypeList"
          />
        </div>
        <div class="head-container">
          <el-tree
            ref="tree"
            :data="typeList"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            :props="{children: 'children',label: 'name' }"
            default-expand-all
            highlight-current
            node-key="id"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
          <el-form-item label="标题" prop="tittle">
            <el-input
              v-model="queryParams.tittle"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
<!--          <el-form-item label="作者" prop="author">-->
<!--            <el-input-->
<!--              v-model="queryParams.author"-->
<!--              clearable-->
<!--              style="width: 240px"-->
<!--              @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="语言" prop="language">-->
<!--            <el-select-->
<!--              v-model="queryParams.language"-->
<!--              clearable-->
<!--              style="width: 240px"-->
<!--              @keyup.enter.native="handleQuery">-->
<!--              <el-option label="中文" value="zh"></el-option>-->
<!--              <el-option label="英文" value="en"></el-option>-->
<!--              <el-option label="日文" value="jp"></el-option>-->
<!--              <el-option label="韩文" value="ko"></el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="提交时间">-->
<!--            <el-date-picker-->
<!--              v-model="dateRange"-->
<!--              end-placeholder="结束日期"-->
<!--              range-separator="-"-->
<!--              start-placeholder="开始日期"-->
<!--              style="width: 240px"-->
<!--              type="daterange"-->
<!--              value-format="yyyy-MM-dd"-->
<!--            ></el-date-picker>-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-button icon="el-icon-search" size="mini" type="primary" @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
              icon="el-icon-plus"
              plain
              size="mini"
              type="primary"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="tableData">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="文章类型" prop="articleType"></el-table-column>
          <el-table-column label="标题" prop="tittle"></el-table-column>
          <el-table-column label="语言" prop="language"></el-table-column>
          <el-table-column label="提交时间" prop="submitTime"></el-table-column>
          <el-table-column label="摘要" prop="excerpt" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row.id)">修改</el-button>
              <el-button type="text" @click="handleRemove(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :limit.sync="queryParams.pageSize"
          :page.sync="queryParams.pageNum"
          :total="total"
          @pagination="getList"/>
      </el-col>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" append-to-body width="1200px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="标题" prop="tittle">
              <el-input v-model="form.tittle"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章类型" prop="articleTypeId">
              <treeselect v-model="form.articleTypeId"
                          :normalizer="normalizer"
                          :options="typeOptions"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="语言" prop="language">
              <el-select v-model="form.language" style="width: 100%">
                <el-option label="中文" value="zh"></el-option>
                <el-option label="英文" value="en"></el-option>
                <el-option label="日文" value="jp"></el-option>
                <el-option label="韩文" value="ko"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="活动时间" prop="activityTime">
              <el-date-picker v-model="form.activityTime" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动时间范围" prop="activityTimeRange">
              <el-input v-model="form.activityTimeRange"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动地点" prop="activityLocation">
              <el-input v-model="form.activityLocation"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="显示图片" prop="img">
              <image-upload v-model="form.img" :limit="3"></image-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摘要" prop="excerpt">
              <el-input v-model="form.excerpt" type="textarea" :rows="6"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="文本类型" prop="textType">
              <el-select v-model="form.textType">
                <el-option label="富文本" value="富文本"/>
                <el-option label="文本" value="文本"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="文章内容" prop="content">
              <editor v-model="form.content" v-if="form.textType==='富文本'"/>
              <el-input v-model="form.content" type="textarea" v-if="form.textType==='文本'" :autosize="{minRows:3}" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </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 {add, detail, edit, page, remove} from '@/api/liandao/articleContent.js';
import {list} from '@/api/liandao/articleType.js';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "articleContent",
  components: {Treeselect},
  created() {
    this.getList();
    this.getTypeList()
    this.getTypeOptions()
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tittle: '',
        author: '',
        startTime: '',
        endTime: '',
        articleTypeId: '',
        language: '',
      },
      //表格数据
      tableData: [],
      // 总数
      total: 0,
      // 表单数据
      form: {},
      // 弹出层标题
      title: '',
      open: false,
      rules: {
        tittle: [{required: true, message: "标题不能为空", trigger: "blur"},],
        articleTypeId: [{required: true, message: "文章类型不能为空", trigger: "blur"},],
        content: [{required: true, message: "文章内容不能为空", trigger: "blur"},],
        language: [{required: true, message: "语言不能为空", trigger: "blur"},],
      },
      typeName: '',
      typeList: [],
      dateRange: [],
      typeOptions: [],
    }
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      if(this.dateRange && this.dateRange.length > 0){
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }
      page(this.queryParams).then(resp => {
          if (resp.code == 200) {
            this.tableData = resp.rows;
            this.total = resp.total;
            this.loading = false;
          }
        }
      );
    },
    getTypeList() {
      list({name: this.typeName}).then(resp => {
          if (resp.code == 200) {
            this.typeList = resp.data;
          }
        }
      );
    },
    getTypeOptions() {
      list().then(resp => {
          if (resp.code == 200) {
            this.typeOptions = resp.data;
          }
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = []
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        articleTypeId: undefined,
        tittle: undefined,
        author: undefined,
        img: undefined,
        excerpt: undefined,
        content: undefined,
        activityTime: undefined,
        activityLocation: undefined,
        activityTimeRange:undefined,
        textType:'富文本',
        language: 'zh',
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleEdit(id) {
      this.reset();
      detail(id).then(resp => {
        if (resp.code === 200) {
          this.form = {...resp.data}
          this.open = true;
          this.title = "修改";
        }
      })
    },
    handleRemove(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(id).then(resp => {
          if (resp.code === 200) {
            this.$message.success('删除成功！')
            this.resetQuery()
          }
        })
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            edit(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          } else {
            add(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.articleTypeId = data.id;
      this.handleQuery();
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
  }
}
</script>

