<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">创建消息</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
      </el-form>
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column
        label="学校名称"
        show-overflow-tooltip
        prop="jjxx_xxmc"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建人"
        show-overflow-tooltip
        prop="jjxx_cjr"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="创建日期"
        width="150"
        prop="jjxx_cjrq"
      ></el-table-column>
      <el-table-column label="消息内容" prop="jjxx_content" width="auto">
        <template slot-scope="scope">
          <div v-html="scope.row.jjxx_content"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row.jjxx_id)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>

    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="750px"
      destory-on-close
    >
      <el-form :model="dialogForm" label-width="80px">
        <el-form-item label="发送学校" prop="xxmc">
          <el-input
            v-model="dialogForm.xxmc"
            placeholder="请输入学校名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="创建日期" prop="cjrq">
          <el-date-picker
            v-model="dialogForm.cjrq"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消息内容" prop="xxnr">
          <Tinymce
            v-if="dialogFormVisible"
            v-model="dialogForm.xxnr"
            :height="300"
            :key="timestamp"
          ></Tinymce>
        </el-form-item>
        <el-form-item label="其他附件" prop="qtfj">
          <FileUpload
            :limit="5"
            :full.sync="dialogForm.qtfj"
            :key="timestamp"
            multiple
          >
            <div slot="tip" class="el-upload__tip">
              支持文件类型:doc(.docx)、.ppt(.pptx)、.xls(.xlsx)、.pps、.wps、.pdf、.txt文件格式。
            </div>
          </FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">发 布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import { jjxx_list, jjxx_add, jjxx_edit, jjxx_del } from "./api";
import { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();

export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      //  type_value: "",
      // type_list: [],
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        xxmc: "",
        cjr: "",
        cjrq: "",
        xxnr: "",
        qtfj: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      timestamp: Date.now(),
    };
  },

  methods: {
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      jjxx_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },

    addData() {
      this.title = "新建消息";
      this.dialogForm = {
        xxmc: "",
        cjr: "",
        cjrq: "",
        xxnr: "",
        qtfj: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑消息";
      this.isEdit = true;
      this.id = item.jjxx_id;
      this.dialogForm.xxmc = item.jjxx_xxmc;
      this.dialogForm.cjr = item.jjxx_cjr;
      this.dialogForm.cjrq = item.jjxx_cjrq;
      this.dialogForm.xxnr = item.jjxx_content;
      this.timestamp = Date.now();
      this.dialogForm.qtfj = item.jjxx_qtfj;

      this.dialogFormVisible = true;
    },
    formSubmit() {
      let data = {
        jjxx_xxmc: this.dialogForm.xxmc,
        jjxx_cjr: real_name,
        jjxx_cjrq: this.dialogForm.cjrq,
        jjxx_content: this.dialogForm.xxnr,
        jjxx_qtfj: this.dialogForm.qtfj,
      };
      if (!this.isEdit) {
        jjxx_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "消息添加成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.jjxx_id = this.id;
        jjxx_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "消息编辑成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        jjxx_id: id,
      };
      this.$confirm("确认删除该消息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          jjxx_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "消息删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style scoped>
</style>
