<template>
  <div class="app-content" style="height:75vh;overflow: auto;">

    <div class="submit-btn upload-header">
      <FileUpload @beforeUpload="beforeUpload" @cancel="onCancel" />
    </div>
    <a-divider style="height: 1px; background-color: #e8e8e8;" />
    <div class="upload-table">
      <a-spin :spinning="loading" style="width: 100%;height: 100%;">
        <a-table :dataSource="tableData" :columns="columns" rowKey="id" :pagination="false">
        </a-table>
      </a-spin>
    </div>
    <div class="upload-bottom" style="text-align:center;padding:6px 0">
      <a-button type="primary" @click="dataImport" :disabled="disabled">导入</a-button>
    </div>

  </div>
</template>
<script>
import { isEmptyParams, readExcelFile, personGender, personBirthday, checkIdentitytionId, checkEmail, checkPhone } from "@/views/utils/common"
import moment from "moment"

export default {
  name: "projectImport",
  data () {
    return {
      columns: [],
      columnsName: [],
      tableData: [],
      disabled: true,
      loading: false,
    };
  },
  created () {

  },
  methods: {
    beforeUpload (file) {
      this.loading = true
      this.columns = []
      this.tableData = []
      this.columnsName = []
      let list = []
      readExcelFile(file, 0).then((data) => {
        if (data) {
          list = data
          if (list.length > 0) {
            for (var key in list[0]) {
              if (!this.columnsName.includes(key)) {
                this.columnsName.push(key)
                let columns = { title: key, dataIndex: key, align: 'center' }
                this.columns.push(columns)
              } else {
                alert('存在重复的列，请检查！')
                this.columns = []
                this.tableData = []
                break
              }
            }
          }
          if (this.columns.length > 0) {
            this.tableData = list
            this.disabled = false
          }
          this.loading = false
        } else this.loading = false
      })
      return false
    },
    dataImport () {
      if (this.tableData.length == 0) {
        this.$message.error('请选择Excel！')
        return
      }
      this.loading = true
      this.tableData.forEach(e => {
        if (e.created)
          e.created = e.created + ' 00:00:00'
        if (e.startDate)
          e.startDate = e.startDate + ' 00:00:01'
        if (e.endDate)
          e.endDate = e.endDate + ' 23:59:59'
      })
      this.$api.project.projectImport(this.tableData).then(({ data = {} }) => {
        if (data) {
          this.$message.success('成功！')
          this.tableData = data
        }
        this.loading = false
      })
    },
    onCancel () {
      this.loading = true
      this.columns = []
      this.tableData = []
      this.columnsName = []
      this.loading = false
    }
  },
};
</script>
<style scoped lang="less">
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
.upload-header {
  height: 30px;
}
.upload-table {
  min-height: 150px;
  max-height: calc(100% - 90px);
  overflow-y: auto;
}
.upload-bottom {
  height: 42px;
}
</style>

