<style lang="scss" scoped>
.preview {
  .el-form-item {
    margin-bottom: 15px;
  }
  label,
  p {
    line-height: 25px !important;
  }
}
/deep/.avatar-uploader .el-upload {
  width: 148px;
  height: 148px;
  line-height: 148px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  .avatar {
    width: 100%;
    height: auto;
  }
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 68px;
  height: 68px;
  line-height: 68px;
  text-align: center;
}
.avatar {
  width: 68px;
  height: 68px;
  display: block;
}
/deep/.speaker_avatar .el-upload {
  width: 80px;
  height: 80px;
  line-height: 80px;
}
</style>
<template>
  <section>
    <p>中科院全刊</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"
            ></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="dialogVisible1=true" plain icon="el-icon-upload" type="primary">导入</el-button>
          </el-form-item>
          <el-form-item style="float:right" >
                <el-button @click="download" plain icon="el-icon-download" type="primary">导出</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <el-button @click="add" type="primary">添加期刊</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        class="table"
        :data="list"
        height="57vh"
        border
        v-loading="loading"
        default-expand-all
        row-key="id"
        style="width: 100%"
      >
        <el-table-column prop="img" label="期刊封面">
          <template slot-scope="scope">
            <img :src="scope.row.img" alt="" height="80" width="60" />
          </template>
        </el-table-column>
        <el-table-column prop="name" width="180" label="期刊名称">
        </el-table-column>
        <el-table-column prop="wxcode" label="微信号"> </el-table-column>
        <el-table-column width="150" prop="organizer_name" label="主办单位">
        </el-table-column>
        <el-table-column prop="desc" width="300" label="简介">
        </el-table-column>
        <el-table-column width="150" prop="name" label="中文名称"> </el-table-column>
        <el-table-column width="150" prop="ename" label="外文名称"> </el-table-column>
        <el-table-column prop="languages" label="语种"> </el-table-column>
        <el-table-column prop="editor" label="主编"> </el-table-column>
        <el-table-column width="150" prop="corganizers" label="主管单位"> </el-table-column>
        <el-table-column width="150" prop="publishingUnit" label="办刊单位">
        </el-table-column>
        <el-table-column width="150" prop="publishingTime" label="创刊时间">
        </el-table-column>
        <el-table-column prop="domestic" label="国内刊号"> </el-table-column>
        <el-table-column prop="issn" label="国际刊号"> </el-table-column>
        <el-table-column width="150" prop="website" label="期刊官网"> </el-table-column>
        <el-table-column width="150" prop="editorialAddress" label="编辑部地址">
        </el-table-column>
        <el-table-column prop="zip" width="150" align="center"  label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" @click="showQrcode(scope.row)"
              >二维码上传</el-button
            >
            <el-button type="text" @click="showJournal(scope.row)"
              >查看</el-button
            >
            <el-button class="edit" type="text" @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button class="del" @click="del" 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"
      />
      <!--二维码上传-->
      <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="100px"
          :model="form1"
          ref="form1"
        >
          <el-form-item label="上传二维码：" prop="img">
            <el-upload
              class="avatar-uploader"
              action="/api/admin/uploadfile"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="form1.qrcode" :src="form1.qrcode" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span class="del" style="position:absolute;top:-8px;left:150px;" @click="delImg">删除</span>
            <span style="font-size: 12px; color: #999999"
              >建议图片尺寸为：210*160</span
            >
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" type="normal" @click="dialogVisible=false,form1={}">取消</el-button>
          <el-button size="small" type="primary" @click="save">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        class="fu-dialog"
        title="导入期刊" width="400px"
        :close-on-click-modal="false"
        :visible.sync="dialogVisible1"
      > 
        <el-upload
          class="upload-demo"
          action="/api/admin/uploadfile"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传.xls/.xlsx文件，且不超过500M</div>
        </el-upload>
        <div>{{file}}</div>
         <span slot="footer" class="dialog-footer">
          <el-button size="small" type="normal" @click="dialogVisible1=false">取消</el-button>
          <el-button size="small" type="primary" @click="upload">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      //   edit:0,
      form: { name: "", page: 1, page_size: 20 },
      total: 1,
      list: [{ name: "2333" }],
      loading: false,
      input: "",
      dialogVisible: false,
      dialogTitle: "",
      dialogVisible1:false,
      form1: {},
      file:''
    };
  },
  methods: {
    delImg(){
      this.$set(this.form1,"qrcode",null)
      this.$forceUpdate()
    },
    save() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          let parm = this.form1;
          let id = this.form1.id;
          this.$api.updateJournal(parm).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  type: "success",
                  message: "保存成功!",
                });
                this.getData();
              } else {
                // this.$message.error("保存失败!");
              }
              this.dialogVisible = false;
            });
        }
      });
    },
    upload(){
      this.$api.upJournalData({file:this.file}).then(res=>{
          this.dialogVisible1=false;
          this.getData();
          this.$message({
            message: "上传成功",
            type: "success",
          });
      })
    },
    download(){
      this.$api.getJournalData().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);
      })
    },
    showQrcode(row){
      this.form1.journal_id = row.journal_id;
      this.form1.id = row.id;
      this.form1.qrcode = row.qrcode;
      this.dialogVisible = true;
    },
    showJournal(row){
      this.$router.push({
        path:"/journal/journal/show",
        query:{id:row.id}
      })
    },
    //封面
    handleAvatarSuccess(res, file) {
      this.$set(this.form1,'qrcode',file.response.data)
    },
    handleAvatarSuccess1(res, file) {
      this.file=file.response.data;
    },
    gopage(size) {
      if (size) {
        this.form.page_size = size;
      }
      this.form.page = this.$refs.pageButton.page;
      this.getData();
    },
    edit(row) {
      this.$router.push({
        path: "/journal/journal/add",
        query: { id: row.id },
      });
    },
    add() {
      this.$router.push({ path: "/journal/journal/add" });
    },
    detail(id) {
      this.$router.push({
        path: "/company/detail",
        query: { id: id, page: this.form.page, page_size: this.form.page_size },
      });
    },
    search() {
      this.getData();
    },
    getData() {
      var parm = this.form;
      this.loading = true;
      this.$api.getJournalList(parm).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    del(id) {
      this.$confirm("确定删除吗", "提示", {
        type: "warning",
      }).then(() => {
        this.$api.deleteEnterprise({ id: id }).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        });
      });
    },
  },
  created() {
    this.getData();
  },
  beforeRouteEnter(to,from,next){
        next(vm=>{
            if(from.path.indexOf('add')>=0){
                vm.getData()
            }
        })
  }
};
</script>
