<template>
  <div>
    <!-- 导入弹窗 -->
    <el-dialog
      ref="exportdialog"
      title="存量导入"
      :visible.sync="openLeadIn"
      width="580px"
      height="300px"
      append-to-body
      :close-on-click-modal="false"
      target="el-dialog__body"
      @closed="dialogClose"
    >
      <div
        v-loading="loading"
        class="dialogAAA"
        element-loading-text="正在导入中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0)"
        :style="{'pointer-events':loading ? '' : 'none'}"
      />
      <el-form ref="formLeadIn" :model="formLeadIn" :rules="rulesLeadIn" label-width="100px" class="addservice">
        <el-form-item label="导入数据类型：" prop="cardtypeid" class="ImportDataType">
          <el-select v-model.trim="formLeadIn.importType" filterable reserve-keyword placeholder="请选择导入数据类型" @change="changecardLeadintypename">
            <el-option v-for="item in importTypelist" :key="item.name" :label="item.label" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="longrentalShow" label="车场编号：" prop="parkid" class="importclass">
          <el-input
            v-model.trim="formLeadIn.parkid"
            clearable
            placeholder="请输入车场编号"
            @change="changeparkid"
          />
        </el-form-item>
        <el-form-item v-if="couponShow" label="车场编号：" prop="parkid" class="importclass">
          <el-input
            v-model.trim="formLeadIn.parkid"
            clearable
            placeholder="请输入车场编号"
            @change="couponchangeparkid"
          />
        </el-form-item>
        <el-form-item v-if="longrentalShow || couponShow" label="车场名称：" prop="parkname" class="importclass">
          <el-input
            v-model.trim="formLeadIn.parkname"
            clearable
            :disabled="true"
            placeholder="请输入车场名称"
          />
        </el-form-item>
        <el-form-item v-if="longrentalShow" label="套餐名称：" prop="cardtype" class="LeadIncardtype">
          <el-select v-model.trim="formLeadIn.cardtype" filterable clearable reserve-keyword placeholder="请选择套餐名称" @change="changecardtype">
            <el-option v-for="item in cardtypenamelist" :key="item.cardTypeId" :label="item.cardTypeName" :value="item.cardTypeId" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="couponShow" label="优惠券类型：" prop="couponType" class="LeadIncardtype">
          <el-select v-model.trim="formLeadIn.couponType" filterable clearable reserve-keyword placeholder="请选择优惠券类型" @change="changecouponType">
            <el-option v-for="item in couponlist" :key="item.name" :label="item.label" :value="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialog_link">
        <el-link type="primary" :underline="false" :class="linkShow?'':'gray'" :disabled="dislink" @click="importLink">
          下载导入模板
          <i type="primary" class="el-icon-download" />
        </el-link>
      </div>
      <el-upload
        ref="upload"
        class="upload-demo"
        :on-change="onChange"
        :auto-upload="false"
        :show-file-list="false"
        :file-list="fileList"
      >
        <div class="ele_dialog">
          <div slot="tip" class="ele_dialog_put">{{ upload_name }}</div>
          <el-button slot="trigger" class="ele_dialog_btn">浏览</el-button>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button :loading="loading" type="primary" :disabled="disimport" @click="submitFormLeadIn">确定</el-button>
      </div>

    </el-dialog>
  </div>
</template>
<script>
import {
  loadImportInvoicecorp,
  manageparkExcelImport,
  longrentalImportInvoicecorp,
  parkImportInvoicecorp,
  importShopCoupon,
  importShop,
  importPresentCoupon,
  importPaperCoupon,
  importEcoupon,
  importActivityCoupon,
  getparknamebyid,
  importECouponDistribution,
  importPreChargeDistribution }
  from '@/api/stockImport'
import { importInvoicecorp } from '@/api/longrentalcar'
import { getcardType } from '@/api/longrentalservice'
import { EventBus } from '@/utils/eventbus'
export default {
  props: {
    // 页面加载
    openLeadIn: {
      type: Boolean,
      default: false
    }
  },
  data() {
    var validNum = (rule, value, callback) => {
      if (this.parknamedate) {
        return callback(new Error('不存在该车编，请核实清楚再输入'))
      } else {
        callback()
      }
    }
    return {
      formLeadIn: {
        importType: '',
        parkid: '',
        parkname: '',
        cardtype: '',
        couponType: ''
      },
      loading: false,
      longrentalShow: false,
      couponShow: false,
      parkingShow: false,
      linkShow: false,
      dislink: true,
      disimport: true,
      fileInfo: null,
      uploadName: '',
      fileData: '',
      cardtypenamelist: '',
      parknamedate: false,
      upload_name: '',
      importTypelist: [
        {
          name: 0,
          label: '长租业务'
        },
        {
          name: 1,
          label: '优惠券'
        },
        {
          name: 2,
          label: '车场'
        }
      ],
      couponlist: [
        {
          name: 1002,
          label: '商户列表'
        },
        {
          name: 1003,
          label: '电子优惠券列表'
        },
        {
          name: 1004,
          label: '定时赠送优惠券列表'
        },
        {
          name: 1005,
          label: '纸质优惠券列表'
        },
        {
          name: 1006,
          label: '活动优惠券列表'
        },
        {
          name: 1007,
          label: '商户优惠券列表'
        },
        {
          name: 1008,
          label: ' 电子优惠券分配列表'
        },
        {
          name: 1009,
          label: ' 预分配优惠券分配列表'
        }
      ],
      rulesLeadIn: {
        parkid: [
          { required: true, message: '请输入车场编号', trigger: 'blur' },
          { validator: validNum, trigger: 'blur' }
        ],
        parkname: [
          { required: true, message: '请输入车场名称', trigger: 'blur' }
        ],
        cardtype: [
          { required: true, message: '请选择套餐名称', trigger: 'change' }
        ],
        couponType: [
          { required: true, message: '请选择优惠券类型', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    dialogClose() {
      this.$emit('update:openLeadIn', false)
      this.longrentalShow = false
      this.couponShow = false
      this.parkingShow = false
      this.linkShow = false
      this.dislink = true
      this.disimport = true
      this.upload_name = ''
      this.formLeadIn.importType = ''
      this.formLeadIn.parkid = ''
      this.formLeadIn.parkname = ''
      this.formLeadIn.cardtype = ''
      this.formLeadIn.couponType = ''
    },
    // 导入数据类型改变事件
    changecardLeadintypename() {
      this.$refs['formLeadIn'].resetFields()
      this.cardtypenamelist = ''
      this.upload_name = ''
      if (this.formLeadIn.importType == 0) {
        this.formLeadIn.parkid = ''
        this.formLeadIn.parkname = ''
        this.formLeadIn.cardtype = ''
        this.longrentalShow = true
        this.linkShow = true
        this.couponShow = false
        this.parkingShow == false
        this.dislink = false
        this.disimport = false
      } else if (this.formLeadIn.importType == 1) {
        this.formLeadIn.parkid = ''
        this.formLeadIn.parkname = ''
        this.formLeadIn.couponType = ''
        this.longrentalShow = false
        this.parkingShow == false
        this.couponShow = true
        this.disimport = false
        this.linkShow = false
      } else if (this.formLeadIn.importType == 2) {
        this.longrentalShow = false
        this.couponShow = false
        this.parkingShow == true
        this.linkShow = true
        this.dislink = false
        this.disimport = false
      }
    },
    changecouponType() {
      if (this.formLeadIn.couponType) {
        this.dislink = false
        this.linkShow = true
      } else {
        this.dislink = true
        this.linkShow = false
      }
    },
    // 下载模板
    importLink() {
      // 选择长租业务时下载的模板
      if (this.formLeadIn.importType == 0) {
        longrentalImportInvoicecorp().then(res => {
          if (res.data.value) {
            this.downloadExcel(window.open(res.data.value))
          } else {
            this.downloadExcel(res)
          }
        })
      } else if (this.formLeadIn.couponType) {
        // 选择优惠券时下载的模板
        loadImportInvoicecorp(this.formLeadIn.couponType).then(res => {
          if (res.data.value) {
            this.downloadExcel(window.open(res.data.value))
          } else {
            this.downloadExcel(res)
          }
        })
      } else if (this.formLeadIn.importType == 2) {
        // 选择车场时下载的模板
        parkImportInvoicecorp().then(res => {
          if (res.data.value) {
            this.downloadExcel(window.open(res.data.value))
          } else {
            this.downloadExcel(res)
          }
        })
      }
    },
    changeparkid() {
      this.formLeadIn.cardtype = ''
      this.formLeadIn.parkname = ''
      const nameparkid = this.formLeadIn.parkid
      getcardType({ parkid: this.formLeadIn.parkid }).then(res => {
        this.cardtypenamelist = res.data
      })
      if (nameparkid) {
        getparknamebyid(nameparkid).then(res => {
          if (res.data && res.data.myname) {
            this.$set(this.formLeadIn, 'parkname', res.data.myname)
            this.parknamedate = false
            this.$refs['formLeadIn'].validateField('parkid')
          } else {
            this.parknamedate = true
            console.log(this.$refs.formLeadIn)
            this.$refs['formLeadIn'].validateField('parkid')
          }
        })
      }
    },
    couponchangeparkid() {
      this.formLeadIn.parkname = ''
      const nameparkid = this.formLeadIn.parkid
      if (nameparkid) {
        getparknamebyid(nameparkid).then(res => {
          if (res.data && res.data.myname) {
            this.$set(this.formLeadIn, 'parkname', res.data.myname)
            this.parknamedate = false
            this.$refs['formLeadIn'].validateField('parkid')
          } else {
            this.parknamedate = true
            console.log(this.$refs.formLeadIn)
            this.$refs['formLeadIn'].validateField('parkid')
          }
        })
      }
    },
    // 选择对应的文件
    onChange(file) {
      if (this.fileData) {
        this.fileData.delete('parkid', this.formLeadIn.parkid)
        this.fileData.delete('cardtypeid', this.formLeadIn.cardtype)
        this.fileData.delete('file', file.raw)
      }
      this.fileInfo = file
      this.fileData = new FormData()
      this.fileData.append('file', file.raw)
      this.upload_name = this.fileInfo.name
      this.formLeadIn.file = this.fileData
    },
    // 确定导入
    submitFormLeadIn() {
      this.$refs['formLeadIn'].validate(valid => {
        if (valid) {
          if (this.upload_name) {
            if (this.formLeadIn.importType == 0) {
              var txt1 = `<p class="text-center mt16">确认在${this.formLeadIn.parkname}车场导入此${this.importTypelist[this.formLeadIn.importType].label}吗？</p>`
              this.$confirm(txt1, '导入确认', {
                dangerouslyUseHTMLString: true,
                type: 'none'
              }).then(() => {
                this.longrentalUpload()
              })
            } else if (this.formLeadIn.importType == 1) {
              var txt2 = `<p class="text-center mt16">确认在${this.formLeadIn.parkname}车场导入此${this.importTypelist[this.formLeadIn.importType].label}吗？</p>`
              this.$confirm(txt2, '导入确认', {
                dangerouslyUseHTMLString: true,
                type: 'none'
              }).then(() => {
                this.couponUpload()
              })
            } else if (this.formLeadIn.importType == 2) {
              this.parkUpload()
            }
          } else {
            this.$message({
              message: '请选择导入文件',
              type: 'warning'
            })
            this.loading = false
          }
        }
      })
    },
    // 长租业务导入接口
    longrentalUpload() {
      this.fileData.append('parkid', this.formLeadIn.parkid)
      this.fileData.append('cardtypeid', this.formLeadIn.cardtype)
      this.$nextTick(() => {
        this.loading = true
      })
      this.loading = true
      importInvoicecorp(this.fileData).then(res => {
        if (res.success) {
          this.$emit('update:openLeadIn', false)
          EventBus.$emit('getListBox')
        } else {
          this.$emit('update:openLeadIn', false)
          EventBus.$emit('getListBox')
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$emit('update:openLeadIn', false)
        this.fileData = new FormData()
      })
    },
    // 导入套餐改变
    changecardtype() {
      if (this.fileData) {
        this.fileData.delete('parkid', this.formLeadIn.parkid)
        this.fileData.delete('cardtypeid', this.formLeadIn.cardtype)
      }
    },
    // 不同优惠券调用不同的导入接口
    couponUpload() {
      console.log('aaaa', this.formLeadIn.couponType)
      switch (this.formLeadIn.couponType) {
        // 导入商户列表
        case 1002:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importShop(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch((e) => {
            debugger
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
          // 导入电子优惠券列表
        case 1003:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importEcoupon(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
          // 导入定时赠送优惠券列表
        case 1004:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importPresentCoupon(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
          // 导入纸质优惠券列表
        case 1005:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importPaperCoupon(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
          // 导入活动优惠券列表
        case 1006:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importActivityCoupon(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
          // 导入商户优惠券列表
        case 1007:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importShopCoupon(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
          // 导入电子优惠券分配列表
        case 1008:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importECouponDistribution(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
        case 1009:
          this.fileData.append('parkid', this.formLeadIn.parkid)
          this.fileData.append('parkname', this.formLeadIn.parkname)
          this.$nextTick(() => {
            this.loading = true
          })
          this.loading = true
          importPreChargeDistribution(this.fileData).then(res => {
            if (res.success) {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            } else {
              this.$emit('update:openLeadIn', false)
              EventBus.$emit('getListBox')
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.$emit('update:openLeadIn', false)
            this.fileData = new FormData()
          })
          break
        default:
          break
      }
    },
    // 车场导入接口
    parkUpload() {
      this.$nextTick(() => {
        this.loading = true
      })
      this.loading = true
      manageparkExcelImport(this.fileData).then(res => {
        if (res.success) {
          this.$emit('update:openLeadIn', false)
          EventBus.$emit('getListBox')
        } else {
          this.$emit('update:openLeadIn', false)
          EventBus.$emit('getListBox')
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$emit('update:openLeadIn', false)
        this.fileData = new FormData()
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.ele_dialog {
  display: flex;
  margin-left: 20px;
  margin-right: 20px;
  padding: 2px;
  border: 1px solid #DDE2EE;
  .ele_dialog_put{
    width: 382px;
    padding-left: 20px;
    margin-right: 10px;
    text-align: left;
    line-height: 36px
  }
  .ele_dialog_btn {
    background: #FFFFFF;
    color: #2D72FF;
    border: 1px solid #2D72FF;
    border-radius: 30px;
  }
}
.dialog_link {
  margin-left: 20px;
}
.dialog_linkresult {
  margin-left: 20px;
  margin-bottom: 10px;
}
.dialogAAA {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 90%;
}
.ImportDataType {
  margin-right: 20px;
  margin-left: 20px;
}
.LeadIncardtype {
  margin-right: 20px;
  margin-bottom: 20px;
  margin-left: 20px;
}
.importclass {
  margin-right: 20px;
  margin-left: 20px;
}
.gray {
  color: #b9b8b8 !important;
}
  /deep/.el-dialog__body {
     height: 370px !important;
  }
</style>

