<template>
  <el-dialog
    :title="$t('ManualEntryPage.tit_insert_data')"
    :visible.sync="show"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    center
    v-dialogDrag
    @close="cancelHandle"
    custom-class="dialog-cont"
    width="40%"
  >
    <div slot="title" style="display: flex; alignitems: center">
      <img
        :src="$icons.imp"
        style="color: #fff; width: 16px; height: 16px; margin-right: 5px"
      />
      <span style="font-size: 16px">{{$t('ManualEntryPage.tit_insert_data')}}</span>
    </div>
    <div class="down-min">
      <dataLoading :loading="isLoading"/>

      <div class="section-item">
        <div class="templete-wrap">
          <div class="item" v-for="tem in templateList" :key="tem.name">
            <a :href="tem.url" download="模板">
              <el-button type="primary" icon="el-icon-download">下载导入模板</el-button>
            </a>
            <div>
              <img :src="temImg" alt="" width="150" style="display:block;margin:10px 0;">
              <span class="editsty" @click="handlePreviewImg"><!-- 查看大图 -->{{$t('ManualEntryPage.view_big_img')}}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="section-item">
        <h3>上传导入模板</h3>
        <el-upload
          class="upload-demo"
          :before-upload="beforeUploadFile"
          multiple
          :limit="3"
          :headers="headerConfig"
          :on-success="handleSuccess"
          :on-progress="handleStarting"
          :action="action"
          accept=".xlsx"
          :name="name"
          :show-file-list="false"
        >
          <el-button type="primary" icon="el-icon-upload">{{$t('ManualEntryPage.tit_insert_data')}}</el-button>
        </el-upload>
      </div>
    </div>
    <div class="down-tip-txt">
      <span>{{$t('ManualEntryPage.step_one')}}</span>
      <span>{{$t('ManualEntryPage.step_two')}}</span>
      <span>{{$t('ManualEntryPage.step_three')}}
        <a
          style="display: inline; color: #0052D9"
          :href="downloadErrorlist"
          download
          >{{$t('ManualEntryPage.down_fail_list')}}</a
        >。</span
      >
    </div>

    <el-image-viewer 
        v-if="showImgViewer" 
        :on-close="()=>{this.previewList=[];this.showImgViewer = false}" 
        :url-list="previewList" 
    />
  </el-dialog>
</template>

<script>
import { dataInterence } from "api/api.js";
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  name: "",
  components: { ElImageViewer },
  props: {
    show: {
      type: Boolean,
    },
  },
  computed: {
    temImg() {
      const imgSourceMap = {
        '/knowledge_event': require('@/assets/img/knowledge/tem_1.png'),
        '/knowledge_policy': require('@/assets/img/knowledge/tem_2.png'),
        '/knowledge_viewpoint': require('@/assets/img/knowledge/tem_3.png'),
        '/knowledge_know': require('@/assets/img/knowledge/tem_4.png'),
      }

      return imgSourceMap[this.$route.path] && imgSourceMap[this.$route.path]
    }
  },
  data() {
    return {
      action: process.env.VUE_APP_API_ROOT + "/entry/import/data", //上传文件
      downloadErrorlist:
        process.env.VUE_APP_API_ROOT +
        "/entry/import/failList" +
        `?${localStorage.getItem("auth")}`, //失败列表下载
      name: "EntryFile",

      templateList: [
        {
          name:/* "模板1"  */this.$t('ManualEntryPage.tem_msg',{index:1}),
          url: process.env.VUE_APP_API_ROOT + "/entry/template?Source=1"
        }
      ],
      headerConfig: {
        Authorization: localStorage.getItem("auth"),
      },
      isLoading: false,

      showImgViewer: false,
      previewList: []
    };
  },
  methods: {
    cancelHandle() {
      this.$emit('update:show',false)
    },
    // 校验文件和大小
    beforeUploadFile(file) {
      let extension = file.name.substring(file.name.lastIndexOf(".") + 1);
      let size = file.size / 1024 / 1024; //M
      if (extension !== "xlsx" && extension !== "xls") {
        this.$message.warning("只能上传后缀是.xlsx和.xls的文件");
        return false;
      }
      if (size > 10) {
        this.$message.warning(/* "文件大小不得超过10M" */this.$t('ManualEntryPage.upload_size_msg'));
        return false;
      }
    },
    handleStarting() {
      this.isLoading = true;
    },
    // 上传成功之后
    handleSuccess(result) {
      //兼容下结构
      let res = this.$parseData({
        headers: {
          dk: sessionStorage.getItem('dk')||""
        },
        data: result
      });
      if (res.Ret === 200) {
        // 0成功 1部分失败 -1全部失败
        let str = `
						<h2 style="margin-bottom:30px">导入成功</h2>
						<p> 本次共成功导入${res.Data.SuccessCount}条数据</p>
					`;
        if (res.Data.Status === 1) {
          // this.$message.warning('部分导入失败')
          str = `
							<h2 style="margin-bottom:30px">部分导入失败</h2>
							<p>${this.$t('ManualEntryPage.upload_tip_msg2',{success_count:res.Data.SuccessCount,fail_count:res.Data.FailCount})}</p>
							<a style="display:inline;color:#5882EF;" href="${this.downloadErrorlist}" download>${this.$t('ManualEntryPage.down_fail_list2')}</a>
						`;
        } else if (res.Data.Status === -1) {
          // this.$message.success("导入成功!");
          str = `
							<h2 style="margin-bottom:30px">导入失败！</h2>
							<a style="display:inline;color:#5882EF;" href="${this.downloadErrorlist}" download>${this.$t('ManualEntryPage.down_fail_list2')}</a>
						`;
        }
        this.$alert(str, "", {
          dangerouslyUseHTMLString: true,
          center: true,
          showConfirmButton: false,
        });
        this.$emit("importSuccess");
      } else {
        this.$message.warning(res.Msg);
      }
      this.isLoading = false;
    },

    handlePreviewImg() {
      this.previewList=[this.temImg]
      this.showImgViewer=true 
    }
  },
};
</script>
<style lang="scss" scoped>
.dialog-cont {
  max-width: 800px;
  .down-min {
    position: relative;
    margin: 20px 0 50px;
    .arrow-ico {
      width: 140px;
      height: 14px;
      display: block;
      background: url("~@/assets/img/data_m/arrow_ico.png") no-repeat center;
      background-size: 100%;
      margin: 0 50px;
    }
    .icon {
      width: 100px;
      height: 100px;
      display: block;
      margin-bottom: 36px;
      &.down_ico {
        background: url("~@/assets/img/data_m/down_ico.png") no-repeat center;
        background-size: cover;
      }
      &.up_ico {
        background: url("~@/assets/img/data_m/up_ico.png") no-repeat center;
        background-size: cover;
      }
    }
  }
  .down-tip-txt {
    font-size: 14px;
    color: #1f2e4d;
    line-height: 25px;
    padding-bottom: 10px;
    span {
      display: block;
      margin-bottom: 18px;
    }
  }

  .section-item {
    padding: 20px 0;
    &:first-child {
      border-bottom: 1px solid #C8CDD9;
    }
    h3 {
      font-size: 16px;
      margin-bottom: 20px;
    }
    .templete-wrap {
      display: flex;
      gap: 20px;
    }
  }
}
</style>
