// 上传设备
<template>
  <div>
    <el-dialog
      title="批量导入"
      width="600px"
      :visible.sync="dialogVisible"
      :destroy-on-close="false"
      :close-on-click-modal="false"
      @close="dialogClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="120px">
        <el-form-item label="服务商平台名称" prop="tenantId" required>
          <el-select v-model="form.tenantId" class="filter-item" placeholder="服务商平台名称" filterable>
            <el-option v-for="(item, index) in merchantLists" :key="index" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="场景" prop="sceneId" :required="false">
          <el-select v-model="form.sceneId" class="filter-item" placeholder="请选择场景" clearable>
            <el-option v-for="(item, index) in sceneLists" :key="index" :label="item.sceneName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="模式" prop="mode" required>
          <el-select v-model="form.mode" class="filter-item" placeholder="请选择模式">
            <el-option v-for="(item, index) in modeLists" :key="index" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="!(!form.mode || form.mode === 'public')" label="⽤户库" prop="userLibId" :required="false">
          <el-select v-model="form.userLibId" class="filter-item" placeholder="请选择⽤户库" clearable>
            <el-option v-for="(item, index) in userlibLists" :key="index" :label="item.userLibName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="上传文件" prop="correctList">
          <batch-upload ref="batchUploadRef" @getFileList="batchUploadResult" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </el-dialog>
    <!-- 批量注册人员-抽屉 -->
    <el-drawer
      :title="hasCompleted ? '导入已完成' : '正在处理中..'"
      :visible.sync="drawerVisible"
      :wrapper-closable="false"
      size="50%"
      direction="rtl"
    >
      <div style="padding: 50px">
        <div>
          已处理条数；{{ hasDealNumber }}; 待处理条数：{{ waitDealNumber }};
          总共条数：{{ allNumber }}
        </div>
        <el-progress
          :percentage="makePercent"
          :format="formatProgress"
          style="margin-top: 10px"
        />
        <el-table
          ref="batch_upload_result"
          v-loading="false"
          :data="batchUploadResultList"
          class="table"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          height="500"
        >
          <el-table-column label="错误序号" align="center" width="100px">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="设备sn" align="center">
            <template slot-scope="scope">
              {{ scope.row.sn }}
            </template>
          </el-table-column>
          <el-table-column label="公司名称" align="center">
            <template slot-scope="scope">
              {{ scope.row.company }}
            </template>
          </el-table-column>
          <el-table-column label="错误描述" align="center">
            <template slot-scope="scope">
              {{ scope.row.err }}
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align: center; margin: 20px 0">
          <el-button type="primary" @click="drawerVisible = false">
            我已知晓
          </el-button>
          <el-button type="warning" @click="downloadBatchUploadErrorList">
            下载错误列表文件
          </el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { add } from '@/api/device'
import BatchUpload from '@/components/BatchUpload'
import { getAllList } from '@/api/merchant'
import { getList as getSceneList } from '@/api/scene'
import { getList as getUserlibList } from '@/api/userlib'

export default {
  components: { BatchUpload },
  data() {
    return {
      merchantLists: [],
      sceneLists: [],
      userlibLists: [],
      modeLists: [
        {
          key: 'public',
          value: '开放'
        },
        {
          key: 'close',
          value: '封闭'
        },
        {
          key: 'elasticity',
          value: '弹性'
        }
      ],
      form: {
        tenantId: null,
        sceneId: null,
        userLibId: null,
        mode: null,
        correctList: {
          correct: [],
          incorrect: []
        }
      },
      rules: {
        tenantId: [
          { required: true, type: 'number', whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        mode: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        correctList: [
          { required: true, type: 'object', message: '模板文件不能为空!', trigger: ['change', 'blur'], validator: (rule, value, callback) => {
            if (Object.keys(value).length > 0 && ((value.correct && value.correct.length > 0) || (value.incorrect && value.incorrect.length > 0))) {
              return callback()
            }
            return callback('数据不能为空!')
          } }
        ]
      },
      dialogVisible: false,
      // 批量注册-处理提示值
      hasDealNumber: 0,
      waitDealNumber: 0,
      allNumber: 0,
      hasCompleted: false,
      // 批量注册-抽屉控制值
      drawerVisible: false,
      batchUploadResultList: []
    }
  },
  computed: {
    // 批量注册-返回向下取整，减少渲染次数
    makePercent() {
      return Math.floor((this.hasDealNumber / this.allNumber) * 100)
    }
  },
  watch: {
    'form.mode': {
      handler(value) {
        if (!value || value === 'public') {
          this.form.userLibId = null
        }
      },
      immediate: true
    }
  },
  created() {
    this.getMerchantList()
    this.getSceneList()
    this.getUserlibList()
  },
  methods: {
    open() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    getMerchantList() {
      getAllList().then(res => {
        this.merchantLists = res.data
      })
    },
    getSceneList() {
      getSceneList({ pageNumber: 1, pageSize: 1000 }).then(res => {
        this.sceneLists = res.data.records
      })
    },
    getUserlibList() {
      getUserlibList({ pageNumber: 1, pageSize: 1000 }).then(res => {
        this.userlibLists = res.data.records
      })
    },
    dialogClose() {
      this.$refs.batchUploadRef.clearFile()
    },
    batchUploadResult(correctList) {
      this.form.correctList = correctList
    },
    // 批量注册人员-操作
    getFileList(correctList) {
      this.hasCompleted = false
      this.drawerVisible = true
      this.hasDealNumber = correctList.incorrect.length
      this.waitDealNumber = correctList.correct.length
      this.allNumber = correctList.incorrect.length + correctList.correct.length
      this.batchUploadResultList = correctList.incorrect.map((item) => {
        return { name: item.file.name, err: item.reason }
      })
      this.makeScrollDown()
      this.doSubmitUrl(correctList, 0)
    },
    // 批量注册人员-并提交至添加到接口
    async doSubmitUrl(correctList, index) {
      if (index <= correctList.correct.length - 1) {
        const item = correctList.correct[index]
        if (item['设备sn'] && item['公司名称']) {
          // 组装数据上传
          const data = JSON.parse(JSON.stringify(this.form))
          data.sn = item['设备sn']
          data.company = item['公司名称']
          try {
            await add(data)
          } catch (err) {
            // console.log(err)
            this.batchUploadResultList.push({
              sn: data.sn,
              company: data.company,
              err: err
            })
          }
        } else {
          this.batchUploadResultList.push({
            sn: item['设备sn'] || '',
            company: item['公司名称'] || '',
            err: '不包含 "设备sn" 或 "公司名称" 字段'
          })
        }
        this.hasDealNumber += 1
        this.waitDealNumber -= 1
        this.makeScrollDown()
        // 继续调用
        this.doSubmitUrl(correctList, ++index)
      } else {
        this.hasCompleted = true
        this.$emit('finish')
      }
    },
    // 批量注册人员-保持滚动条在最底部
    makeScrollDown() {
      this.$nextTick(() => {
        this.$refs.batch_upload_result.$refs.bodyWrapper.scrollTop = this.$refs.batch_upload_result.$refs.bodyWrapper.scrollHeight
      })
    },
    // 批量注册人员-返回进度条尾部显示值
    formatProgress(percent) {
      return percent === 100 ? '已完成' : ''
    },
    // 批量注册人员-导出错误名单
    downloadBatchUploadErrorList() {
      const list = this.batchUploadResultList
      let string = '序号\t文件名\t错误原因\n'
      for (let i = 0; i < this.batchUploadResultList.length; i++) {
        string +=
          (i + 1).toString() +
          '\t' +
          list[i].name +
          '\t' +
          list[i].err +
          '\t' +
          '\n'
      }
      const blob = new Blob([string], { type: 'text/plain;charset=utf-8' })
      const excel_url = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = excel_url
      link.download = '批量注册结果错误名单.xls'
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    },
    submit(e) {
      e.preventDefault()
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false
        }
        this.dialogVisible = false
        this.getFileList(this.form.correctList)
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
.table {
  margin-top: 15px;
}
</style>
