<template>
  <div class="app-container">
    <upload-excel-component
      :on-success="handleSuccess"
      :before-upload="beforeUpload"
    />
    <el-button @click="inputHandler">導入</el-button>
    <el-table
      :key="itemkey"
      :data="tableData"
      border
      highlight-current-row
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column
        v-for="item of tableHeader"
        :key="item"
        :prop="item"
        :label="item"
      />
    </el-table>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import UploadExcelComponent from './components/UploadExcel.vue'
import { fetchEhrEmployeeByName, fetchCaseLevelList } from "@api/uploadExcelApi"
import config from '@config/index.js'
export default {
  name:'uploadExcel',
  components: { UploadExcelComponent },
  props:{
    env:{
      type: String,
      default: 'prod_sz'
    }
  },
  data() {
    return {
      tableData: [],
      tableHeader: [],
      itemkey: '', // 临时变量
      levelKeys: [
        { name: 'level1', value: '核心' },
        { name: 'level2', value: '重要' },
        { name: 'level3', value: '常规' }
      ]
    }
  },
  computed: {
    ...mapGetters('user/', ['wadeUserID']),
    baseUrl(){
      console.log('config', config, this.env)
      return config[this.env].VUE_APP_GRAPHAPI_URL
    }
  },
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
      this.tableHeader.push('isUpload')
      // this.tableData = this.tableData.map(obj => ({ ...obj, isUpload: false }))
      this.levelKeys.map(level => {
        this.tableData.forEach((obj, index, array) => {
          array[index][level.name] = this.getLevel(level.value)
          array[index].isUpload = 'x'
        })
      })
    },
    inputHandler() {
      this.levelKeys.forEach(level => {
        console.log(`=====================${level.value}======================`)
        this.tableData
          .filter(item => {
            return item[level.value] === 'ν'
          })
          .map(async item => {
              const rest = await fetchEhrEmployeeByName(this.baseUrl,{ name: item['姓名'] })
                if (rest.status === 200) {
                  if (rest.data === null || rest.data.wade_user_id === null) {
                    this.$message({
                      type: 'error',
                      message: `系統找不到${item['姓名']}!`
                    })
                    return
                  }
                  console.log(rest.data.wade_user_id)
                  const params = {
                    customer_id: '0BE8F97E-320D-49D7-A13B-E4E5EF4738ED', // 華星
                    case_level: item[level.name],
                    match_case_user_id: rest.data.wade_user_id,
                    match_type: '1',
                    update_user_id: this.wadeUserID
                  }
                  const res = await fetchCaseLevelList(this.baseUrl,params)
                  if (res.status === 200) {
                    console.log(
                      `${item[level.name]} isUpload: ${item['姓名']}`
                    )
                    item.isUpload = 'ν'
                    this.itemkey = Math.random() // 在这边给key赋随机值
                    this.$message({ type: 'success', message: '新增成功!' })
                  }
                }
              })
          })
      // })
    },
    searchitem(index) {
      console.log(this.tableHeader[index])
      return this.tableHeader[index]
    },
    getLevel(level) {
      switch (level) {
        case '重要':
          return '重要專利'
        case '核心':
          return '核心專利'
        case '常规':
          return '常規專利'
      }
    }
  }
}
</script>
