<style lang="scss">
.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;
    }
}
</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"
            ></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="scope.row.fileurl" target="_blank"
              ><span>{{ scope.row.filename }}</span></a
            >
          </template>
        </el-table-column>
        <el-table-column prop="cname" label="是否问卷">
          <template slot-scope="scope">
            <a @click="showSurvey(scope.row)" target="_blank"
              ><span>查看问卷</span></a
            >
            <a
              v-if="scope.row.template_qrcode"
              :href="scope.row.template_qrcode"
              target="_blank"
              ><span> | 生成二维码</span></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.length }}</span></a
              >人<br />
            </span>
            已填报:<a
              :href="scope.row.fileurl"
              target="_blank"
              @click="analyseData('finish',scope.row.id)"
              ><span>{{ scope.row.finish_ids.length }}</span></a
            >人<br />
            <span v-if="scope.row.receiver_ids">
              未填报:<a
                :href="scope.row.fileurl"
                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="问卷统计">
          <template slot-scope="scope">
            <a @click="showMessageAnalyse">查看</a>
          </template>
        </el-table-column>
        <el-table-column prop="cname" label="发送者"> </el-table-column>
        <!-- <el-table-column prop="receiver_names" label="接收者"> -->
        </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
      :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="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="接收人">
          <!-- <el-select placeholder="请选择接收人" v-model="form1.receiver_id" multiple>
                        <el-option
                            v-for="(item, index) in userList"
                            :key="index"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select> -->
          <el-cascader
            v-model="form1.receiver_id"
            :options="options"
            :props="props"
            @change="handleChange"
            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="问卷模板">
          <el-select
            placeholder="请选择问卷模板"
            v-model="form1.template_id"
            clearable
          >
            <el-option
              v-for="(item, index) in templateList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件">
          <input @change="upload('fileurl')" id="fileurl" type="file" />
          <a :href="form1.fileurl" target="_blank"
            ><span>{{ form1.filename }}</span></a
          >
        </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="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible1"
    >
      <div class="item-main">
        <img src="../../assets/survey_logo.png" alt="" class="survey_logo" />
        <h5 align="center" class="tpl_title">{{ title }}</h5>
        <el-form ref="form" :model="form2" class="tpl_form over_y">
          <el-form-item
            v-for="(item, index) in widgetList"
            :key="index"
            :label="item.label"
          >
            <el-input
              v-if="item.type == 'input'"
              v-model="form2[item.label]"
              :placeholder="item.placeholder"
            ></el-input>
            <el-input
              v-if="item.type == 'textarea'"
              type="textarea"
              v-model="form2[item.label]"
              :placeholder="item.placeholder"
            ></el-input>
            <el-radio-group
              v-if="item.type == 'radio'"
              v-model="form2[item.label]"
            >
              <el-radio
                :label="iitem.label"
                v-for="(iitem, index) in item.items"
                :key="index"
                >{{ iitem.label }}</el-radio
              >
            </el-radio-group>

            <el-checkbox-group
              v-else-if="item.type == 'checkbox'"
              v-model="form2[item.label]"
            >
              <el-checkbox
                :label="iitem.label"
                v-for="(iitem, index) in item.items"
                :key="index"
              ></el-checkbox>
            </el-checkbox-group>
            <el-upload
              v-if="item.type == 'image'"
              action="/api/admin/uploadfile"
              list-type="picture-card"
              :data="{ type: item.label }"
              :on-success="handleSuccess"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-upload
              v-if="item.type == 'file'"
              class="upload-demo"
              ref="upload"
              action="/api/admin/uploadfile"
              :data="{ type: item.label }"
              :on-success="handleSuccess"
            >
              <!-- <el-button slot="trigger" size="small" type="primary">选取文件</el-button> -->
              <el-button
                style="margin-left: 10px"
                size="small"
                type="primary"
                plain
                >添加文件</el-button
              >
              <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
            </el-upload>
          </el-form-item>
          <el-form-item v-if="this.widgetList.length">
            <el-button
              size="medium"
              type="primary"
              @click="saveResult"
              style="width: 100%"
              >提交</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </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="form.name"
              ></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 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: [],
      cur_message_id:null,
      cur_post_type:null,
      postResult:{},
      analyseDataList:[],
      title: "",
      widgetList: [],
      form: { name: "", page: 1, page_size: 20 },
      form1: {},
      form2: {
      },
      total: 1,
      list: [{ name: "2333" }],
      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" }],
      },
      props: { multiple: true },
      options: [
        {
          value: 1,
          label: "东南",
          children: [
            {
              value: 2,
              label: "上海",
              children: [
                { value: 3, label: "普陀" },
                { value: 4, label: "黄埔" },
                { value: 5, label: "徐汇" },
              ],
            },
            {
              value: 7,
              label: "江苏",
              children: [
                { value: 8, label: "南京" },
                { value: 9, label: "苏州" },
                { value: 10, label: "无锡" },
              ],
            },
            {
              value: 12,
              label: "浙江",
              children: [
                { value: 13, label: "杭州" },
                { value: 14, label: "宁波" },
                { value: 15, label: "嘉兴" },
              ],
            },
          ],
        },
        {
          value: 17,
          label: "西北",
          children: [
            {
              value: 18,
              label: "陕西",
              children: [
                { value: 19, label: "西安" },
                { value: 20, label: "延安" },
              ],
            },
            {
              value: 21,
              label: "新疆维吾尔族自治区",
              children: [
                { value: 22, label: "乌鲁木齐" },
                { value: 23, label: "克拉玛依" },
              ],
            },
          ],
        },
      ],
    };
  },
  computed() {},
  methods: {
    showMessageAnalyse(){
          this.$router.push({path:'/message/message/analyse'})
    },
    downloadExcel(){
      this.$api.downloadSurveyResult({message_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.dialogTitle2 = "统计结果"
      this.dialogVisible2 = true;
      this.$api.getMessageSurveyAnalyse({type:type,message_id:message_id}).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();
    },
    openDiag() {
      this.form1 = {};
      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.getAllTemplateList().then((res) => {
        this.templateList = res.data.data;
      });
      this.$api.getAllSysUserList().then((res) => {
        this.userList = res.data.data;
      });
      this.$api.getMessageList(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.delMessageInfo({ id: id }).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        });
      });
    },
    edit(row) {
      this.form1 = row;
      let receiver_ids = this.form1.receiver_id;
      console.log(receiver_ids, 33333333333333);
      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.receiver_id = JSON.stringify(parm.receiver_id);
          if (id) {
            this.$api.editMessage(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.addMessage(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>
