<template>
  <a-modal
    centered
    class="importGuide-modal"
    :footer="null"
    :maskClosable="false"
    :title="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 v-if="!isTotalFlag">
            <li>1) 导入的Excel文件中必须包含名为“产品编码”、“数量”、“出库仓库”的列，且名称必须相同</li>
            <li>2) 除了“产品编码”、“数量”、“出库仓库”三列，其他列可自定义，不影响数据导入</li>
            <li>3) 如果导入的产品已经存在，则不会导入该行</li>
            <li v-if="uploadParams.salesPromoSn">4) 导入的活动产品必须是活动规则中的产品 </li>
            <li>
              <a :href="filePath" style="margin: 5px 0 0;display: block;">
                <a-icon type="download" style="padding-right: 5px;" />下载导入模板
              </a>
            </li>
          </ul>
          <ul v-else>
            <li>1) 导入的Excel文件中必填项为"销售单号"、"产品编码"，需填写系统上已完结的销售单与该销售单内未参与促销活动、未被累计且符合当前促销规则的产品。</li>
            <li>2) 错误项忽略不能导入，正确项可直接导入。</li>
            <li>
              <a :href="fileOtherPath" 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="!isTotalFlag?attachAction:attachOtherAction"
              :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" />
    <!-- 导入累计产品 -->
    <chooseImportTotalModal :openModal="openTotalImportModal" :paramsData="paramsData" @close="handleClose" @ok="hanldeOk" />
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import ChooseImportModal from './chooseImportModal.vue'
import chooseImportTotalModal from './chooseImportTotalModal.vue'
import { Upload } from '@/components'
export default {
  name: 'SalesImportGuideModal',
  mixins: [commonMixin],
  components: { ChooseImportModal, chooseImportTotalModal, Upload },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      openImportModal: false, //  导入
      openTotalImportModal: false, // 导入累计弹窗
      attachAction: process.env.VUE_APP_API_BASE_URL + '/sales/detail/importParse', // 解析地址
      attachOtherAction: process.env.VUE_APP_API_BASE_URL + '/sales/detail/importBorrow', // 解析地址
      paramsData: null, // 参数数据
      uploadParams: { // 上传参数
        salesBillSn: '',
        salesPromoSn: '',
        promoProductClz: ''
      },
      exportLoading: false,
      // 模板文件地址
      filePath: location.protocol + '//' + location.host + '/templ/销售导入产品模板.xlsx',
      fileOtherPath: location.protocol + '//' + location.host + '/templ/销售产品模板.xlsx',
      curRowData: null,
      isTotalFlag: false, // 是否是累计产品
      title: '导入产品'
    }
  },
  methods: {
    pageInit (params, row, title) {
      console.log(params)
      this.title = title ? ('导入' + title) : '导入产品'
      this.curRowData = row || null
      this.isTotalFlag = params.promoProductClz && (params.promoProductClz === 'BORROW_GATE' || params.promoProductClz === 'BORROW_REGULAR')
      // 判断是否是累计产品导入
      if (this.isTotalFlag) {
        params.promoProductClz = params.promoProductClz.split('_')[1]
      }
      this.uploadParams = { ...this.uploadParams, ...params }
    },
    // 下一步
    handlerNextStep () {
      if (this.paramsData) {
        this[!this.isTotalFlag ? 'openImportModal' : 'openTotalImportModal'] = true
      } else {
        this.$message.warning('添加文件后再进行下一步操作！')
      }
    },
    // 上传文件  change
    changeImport (file) {
      // console.log(file, JSON.parse(file))
      if (file && JSON.parse(file).length > 0) {
        this.paramsData = Object.assign({}, JSON.parse(file)[0] || null, this.uploadParams)
      } else {
        this.paramsData = null
      }
    },
    // 导入
    hanldeOk (obj) {
      if (obj && obj.length > 0) {
        this.$emit('ok', obj, this.curRowData, this.uploadParams.promoProductClz, this.isTotalFlag)
        this.isShow = false
      }
    },
    // 关闭
    handleClose () {
      this.openImportModal = false
      this.openTotalImportModal = false
      this.isShow = false
    }
  },
  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>
