<template>
  <a-modal
    centered
    class="importGuide-modal"
    :footer="null"
    :maskClosable="false"
    title="散件导入"
    v-model="isShow"
    @cancel="isShow=false"
    width="60%">
    <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>
            <li>4) 支持上传文件格式：.xls、.xlsx。</li>
            <li>
              <a :href="filePath" style="margin: 5px 0 0;display: block;">
                <a-icon type="download" style="padding-right: 5px;" />下载导入模板
              </a>
            </li>
          </ul>
        </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="attach"
              :action="attachAction"
              :uploadParams="uploadParams"
              upText="选择导入文件"
              @remove="resetSearchForm"
              @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 ChooseImportModal from './chooseImportModal.vue'
import { Upload } from '@/components'
export default {
  name: 'ImportGuideModal',
  components: { Upload, ChooseImportModal },
  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 + '/stockImport/importForSpareParts',
      paramsData: null,
      uploadParams: {
        savePathType: 'local'
      },
      filePath: location.protocol + '//' + location.host + '/templ/散件产品导入模板.xlsx'
    }
  },
  methods: {
    // 清空数据
    resetSearchForm () {
      this.$refs.importUpload.setFileList() //  清空导入文件
      this.paramsData = null //  清空上传数据
    },
    // 下一步
    handlerNextStep () {
      if (this.paramsData) {
        this.openImportModal = true
      } else {
        this.$message.warning('添加文件后再进行下一步操作！')
      }
    },
    // 上传文件  change
    changeImport (file) {
      if (file && JSON.parse(file).length > 0) {
        this.paramsData = Object.assign({}, JSON.parse(file)[0] || null, this.params)
      } else {
        this.paramsData = null
      }
    },
    // 导入
    hanldeOk () {
      this.$emit('ok')
      this.isShow = false
    },
    // 关闭
    handleClose () {
      this.openImportModal = false
      this.isShow = false
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetSearchForm()
      }
    }
  }
}
</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>
