<template>
  <a-modal
    centered
    id="costSetting-importGuide-modal"
    class="importGuide-modal"
    :footer="null"
    :maskClosable="false"
    title="导入"
    v-model="isShow"
    @cancel="isShow=false"
    :width="750">
    <div class="importGuide-con">
      <div class="explain-con">
        <div class="explain-item">
          <div class="explain-tit">
            <span>1</span>准备导入
          </div>
          <ul>
            <li>1) 导入的Excel文件中必须包含名为“供应商名称“、“产品编码”、“成本”的列，且名称必须与系统相同</li>
            <li>2) 除了“供应商名称“、“产品编码”、“成本”三列，其他列可自定义，不影响数据导入</li>
            <li>3) 如果导入的供应商名称、产品已经存在，未创建之间的关联，则允许导入，同时系统创建关联；若已建立之间的关联，则允许导入，成本按照更新处理，若不存在，则需先完善供应商和产品</li>
          </ul>
          <a-button
            id="importGuide-tpl"
            type="link"
            icon="download"
            style="padding: 0 0 0 23px;"
            :loading="exportLoading"
            @click="handleExport">下载导入模板</a-button>
        </div>
        <div class="explain-item">
          <div class="explain-tit">
            <span>2</span>上传数据文件
          </div>
          <p>目前支持的文件类型*.xls，*.xlsx.</p>
          <div style="overflow: hidden;padding-left: 23px;">
            <Upload
              id="importGuide-attachList"
              ref="importUpload"
              :maxNums="1"
              fileType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
              uploadType="import"
              :action="attachAction"
              :uploadParams="uploadParams"
              upText="添加文件"
              @change="changeImport"></Upload>
          </div>
        </div>
      </div>
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          type="primary"
          id="importGuide-nextStep"
          size="large"
          class="button-primary"
          @click="handlerNextStep"
          style="padding: 0 60px;">下一步</a-button>
        <a-button
          id="importGuide-cancel"
          size="large"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 60px;margin-left: 15px;">取消</a-button>
      </div>
    </div>
    <!-- 导入 -->
    <chooseImportModal :openModal="openImportModal" :paramsData="paramsData" @close="handleClose" @ok="hanldeOk" />
  </a-modal>
</template>

<script>
// 组件
import { Upload } from '@/components'
import ChooseImportModal from './chooseImportModal.vue'
// 接口
import { supplierProductNewDownloadExcel } from '@/api/supplier'
export default {
  name: 'ImportGuideModal',
  components: { ChooseImportModal, Upload },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    params: { // 参数
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      openImportModal: false, //  导入
      attachAction: process.env.VUE_APP_API_BASE_URL + '/upload', // 上传接口
      paramsData: null, // 参数
      uploadParams: {
        savePathType: 'local' // 文件上传位置
      },
      exportLoading: false // 导出错误项loading
    }
  },
  methods: {
    // 下一步
    handlerNextStep () {
      if (this.paramsData) {
        this.openImportModal = true
      } else {
        this.$message.warning('添加文件后再进行下一步操作！')
      }
    },
    // 上传文件  change
    changeImport (file) {
      if (file) {
        this.paramsData = {
          path: file
        }
      }
    },
    // 导入
    hanldeOk (obj) {
      if (obj && obj.length > 0) {
        this.$emit('ok', obj)
        this.isShow = false
      }
    },
    // 关闭
    handleClose () {
      this.openImportModal = false
      this.isShow = false
    },
    //  导出
    handleExport () {
      const _this = this
      _this.spinning = true
      _this.exportLoading = true
      supplierProductNewDownloadExcel({}).then(res => {
        _this.spinning = false
        _this.exportLoading = false
        if (res.type == 'application/json') {
          var reader = new FileReader()
          reader.addEventListener('loadend', function () {
            const obj = JSON.parse(reader.result)
            _this.$notification.error({
              message: '提示',
              description: obj.message
            })
          })
          reader.readAsText(res)
        } else {
          this.download(res)
        }
      })
    },
    // 下载模板文件
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const fname = '成本设置导入模板'
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.paramsData = null
        this.$refs.importUpload.setFileList('')
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .importGuide-modal{
    .importGuide-con{
      .explain-con{
        .explain-item{
          margin-bottom: 10px;
          .explain-tit{
            font-weight: bold;
            span{
              display: inline-block;
              width: 18px;
              height: 18px;
              line-height: 16px;
              text-align: center;
              margin-right: 5px;
              border-radius: 50%;
              border: 1px solid rgba(0, 0, 0, 0.3);
            }
          }
          p{
            margin: 8px 0;
            padding-left: 23px;
          }
          ul{
            list-style: none;
            padding: 0;
            padding-left: 23px;
            margin: 0;
            li{
              line-height: 26px;
            }
          }
        }
        #importGuide-attachList{
          width: 100%;
        }
      }
      .btn-con{
        margin: 10px 0 20px;
        text-align: center;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
