<template>
  <a-modal
    centered
    class="importGuide-modal"
    :footer="null"
    :maskClosable="false"
    title="导入产品"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <div class="importGuide-con">
      <div class="explain-con">
        <div class="explain-item">
          <div class="explain-tit">
            <span>1</span>准备导入
          </div>
          <ul>
            <li v-if="goodFlag==='1'">1) 导入的Excel文件中必须包含名为“产品编码”的列，“产品编码”必填</li>
            <li v-if="goodFlag==='0'&&rulesType!='0'">1) 导入的Excel文件中必须包含名为“产品编码”的列，“产品编码”必填</li>
            <li v-if="goodFlag==='0'&&rulesType=='0'">1) 导入的Excel文件中必须包含名为“产品编码”、“省级特价”、“市级特价”和“特约特价”的列，都为必填，省级特价≤市级特价≤特约特价</li>
            <li>2) 如果导入的产品已经存在，则不会导入该行</li>
            <li>3)起订类型和起订数量非必填：起订类型为空则默认为“按数量设置”，起订数量为空则默认为“1”。</li>
            <li>
              <a :href="filePath" style="margin: 5px 0 0;display: block;" id="importGuide-download">
                <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"
              :uploadParams="uploadData"
              :action="goodFlag=='1'?attachAction:attachAction1"
              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 ref="chooseImportBox" :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: { ChooseImportModal, Upload },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default: null
    },
    goodFlag: {// 0  特价  1 买产品送产品  买产品送采购额
      type: [String, Number],
      default: '0'
    },
    rulesType: {// 0 手动 1折扣 2直降
      type: [String, Number],
      default: '0'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      openImportModal: false, //  导入
      attachAction: process.env.VUE_APP_API_BASE_URL + '/promotion/rule/importNormalProduct', // 导入门槛、正价、促销产品
      attachAction1: process.env.VUE_APP_API_BASE_URL + '/promotion/rule/importSpecialProduct', // 导入特价产品
      paramsData: null, // 上传参数
      filePath: '', // 上传文件路径
      uploadData: null// 上传数据
    }
  },
  methods: {
    // 清空数据
    resetSearchForm () {
      this.$refs.importUpload.setFileList() //  清空导入文件
      this.paramsData = null //  清空上传数据
      this.uploadData = null
    },
    // 下一步
    handlerNextStep () {
      if (this.paramsData) {
        this.openImportModal = true
        this.$refs.chooseImportBox.setFlag({ goodFlag: this.goodFlag, discountType: this.rulesType })
      } 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 (obj) {
      if (obj && obj.length > 0) {
        this.$emit('ok', obj)
        this.openImportModal = 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()
      } else {
        if (this.goodFlag != '0') {
          this.filePath = location.protocol + '//' + location.host + '/templ/促销规则导入产品.xlsx'
          this.uploadData = { savePathType: 'ali' }
        } else {
          this.uploadData = { discountType: this.rulesType, savePathType: 'ali' }
          this.filePath = location.protocol + '//' + location.host + (this.rulesType == '0' ? '/templ/促销规则导入特价产品.xlsx' : this.rulesType == '1' ? '/templ/促销规则导入特价产品（打折）.xlsx' : '/templ/促销规则导入特价产品（直降）.xlsx')
        }
      }
    }
  }
}
</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>
