<style lang="scss" scoped>
.preview {
  .el-form-item {
    margin-bottom: 15px;
  }
  label,
  p {
    line-height: 25px !important;
  }
}
.el-cascader {
  width: 100%;
}
a {
  text-decoration: none;
  color: #409eff;
  cursor: pointer;
}
// 右边
.item-main {
  // margin:34px;
  // height:600px;
  // background: #FFFFFF;
  border: 1px solid #dddddd;
  box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.13);
  border-radius: 20px;
  .el-form.over_y {
    max-height: calc(100vh - 340px);
  }
  /deep/.el-form-item__label {
    float: none;
  }
}
// label样式
.edit_label {
  /deep/.el-input__inner {
    background: #f5faff;
    border: 0px;
    padding: 0px;
  }
}
/deep/.opicon {
  font-weight: bold;
  padding: 5px;
  color: #3895fe;
}
.tpl_title {
  font-size: 18px;
  margin: 20px 0;
}
.tpl_form {
  margin: 20px;
  border: 1px solid #ccc;
  border-radius: 20px;
  /deep/.el-form-item {
    background: none;
  }
  /deep/.el-form-item__content {
    width: 100%;
  }
}
.survey_logo {
  position: relative;
  top: 15px;
  left: 20px;
}
.result_dialog .el-dialog {
  width: 40%;
  margin-top: 20vh !important;
}
.result-item {
  height: 35px;
  .label {
    font-weight: 600;
  }
}
/deep/.w-e-text {
  min-height: 500px;
}
.w-e-text-container{
  height: 500px;
}
.fu-dialog {
  .el-dialog {
    width: 60%;
  }
}
</style>
<template>
  <section>
    <p><span>信息管理></span>消息管理</p>
    <div class="content">
      <div class="filter">
        <el-form
          size="small"
          label-width="70px"
          :inline="true"
          label-position="left"
        >
          <el-form-item label="">
            <el-input
              clearable
              placeholder="请输入标题"
              v-model="form.name"
              @clear="getData"
              @keyup.enter.native="search"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <el-button @click="openDiag" type="primary">发送消息</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        class="table"
        :data="list"
        height="50vh"
        border
        v-loading="loading"
        default-expand-all
        row-key="id"
        style="width: 100%"
      >
        <el-table-column prop="name" label="标题"> </el-table-column>
        <el-table-column prop="name" label="附件">
          <template slot-scope="scope">
            <a :href="item.url" target="_blank" :download="item.url"
              v-for="(item,index) in scope.row.fileurl" :key="index"
              ><span>{{ item.name }}</span><br></a>
          </template>
        </el-table-column>
        <el-table-column prop="cname" label="消息查阅统计">
          <template slot-scope="scope">
            <span v-if="scope.row.receiver_ids"
              >共统计:
              <a target="_blank" @click="analyseData('total',scope.row.id)"
                ><span>{{ scope.row.receiver_ids?scope.row.receiver_ids.length:0 }}</span></a
              >人<br />
            </span>
            已阅读:<a
              target="_blank"
              @click="analyseData('finish',scope.row.id)"
              ><span>{{ scope.row.finish_ids?scope.row.finish_ids.length:0 }}</span></a
            >人<br />
            <span v-if="scope.row.receiver_ids">
              未阅读:<a
                target="_blank"
                @click="analyseData('unfinish',scope.row.id)"
                ><span>{{ scope.row.unfinish_ids.length }}</span></a
              >人<br
            /></span>
          </template>
        </el-table-column>
        <el-table-column prop="cname" label="发送者"> </el-table-column></el-table-column>
        <el-table-column prop="ctime" label="发送时间"> </el-table-column>
        <el-table-column prop="zip" width="150" label="操作">
          <template slot-scope="scope">
            <el-button class="edit" type="text" @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button class="del" @click="del(scope.row.id)" type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Page
        ref="pageButton"
        :current="form.page"
        :page_size="form.page_size"
        :total="total"
        @pageChange="gopage"
      />
    </div>

    <el-dialog
      class='fu-dialog'
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
    >
      <el-form
        size="small"
        class="preview"
        :inline="false"
        label-width="80px"
        :model="form1"
        ref="form1"
        :rules="rules"
      >
        <el-form-item label="标题" prop="name">
          <el-input v-model="form1.name" placeholder="请输入标题" maxlength="50" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="接收人" prop="receiver_id">
          <el-cascader
            v-model="form1.receiver_id"
            :options="options"
            :props="props"
            @change="handleChange"
            filterable
            clearable
          >
            <template slot-scope="{ node, data }">
              <span>{{ data.label }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <fuEditor v-model="form1.content" :isClear="false" @change="changeEditor"></fuEditor>
        </el-form-item>
        <el-form-item label="附件">
          <!-- <input @change="upload('fileurl')" id="fileurl" type="file" /> -->
          <el-upload
            class="upload-demo"
            action="/api/admin/uploadfile"
            multiple
            :limit="5"
            :on-remove="handleRemove"
            :on-success="handleUploadSuccess"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
          </el-upload>
          <span>
            <a :href="form1.fileurl" target="_blank"
            ><span>{{ form1.filename }}</span></a
          >
          <i class="el-icon-circle-close" style='margin-left:10px;color: #409eff;' @click='clearFilename' v-show='form1.filename'></i>
          </span>
          
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 数据统计分析 -->
    <el-dialog
      :title="dialogTitle2"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible2"
    >
      <div class="content">
        <div class="filter">
          <el-form
            size="small"
            label-width="70px"
            :inline="true"
            label-position="left"
          >
            <el-form-item label="">
              <el-input
                clearable
                placeholder="请输入标题"
                v-model="form1.name"
                @clear="search1"
                @keyup.enter.native="search1"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search1">搜索</el-button>
            </el-form-item>
            <el-form-item style="float: right">
              <el-button type="primary" @click="downloadExcel">导出Excel</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table
          class="table"
          :data="analyseDataList"
          height="50vh"
          border
          v-loading="loading"
          default-expand-all
          row-key="id"
          style="width: 100%"
        >
          <el-table-column prop="realname" label="姓名"> </el-table-column>
          <el-table-column prop="journal_name" label="所属期刊"></el-table-column>
          <el-table-column prop="cname" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.post_status">已阅读</span>
              <span v-else>未阅读</span>
            </template>
          </el-table-column>
          <el-table-column prop="post_time" label="查阅时间"> </el-table-column>
          <!-- <el-table-column prop="zip" width="80" label="操作">
            <template slot-scope="scope">
              <el-button class="edit" type="text" @click="showPostResult(scope.row)"
                >查看</el-button
              >
            </template>
          </el-table-column> -->
        </el-table>
        <Page
          ref="pageButton"
          :current="form.page"
          :page_size="form.page_size"
          :total="total"
          @pageChange="gopage"
        />
      </div>
    </el-dialog>
    <!-- 展示填报结果 -->
    <el-dialog
    :title="dialogTitle3"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible3"
    class="result_dialog"
    >
    <div class="content">
        <div class="result-item" v-for="(item,key) in this.postResult" :key="key">
            <span class="label">{{key}}：</span><span class="value">{{item}}</span>
        </div>
        <div v-if="Object.keys(postResult).length==0">暂无数据</div>
    </div>
    </el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
import fuEditor from "../../components/fuEditor";
export default {
  components: {
    Page,
    fuEditor,
  },
  data() {
    return {
      //   checked: [],
      fileList:[],
      cur_message_id: null,
      cur_post_type: null,
      postResult: {},
      analyseDataList: [],
      title: "",
      widgetList: [],
      form: { name: "", page: 1, page_size: 20 },
      form1: {},
      form2: {},
      total: 1,
      list: [],
      loading: false,
      input: "",
      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      dialogVisible3: false,
      dialogTitle: "",
      dialogTitle2: "",
      dialogTitle3: "",
      defaultProps: {},
      data: [],
      templateList: [],
      userList: [],
      checkList: [],
      rules: {
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
        receiver_id: [
          { required: true, message: "请选择接收人", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入消息内容", trigger: "change" },
        ],
      },
      props: { multiple: true },
      options: [],
    };
  },
  methods: {
    handleRemove(file,filelist){
      let i = this.fileList.indexOf(file)
      this.fileList.splice(i,1)
    },
    handleUploadSuccess(res,file,filelist){
        this.fileList.push(res.data)
    },
    clearFilename() {
      this.form1.filename = "";
      document.getElementById("fileurl").value = "";
    },
    changeEditor(val) {
      this.form1.content = val;
    },
    showMessageAnalyse() {
      this.$router.push({ path: "/message/message/analyse" });
    },
    downloadExcel() {
      this.$api
        .downloadNoticeAnalyse({
          id: this.cur_message_id,
          type: this.cur_post_type,
        })
        .then((res) => {
          var elink = document.createElement("a");
          let blob = new Blob([res.data], {
            type: "application/vnd.ms-excel,charset=UTF-8",
          });
          let objUrl = URL.createObjectURL(blob);
          let file_name = decodeURIComponent(
            res.headers["content-disposition"].split("=")[1]
          );
          console.log(file_name);
          elink.download = file_name;
          elink.style.display = "none";
          elink.href = objUrl;
          document.body.appendChild(elink);
          elink.click();
          document.body.removeChild(elink);
          this.download_loading = false;
        });
    },
    showPostResult(row) {
      this.postResult = row.post_result;
      this.dialogTitle3 = "填报结果";
      this.dialogVisible3 = true;
    },
    analyseData(type, message_id) {
      this.form1 = {};
      let parm = this.form1;
      parm.type = type;
      parm.id = message_id;
      this.dialogTitle2 = "统计结果";
      this.dialogVisible2 = true;
      this.$api.getNoticeAnalyse(parm).then((res) => {
        this.analyseDataList = res.data.data.list;
        this.cur_message_id = message_id;
        this.cur_post_type = type;
      });
    },
    handleSuccess(res, file) {
      this.form2[res.data.type] = res.data.url;
    },
    change(i, label) {
      this.form2[label] = i;
    },
    changeMul(i, label) {
      this.form2[label] = i;
    },
    showSurvey(row) {
      let _this = this;
      this.title = row.name;
      this.message_id = row.id;
      this.$api.getMessageInfo({ id: row.id }).then((res) => {
        if (res.data.code == 0) {
          this.widgetList = res.data.data.widget;
          this.widgetList.forEach((i) => {
            if (i.type == "checkbox") {
              //   _this.form2[i.label] = [] ;
              _this.$set(_this.form2, i.label, []);
            }
          });
          console.log(this.form2);
          //   debugger
          this.dialogVisible1 = true;
        }
      });
    },
    upload(type) {
      var file = document.getElementById(type).files;
      var data = new FormData();
      data.append("file", file[0]);
      this.$api.uploadFile(data).then((res) => {
        if (res.data.code == 0) {
          this.$set(this.form1, type, res.data.data.url);
          this.$set(this.form1, "filename", res.data.data.name);
          this.$message({ message: "上传成功！", type: "success" });
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    handleChange(value) {
      console.log(value);
    },
    search() {
      let parm = this.form;
      this.getData();
    },
    search1() {
      let parm = this.form1;
      let type = this.cur_post_type;
      let message_id = this.cur_message_id;
      parm.type = type;
      parm.id = message_id;
      this.dialogTitle2 = "统计结果";
      this.dialogVisible2 = true;
      this.$api.getNoticeAnalyse(parm).then((res) => {
        this.analyseDataList = res.data.data.list;
        this.cur_message_id = message_id;
        this.cur_post_type = type;
      });
    },
    openDiag() {
      this.form1 = {};
      this.fileList = []
      this.dialogVisible = true;
      this.dialogTitle = "发送消息";
    },
    gopage(size) {
      if (size) {
        this.form.page_size = size;
      }
      this.form.page = this.$refs.pageButton.page;
      this.getData();
    },
    getData() {
      var parm = this.form;
      this.loading = true;
      this.$api.getAllSysUserList().then((res) => {
        this.userList = res.data.data;
      });
      this.$api.getNoticeList(parm).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
      this.$api.getMessageReceiverList().then((res) => {
        this.options = res.data.data;
      });
    },
    del(id) {
      this.$confirm("确定删除吗", "提示", {
        type: "warning",
      }).then(() => {
        this.$api.delNotice({ id: id }).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        });
      });
    },
    edit(row) {
      this.$api.getNoticeInfo({ id: row.id,type:"edit" }).then((res) => {
        this.form1 = res.data.data;
        this.fileList = res.data.data.fileurl
        this.dialogTitle = "编辑消息";
        let receiver_ids = this.form1.receiver_id;
        if (typeof receiver_ids == "string") {
          receiver_ids = JSON.parse(receiver_ids);
        }
        // receiver_ids.forEach((element,index) => {
        //     receiver_ids[index] = parseInt(element)
        // });
        this.form1.receiver_id = receiver_ids;
        this.dialogVisible = true;
      });
    },
    saveResult() {
      let result = JSON.stringify(this.form2);
      this.$api
        .saveSurveyResult({
          message_id: this.message_id,
          result: result,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.$message({
              type: "success",
              message: "保存成功!",
            });
          } else {
            this.$message.error("保存失败!");
          }
          this.dialogVisible1 = false;
        });
    },
    save() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          let parm = this.form1;
          let id = this.form1.id;
          parm.fileurl = JSON.stringify(this.fileList)
          parm.receiver_id = JSON.stringify(parm.receiver_id);
          if (id) {
            this.$api.editNotice(parm).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  type: "success",
                  message: "保存成功!",
                });
              } else {
                this.$message.error("保存失败!");
              }
              this.getData();
              this.dialogVisible = false;
            });
          } else {
            this.$api.addNotice(parm).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  type: "success",
                  message: "保存成功!",
                });
              } else {
                this.$message.error("保存失败!");
              }
              this.getData();
              this.dialogVisible = false;
            });
          }
        }
      });
    },
  },
  created() {
    this.getData();
  },
};
</script>
