<template>
  <div class="main-contain">
    <el-dialog
      title="提示"
      :visible.sync="exportVisible"
      width="40%"
    >

      <div>
        您提交的排班信息已上传成功，系统正在执行导入，请稍候点击当前页面右上角“下载日志”查看导入结果
      </div>

      <span slot="footer" class="dialog-footer">
    <el-button @click="exportVisible = false">取 消</el-button>
    <el-button type="primary" @click="exportVisible = false">确 定</el-button>
  </span>
    </el-dialog>


    <el-dialog
      title="提示"
      :visible.sync="exportLogVisible"
      width="40%"
    >

      <div v-for="(item,index) in logs" :key="index">
        <span> {{ item.export_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</span>
        <br/>
        <br/>
        <span>{{getContent(item)}}</span>
        <span >点击</span>
        <span style="color:blue" @click="generateTxt(item)">查看详情</span>
        <br/>
        <br/>
      </div>

      <span slot="footer" class="dialog-footer">
    <el-button @click="exportLogVisible = false">取 消</el-button>
    <el-button type="primary" @click="exportLogVisible = false">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <span>您提交的文档不是系统标准导入模板，请检查您的文档或重新下载文档</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>


    <el-dialog
      title="提示"
      :visible.sync="isExistRepeatVisible"
      width="40%"
    >
      <span>检查到文档中同一天同个人存在多个排班,请检查并修改您的文档,重新导入</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="isExistRepeatVisible = false">取 消</el-button>
    <el-button type="primary" @click="isExistRepeatVisible = false">确 定</el-button>
  </span>
    </el-dialog>

    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
        <div style="display:flex;align-items:center">
          <schedule-upload-excel style="margin-right:10px;" :on-success='handleSuccess' :before-upload="beforeUpload"></schedule-upload-excel>
          <el-button v-if="isShow()" type="primary" size="small" @click="export_file()" :loading="downloadLoading">
            导出模版
          </el-button>

          <el-button
            @click="generateLog()"
            class="filter-item"
            type="primary"
            size="small"
          >下载日志
          </el-button>

          <el-button type="primary" size="small" icon="el-icon-printer" @click="printScheduleAction">打印</el-button>
          <el-button type="primary" size="small" icon="el-icon-setting" @click="setScheduleTemplateAction">排班模板
          </el-button>
        </div>
    </div>
    <div class="app-container">
      <!-- <el-row>
        <el-col :span="24"> -->
      <el-tabs v-model="activeName" :tab-position="tabPosition" ref="elTabs">
        <el-tab-pane name="lastWeek">
          <span slot="label"><i class="el-icon-date"></i> 上周 ({{theWeek.lastWeek}})</span>
        </el-tab-pane>
        <el-tab-pane name="thisWeek">
          <span slot="label"><i class="el-icon-date"></i> 本周 ({{theWeek.thisWeek}})</span>
        </el-tab-pane>
        <el-tab-pane name="nextWeek">
          <span slot="label"><i class="el-icon-date"></i> 下一周 ({{theWeek.nextWeek}})</span>
        </el-tab-pane>
        <el-tab-pane name="nextTwoWeek">
          <span slot="label"><i class="el-icon-date"></i> 下下周 ({{theWeek.nextTwoWeek}})</span>
        </el-tab-pane>
      </el-tabs>
      <!-- </el-col>
    </el-row> -->

      <table-data :week-time="activeName" :partitions-prop="partitions" :schedule-zone-row-prop="scheduleZoneRow"
                  :schedule-zone-prop="scheduleZone" title=""></table-data>
    </div>
  </div>
</template>

<script>
  import { parseTime } from '@/utils'
  import tableData from './components/tableData'
  import { exportSchedule, getWeekPanels, initDate } from '@/api/schedule'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import UploadExcel from '@/xt_pages/components/UploadExcel'
  import ScheduleUploadExcel from './components/scheduleUploadExcel/index'
  import { generateLog } from '@/api/config'

  export default {
    name: 'appointment',
    data() {
      return {
        crumbs: [
          { path: false, name: '排班管理' },
          { path: '/workforce/appointment', name: '排班管理' }
        ],
        activeName: 'thisWeek',
        tabPosition: 'top',
        partitions: {},
        scheduleZone: [],
        days: [],
        logs: [],
        scheduleZoneRow: [],
        downloadLoading: false,
        msgTipVisible: false,
        dialogVisible: false,
        isExistRepeatVisible: false,
        exportVisible:false,
        exportLogVisible: false,
        theWeek: {
          lastWeek: 0,
          thisWeek: 0,
          nextWeek: 0,
          nextTwoWeek: 0
        }
      }
    },
    components: {
      ScheduleUploadExcel,
      tableData,
      BreadCrumb,
      UploadExcel
    },
    methods: {
      generateTxt: function(log) {
        var content = ''
        var errlog = log.err_logs
        content = this.getContent(log)

        for (let i = 0; i < errlog.length; i++) {
          if (content.length == 0) {
            content = errlog[i].err_msg
          } else {
            content = content + '\n' + errlog[i].err_msg
          }
        }

        var url = new Blob(['\ufeff' + content], { type: 'text/txt,charset=UTF-8' })
        if (typeof url === 'object' && url instanceof Blob) {
          url = URL.createObjectURL(url) // 创建blob地址
        }
        const aLink = document.createElement('a')
        aLink.href = url
        aLink.download = this.timestampToTime(log.export_time) + '临时排班导入日志.txt'
        aLink.click()

      },
      timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000)//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '年'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月'
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + '日 '
        var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + '时'
        var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + '分'
        var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds()) + '秒'
        return Y + M + D + h + m + s
      },
      getContent(log) {
        return '您导入的文档共' + log.total_num + '条患者数据' + '，' + '已成功导入' + log.success_num + '条，导入失败' + log.fail_num + '条,'
      },
      generateLog() {
        let params = {
          'log_type': 2
        }
        generateLog(params).then(
          response => {
            if (response.data.state === 1) {
              this.logs = response.data.data.logs
              this.exportLogVisible = true
            } else {
              this.$message.error(response.data.msg)
            }
          }
        )

      },
      getWeekPanels() {
        // this.scheduleZoneRow = []
        // this.scheduleZone = []
        getWeekPanels(1).then(response => {
          if (response.data.state == 0) {
            return false
          }
          var partitions = response.data.data.partitions
          this.theWeek.thisWeek = response.data.data.theWeek
          this.theWeek.lastWeek = this.theWeek.thisWeek - 1
          this.theWeek.nextWeek = this.theWeek.thisWeek + 1
          this.theWeek.nextTwoWeek = this.theWeek.thisWeek + 2
          // 在控制变量改变的时候进行 强制渲染更新
          let childrenRefs = this.$refs.elTabs.$children
          this.$nextTick(() => {
            childrenRefs.forEach(child => child.$forceUpdate())
          })

          var that = this
          if (partitions.length > 0) {
            partitions.forEach(function(partition) {
              if (partition.jihaos.length == 0) {
                return false
              }
              that.scheduleZoneRow.push(partition.jihaos.length)
              that.partitions[partition.id] = partition
              partition.jihaos.forEach(function(jihao) {
                var thisPa = {
                  area: partition.name,
                  zone_id: partition.id,
                  zone_type: partition.type,
                  cut: jihao.number,
                  jihao_id: jihao.id,
                  Mon_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Mon_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Mon_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Tue_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Tue_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Tue_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Wed_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Wed_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Wed_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Thurs_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Thurs_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Thurs_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Fri_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Fri_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Fri_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Sat_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Sat_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Sat_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Sun_A: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Sun_N: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  Sun_M: {
                    schedule_id: 0,
                    mode_id: 0,
                    mode_name: '',
                    patient_id: 0,
                    patient: ''
                  },
                  total: 0
                }
                that.scheduleZone.push(thisPa)
              })
            })
          }
        })
      },
      setScheduleTemplateAction: function() {
        this.$router.push({ path: '/workforce/template' })
      }, export_file: function() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const filterVal = [
            '区号',
            '机号'
          ]
          const multiHeader = [['区号', '机号', '周一'+"("+this.days[0]+")" , '', '', '周二'+"("+this.days[1]+")", '', '', '周三'+"("+this.days[2]+")" , '', '', '周四'+"("+this.days[3]+")" , '', '', '周五'+"("+this.days[4]+")" , '', '', '周六'+"("+this.days[5]+")" , '', '', '周日'+"("+this.days[6]+")" , '', '', '周一'+"("+this.days[7]+")" , '', '', '周二'+"("+this.days[8]+")" , '', '', '周三'+"("+this.days[9]+")" , '', '', '周四'+"("+this.days[10]+")" , '', '', '周五'+"("+this.days[11]+")" , '', '', '周六'+"("+this.days[12]+")" , '', '', '周日'+"("+this.days[13]+")", '', '', '周一'+"("+this.days[14]+")", '', '', '周二'+"("+this.days[15]+")", '', '', '周三'+"("+this.days[16]+")", '', '', '周四'+"("+this.days[17]+")", '', '', '周五'+"("+this.days[18]+")", '', '', '周六'+"("+this.days[19]+")", '', '', '周日'+"("+this.days[20]+")", '', '']]
          const header = ['', '', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上']
          // const merges = ['A1:A2', 'B1:B2']
          const merges = ['A1:A2', 'B1:B2', 'C1:E1', 'F1:H1', 'I1:K1', 'L1:N1', 'O1:Q1', 'R1:T1', 'U1:W1', 'X1:Z1', 'AA1:AC1', 'AD1:AF1', 'AG1:AI1', 'AJ1:AL1', 'AM1:AO1', 'AP1:AR1', 'AS1:AU1', 'AV1:AX1', 'AY1:BA1', 'BB1:BD1', 'BE1:BG1', 'BH1:BJ1', 'BK1:BM1']
          var list = []
          let i = 0
          for (let key  in this.partitions) {
            var tempValue
            var jihaos = this.partitions[key].jihaos

            if (i == 0) {
              let m = 'A3' + ':' + 'A' + (2 + jihaos.length)
              tempValue = (2 + jihaos.length)
              merges.push(m)
            } else {
              let m = 'A' + (tempValue + 1) + ':' + 'A' + ((tempValue + 1) + jihaos.length - 1)
              tempValue = ((tempValue + 1) + jihaos.length - 1)
              merges.push(m)
            }
            for (let b = 0; b < jihaos.length; b++) {
              let obj = {}
              obj['区号'] = this.partitions[key].name
              obj['机号'] = jihaos[b].number.toString()
              list.push(obj)
            }

            i++
          }
          console.log(list)
          const data = this.formatJson(filterVal, list)
          console.log("data",data)
          const filename = '排班模版'
         
          excel.export_json_to_excel({
            multiHeader,
            header,
            merges,
            data,
            filename
          })
          this.downloadLoading = false
        })
     
      }, formatJson(filterVal, jsonData) {
        return jsonData.map(v =>
          filterVal.map(j => {
            console.log(v)
            console.log(j)
            console.log(v[j])

            if (j === 'timestamp') {
              return parseTime(v[j])
            } else {
              return v[j]
            }
          })
        )
      },
      printScheduleAction: function() {
        var date = new Date().getTime()
        if (this.activeName == 'lastWeek') {
          date = date - 7 * 24 * 60 * 60 * 1000
        } else if (this.activeName == 'nextWeek') {
          date = date + 7 * 24 * 60 * 60 * 1000
        } else if (this.activeName == 'nextTwoWeek') {
          date = date + 14 * 24 * 60 * 60 * 1000
        }
        this.$router.push({
          path: '/workforce/schedule/print',
          query: { date: date }
        })
      }, isShow() {
        let isShow = false
        if (Object.keys(this.partitions).length != 0) {
          isShow = true
          let i = 0
          for (let key in this.partitions) {
            if (i == 0) {
              if (this.partitions[key].jihaos.length > 0) {
                isShow = true

              }
            }
            i++
          }
        }
        return isShow
      }, handleSuccess({ results, header }) {
        console.log(header)
        console.log(results)


        let headers = []
        for (let key in results[0]) {
          headers.push(key)
        }
        if (results.length > 0) {
          var isHas_1 = headers.includes('区号')
          console.log("1"+isHas_1)
          var isHas_2 = headers.includes('机号')
          console.log("2"+isHas_2)


          var isHas_3 = headers.includes('周一'+"("+this.days[0]+")")
          console.log("3"+isHas_3)


          var isHas_4 = headers.includes('')
          console.log("4"+isHas_4)


          var isHas_5 = headers.includes('_1')
          console.log("5"+isHas_5)

          var isHas_6 = headers.includes('周二'+"("+this.days[1]+")")
          console.log("6"+isHas_6)

          var isHas_7 = headers.includes('_2')
          console.log("7"+isHas_7)

          var isHas_8 = headers.includes('_3')
          console.log("8"+isHas_8)

          var isHas_9 = headers.includes('周三'+"("+this.days[2]+")")
          console.log("9"+isHas_9)

          var isHas_10 = headers.includes('_4')
          console.log("10"+isHas_10)

          var isHas_11 = headers.includes('_5')
          console.log("11"+isHas_11)

          var isHas_12 = headers.includes('周四'+"("+this.days[3]+")")
          console.log("12"+isHas_12)

          var isHas_13 = headers.includes('_6')
          console.log("13"+isHas_13)

          var isHas_14 = headers.includes('_7')
          console.log("14"+isHas_14)

          var isHas_15 = headers.includes('周五'+"("+this.days[4]+")")
          console.log("15"+isHas_15)

          var isHas_16 = headers.includes('_8')
          console.log("16"+isHas_16)


          var isHas_17 = headers.includes('_9')
          console.log("17"+isHas_17)


          var isHas_18 = headers.includes('周六'+"("+this.days[5]+")")
          console.log("18"+isHas_18)


          var isHas_19 = headers.includes('_10')
          console.log("19"+isHas_19)


          var isHas_20 = headers.includes('_11')
          console.log("20"+isHas_20)


          var isHas_21 = headers.includes('周日'+"("+this.days[6]+")")
          console.log("21"+isHas_21)

          var isHas_22 = headers.includes('_12')
          console.log("22"+isHas_22)

          var isHas_23 = headers.includes('_13')
          console.log("23"+isHas_23)


          var isHas_24 = headers.includes('周一'+"("+this.days[7]+")")
          console.log("24"+isHas_24)

          var isHas_25 = headers.includes('_14')
          console.log("25"+isHas_25)

          var isHas_26 = headers.includes('_15')
          console.log("26"+isHas_26)

          var isHas_27 = headers.includes('周二'+"("+this.days[8]+")")
          console.log("27"+isHas_27)

          var isHas_28 = headers.includes('_16')
          console.log("28"+isHas_28)

          var isHas_29 = headers.includes('_17')
          console.log("29"+isHas_29)

          var isHas_30 = headers.includes('周三'+"("+this.days[9]+")")
          console.log("30"+isHas_30)

          var isHas_31 = headers.includes('_18')
          console.log("31"+isHas_31)

          var isHas_32 = headers.includes('_19')
          console.log("32"+isHas_32)

          var isHas_33 = headers.includes('周四'+"("+this.days[10]+")")
          console.log("33"+isHas_33)

          var isHas_34 = headers.includes('_20')
          console.log("34"+isHas_34)

          var isHas_35 = headers.includes('_21')
          console.log("35"+isHas_35)

          var isHas_36 = headers.includes('周五'+"("+this.days[11]+")")
          console.log("36"+isHas_36)

          var isHas_37 = headers.includes('_22')
          console.log("37"+isHas_37)

          var isHas_38 = headers.includes('_23')
          console.log("38"+isHas_38)

          var isHas_39 = headers.includes('周六'+"("+this.days[12]+")")
          console.log("39"+isHas_39)

          var isHas_40 = headers.includes('_24')
          console.log("40"+isHas_40)

          var isHas_41 = headers.includes('_25')
          console.log("41"+isHas_41)

          var isHas_42 = headers.includes('周日'+"("+this.days[13]+")")
          console.log("42"+isHas_42)

          var isHas_43 = headers.includes('_26')
          console.log("43"+isHas_43)

          var isHas_44 = headers.includes('_27')
          console.log("44"+isHas_44)

          var isHas_45 = headers.includes('周一'+"("+this.days[14]+")")
          console.log("45"+isHas_45)

          var isHas_46 = headers.includes('_28')
          console.log("46"+isHas_46)

          var isHas_47 = headers.includes('_29')
          console.log("47"+isHas_47)

          var isHas_48 = headers.includes('周二'+"("+this.days[15]+")")
          console.log("48"+isHas_48)

          var isHas_49 = headers.includes('_30')
          console.log("49"+isHas_49)

          var isHas_50 = headers.includes('_31')
          console.log("50"+isHas_50)

          var isHas_51 = headers.includes('周三'+"("+this.days[16]+")")
          console.log("51"+isHas_51)

          var isHas_52 = headers.includes('_32')
          console.log("52"+isHas_52)

          var isHas_53 = headers.includes('_33')
          console.log("53"+isHas_53)

          var isHas_54 = headers.includes('周四'+"("+this.days[17]+")")
          console.log("54"+isHas_54)

          var isHas_55 = headers.includes('_34')
          console.log("55"+isHas_55)

          var isHas_56 = headers.includes('_35')
          console.log("56"+isHas_56)

          var isHas_57 = headers.includes('周五'+"("+this.days[18]+")")
          console.log("57"+isHas_57)

          var isHas_58 = headers.includes('_36')
          console.log("58"+isHas_58)

          var isHas_59 = headers.includes('_37')
          console.log("59"+isHas_59)


          var isHas_60 = headers.includes('周六'+"("+this.days[19]+")")
          console.log("60"+isHas_60)

          var isHas_61 = headers.includes('_38')
          console.log("61"+isHas_61)

          var isHas_62 = headers.includes('_39')
          console.log("62"+isHas_62)

          var isHas_63 = headers.includes('周日'+"("+this.days[20]+")")
          console.log("63"+isHas_63)

          var isHas_64 = headers.includes('_40')
          console.log("64"+isHas_64)

          var isHas_65 = headers.includes('_41')
          console.log("65"+isHas_65)


          if (!(isHas_1 && isHas_2 && isHas_3 && isHas_4 && isHas_5 &&
            isHas_6 && isHas_7 && isHas_8 && isHas_9 && isHas_10 &&
            isHas_11 && isHas_12 && isHas_13 && isHas_14 && isHas_15 &&
            isHas_16 && isHas_17 && isHas_18 && isHas_19 && isHas_20 &&
            isHas_21 && isHas_22 && isHas_23 && isHas_24 && isHas_25 &&
            isHas_26 && isHas_27 && isHas_28 && isHas_29 && isHas_30 &&
            isHas_31 && isHas_32 && isHas_33 && isHas_34 && isHas_35 &&
            isHas_36 && isHas_37 && isHas_38 && isHas_39 && isHas_40 &&
            isHas_41 && isHas_42 && isHas_43 && isHas_44 && isHas_45 &&
            isHas_46 && isHas_47 && isHas_48 && isHas_49 && isHas_50 &&
            isHas_51 && isHas_52 && isHas_53 && isHas_54 && isHas_55 &&
            isHas_56 && isHas_57 && isHas_58 && isHas_59 && isHas_60 &&
            isHas_61 && isHas_62 && isHas_63 && isHas_64 && isHas_65)) {

            console.log("222222222")

            this.dialogVisible = true
            return
          }
        } else {
          console.log("11111111")

          this.dialogVisible = true
          return
        }

        let is_system_zone = true
        let is_device_number = true
        let zones = []
        let deviceNumber = []

        for (let i = 0; i < results.length; i++) {
          if (results[i + 1]) {
            zones.push(results[i + 1]['区号'])
          }
          if (results[i + 1]) {
            deviceNumber.push(results[i + 1]['机号'])
          }

        }

        const obj = {}
        zones = zones.reduce((cur, next) => {
          obj[next] ? '' : obj[next] = true && cur.push(next)
          return cur
        }, []) // 设置cur默认类型为数组，并且初始值为空的数组

        const obj2 = {}
        deviceNumber = deviceNumber.reduce((cur, next) => {
          obj2[next] ? '' : obj2[next] = true && cur.push(next)
          return cur
        }, []) // 设置cur默认类型为数组，并且初始值为空的数组



        var total_zones = []
        var total_device_number = []

        for (let key in this.partitions) {
          total_zones.push(this.partitions[key].name)
        }

        for (let key in this.partitions) {
          var jihaos = this.partitions[key].jihaos

          for (let i = 0; i < jihaos.length; i++) {
            total_device_number.push(jihaos[i].number)
          }
        }

        for (let i = 0; i < zones.length; i++) {
          if (!total_zones.includes(zones[i])) {
            is_system_zone = false
          }
        }


        console.log("number:")
        console.log(total_device_number)
        console.log("23121212:")
        console.log(deviceNumber)


        for (let i = 0; i < deviceNumber.length; i++) {
          if (!total_device_number.includes(deviceNumber[i])) {
            console.log("number:11111")
            is_device_number = false
          }
        }




        let tempObj = null
        if (results.length > 0) {
          tempObj = results[0]
        }

        let datas = []
        let failed_datas = []

        for (let i = 0; i < results.length; i++) {
          for (let key in results[i + 1]) {
            let tempObjTwo = { partition_name: '', device_number_name: '' }

            if (key != '区号' && key != '机号') {
              let obj = {}
              obj['name'] = results[i + 1][key].replace(/\s/g, '')
              if (tempObj[key] == '上午') {
                obj['schedule_type'] = 1
              } else if (tempObj[key] == '下午') {
                obj['schedule_type'] = 2
              } else if (tempObj[key] == '晚上') {
                obj['schedule_type'] = 3
              } else {

                obj['schedule_type'] = 0

              }
              obj['schedule_week'] = this.getWeekByResultsStr(key)
              obj['schedule_date'] = this.getDateByResultsStr(key)
              obj['row'] = this.getRow(key)

              tempObjTwo = obj
            }

            tempObjTwo['partition_name'] = results[i + 1]['区号']
            tempObjTwo['device_number_name'] = results[i + 1]['机号']
            tempObjTwo['index'] = i + 3

            if (tempObjTwo['schedule_week'] != undefined && tempObjTwo['name'] != undefined && tempObjTwo['schedule_type'] != undefined && tempObjTwo['schedule_date'] != undefined) {
              var current_date = new Date()
              var m_year = current_date.getFullYear()
              var m_month = current_date.getMonth() + 1
              m_month = m_month < 10 ? '0' + m_month.toString() : m_month.toString()
              var m_date = current_date.getDate() < 10 ? '0' + current_date.getDate().toString() : current_date.getDate().toString()
              var current_date_two = new Date(m_year + '-' + m_month + '-' + m_date)
              if (new Date(tempObjTwo['schedule_date']).getTime() > new Date(current_date_two).getTime()) {
                datas.push(tempObjTwo)
              }else{
                failed_datas.push(tempObjTwo)
              }
            }
          }

        }

        console.log(datas)


        let date_arr = []
        //校验文档是否规范
        for (let i = 0; i < datas.length; i++) {
          if (datas[i].schedule_date !== '0') {
            date_arr.push(datas[i].schedule_date)
          }

          if (datas[i].device_number_name == undefined) {
            console.log(datas[i].name)
            console.log("number:222222")

            is_device_number = false

          }

          if (datas[i].partition_name == undefined) {
            is_system_zone = false

          }

          if (datas[i].schedule_date === '0') {
            is_system_zone = false

          }

          if (datas[i].schedule_week === '0') {
            is_system_zone = false

          }

          if (datas[i].schedule_type === 0) {
            is_system_zone = false

          }
        }



        //校验文档中同一天同个人是否存在多个排班
        let is_exist_repeat = false
        const obj3 = {}
        date_arr = date_arr.reduce((cur, next) => {
          obj3[next] ? '' : obj3[next] = true && cur.push(next)
          return cur
        }, []) // 设置cur默认类型为数组，并且初始值为空的数组

        let tempObjArr = []
        for (let i = 0; i < date_arr.length; i++) {
          let obj = {}
          let key = date_arr[i]
          let value = []

          for (let b = 0; b < datas.length; b++) {
            if (key == datas[b].schedule_date) {
              value.push(datas[b].name)
            }
          }
          obj[key] = value
          tempObjArr.push(obj)
        }

        for (let i = 0; i < tempObjArr.length; i++) {
          for (let key in tempObjArr[i]) {
            let arr = tempObjArr[i][key]
            const obj = {}
            arr = arr.reduce((cur, next) => {
              obj[next] ? '' : obj[next] = true && cur.push(next)
              return cur
            }, []) // 设置cur默认类型为数组，并且初始值为空的数组

            if (tempObjArr[i][key].length > arr.length) {    //比对原数组和去重后的原数组总数，不同的话则存在重复元素
              is_exist_repeat = true
            }
          }
        }

        console.log(is_system_zone)
        console.log(is_device_number)
        console.log(headers.length)


        if (headers.length > 65 || !is_system_zone || !is_device_number) {
          this.dialogVisible = true
          return
        } else {
          if (is_exist_repeat) {
            this.isExistRepeatVisible = true
            return
          } else {
            this.exportVisible = true

            this.$alert('您提交的排班信息已上传成功，系统正在执行导入，请稍候点击当前页面右上角“下载日志”查看导入结果', '上传成功', {
              confirmButtonText: '确定',
              callback: action => {

              }
            });


            let params = {
              schedule: datas,
              failed_schedule: failed_datas

            }

            //今天的时间
            var day2 = new Date()
            day2.setTime(day2.getTime())
            var month = day2.getMonth() + 1 < 10 ? '0' + (day2.getMonth() + 1).toString() : (day2.getMonth() + 1).toString()
            var day = day2.getDate() < 10 ? '0' + (day2.getDate()).toString() : (day2.getMonth() + 1).toString()
            var date = day2.getFullYear() + '-' + month + '-' + day
            exportSchedule(params, date).then(response => {
              if (response.data.state == 0) {
                return false
              }
              this.getWeekPanels()

            })
          }

        }

      },
      getInitDate() {
        initDate().then(response => {
          if (response.data.state == 0) {
            return false
          }
          this.days = response.data.data.days
        })
      },
      getWeekByResultsStr(key) {

        switch (key) {
          case '周一'+"("+this.days[0]+")":
            return '1'
            break
          case  '周一'+"("+this.days[7]+")":
            return '1'
            break

          case   '周一'+"("+this.days[14]+")":
            return '1'
            break

          case   '':
            return '1'
            break

          case    '_1':
            return '1'
            break

          case   '_14':
            return '1'
            break

          case   '_15':
            return '1'
            break

          case    '_28':
            return '1'
            break

          case   '_29':
            return '1'
            break

          case '周二'+"("+this.days[1]+")" :
            return '2'
            break
          case '周二'+"("+this.days[8]+")":
            return '2'
            break
          case '周二'+"("+this.days[15]+")" :
            return '2'
            break

          case '_2':
            return '2'
            break

          case '_3':
            return '2'
            break

          case  '_16' :
            return '2'
            break

          case '_17' :
            return '2'
            break

          case '_30':
            return '2'
            break

          case  '_31':
            return '2'
            break

          case '周三'+"("+this.days[2]+")":
            return '3'
            break
          case '周三'+"("+this.days[9]+")":
            return '3'
            break
          case  '周三'+"("+this.days[16]+")":
            return '3'
            break
          case  '_4' :
            return '3'
            break
          case '_5'  :
            return '3'
            break
          case  '_18'  :
            return '3'
            break
          case  '_19' :
            return '3'
            break
          case  '_32' :
            return '3'
            break
          case  '_33' :
            return '3'
            break

          case '周四'+"("+this.days[3]+")":
            return '4'
            break

          case '周四'+"("+this.days[10]+")" :
            return '4'
            break
          case'周四'+"("+this.days[17]+")" :
            return '4'
            break
          case  '_6' :
            return '4'
            break

          case  '_7':
            return '4'
            break
          case   '_20':
            return '4'
            break
          case  '_21':
            return '4'
            break
          case   '_34':
            return '4'
            break
          case   '_35':
            return '4'
            break

          case '周五'+"("+this.days[4]+")" :
            return '5'
            break

          case  '周五'+"("+this.days[11]+")":
            return '5'
            break
          case  '周五'+"("+this.days[18]+")" :
            return '5'
            break
          case '_8':
            return '5'
            break
          case '_9' :
            return '5'
            break
          case '_22' :
            return '5'
            break
          case '_23':
            return '5'
            break
          case  '_36':
            return '5'
            break
          case  '_37':
            return '5'
            break
          case '周六'+"("+this.days[5]+")":
            return '6'
            break
          case  '周六'+"("+this.days[12]+")":
            return '6'
            break
          case  '周六'+"("+this.days[19]+")":
            return '6'
            break
          case  '_10' :
            return '6'
            break
          case  '_11':
            return '6'
            break
          case  '_24' :
            return '6'
            break
          case '_25' :
            return '6'
            break
          case  '_38' :
            return '6'
            break
          case  '_39':
            return '6'
            break

          case '周日' +"("+this.days[6]+")":
            return '7'
            break
          case  '周日'+"("+this.days[13]+")":
            return '7'
            break
          case '周日'+"("+this.days[20]+")":
            return '7'
            break
          case '_12' :
            return '7'
            break
          case '_13' :
            return '7'
            break
          case  '_26':
            return '7'
            break
          case '_27':
            return '7'
            break
          case '_40' :
            return '7'
            break
          case  '_41':
            return '7'
            break
          default:
            return '0'
            break

        }

      }, getRow(key) {
        switch (key) {
          case '周一'+"("+this.days[0]+")" : //本周一

            return 'C'
            break

          case  '': //本周一
            return 'D'
            break

          case  '_1': //本周一
            return 'E'
            break

          case '周一'+"("+this.days[7]+")": //下周一

            return 'X'

            break

          case '_14': //下周一

            return 'Y'
            break

          case   '_15': //下周一
            return 'Z'
            break

          case '周一'+"("+this.days[14]+")" : //下下周一

            return 'AS'
            break

          case  '_28': //下下周一

            return 'AT'
            break

          case '_29': //下下周一

            return 'AU'
            break

          case '周二'+"("+this.days[1]+")" : //本周二
            return 'F'
            break

          case  '_2': //本周二
            return 'G'
            break

          case '_3': //本周二
            return 'H'
            break

          case '周二'+"("+this.days[8]+")"://下周二
            return 'AA'

            break

          case  '_16' ://下周二
            return 'AB'

            break

          case  '_17'://下周二
            return 'AC'

            break

          case '周二'+"("+this.days[15]+")"://下下周二
            return 'AV'

            break

          case '_30' ://下下周二
            return 'AW'

            break

          case '_31'://下下周二
            return 'AX'
            break

          case '周三' +"("+this.days[2]+")": //本周三
            return 'I'

            break

          case  '_4': //本周三
            return 'J'
            break

          case '_5': //本周三
            return 'K'
            break

          case '周三'+"("+this.days[9]+")": //下周三
            return 'AD'

            break

          case  '_18' : //下周三
            return 'AE'

            break

          case '_19': //下周三
            return 'AF'

            break

          case '周三'+"("+this.days[16]+")" : //下下周三
            return 'AY'

            break

          case '_32' : //下下周三
            return 'AZ'

            break

          case  '_33': //下下周三
            return 'BA'

            break

          case '周四'+"("+this.days[3]+")":
            return 'L'

            break

          case '_6' :
            return 'M'

            break

          case  '_7':
            return 'N'

            break

          case '周四'+"("+this.days[10]+")":
            return 'AG'

            break

          case '_20' :
            return 'AH'

            break

          case  '_21':

            return 'AI'

            break

          case '周四'+"("+this.days[17]+")":

            return 'BB'

            break

          case '_34':

            return 'BC'

            break

          case  '_35':

            return 'BD'

            break
          case '周五'+"("+this.days[4]+")":
            return 'O'

            break

          case  '_8':
            return 'P'

            break

          case  '_9':
            return 'Q'
            break
          case '周五'+"("+this.days[11]+")":
            return 'AJ'
            break

          case '_22' :
            return 'AK'

            break

          case  '_23':
            return 'AL'

            break
          case '周五'+"("+this.days[18]+")":
            return 'BE'

            break

          case '_36':
            return 'BF'

            break

          case  '_37':
            return 'BG'

            break

          case '周六' +"("+this.days[5]+")" :
            return 'R'

            break

          case  '_10' :
            return 'S'

            break

          case '_11':
            return 'T'

            break
          case '周六'+"("+this.days[12]+")":
            return 'AM'

            break

          case  '_24' :
            return 'AN'

            break

          case  '_25':
            return 'AO'

            break

          case '周六'+"("+this.days[19]+")":
            return 'BH'

            break

          case '_38' :
            return 'BI'

            break

          case '_39':
            return 'BJ'

            break
          case '周日'+"("+this.days[6]+")" :
            return 'U'

            break

          case  '_12' :
            return 'V'

            break

          case  '_13':
            return 'W'

            break
          case '周日'+"("+this.days[13]+")":
            return 'AP'

            break

          case  '_26' :
            return 'AQ'

            break

          case  '_27':
            return 'AR'

            break
          case '周日'+"("+this.days[20]+")":
            return 'BK'

            break

          case '_40':
            return 'BL'

            break

          case '_41':
            return 'BM'
            break
        }

      }, getDateByResultsStr(key) {

        switch (key) {
          case '周一'+"("+this.days[0]+")" : //本周一

            return this.days[0]
            break

          case  '': //本周一
            return this.days[0]

            break

          case  '_1': //本周一
            return this.days[0]
            break

          case '周一'+"("+this.days[7]+")": //下周一
            return this.days[7]

            break

          case '_14': //下周一

            return this.days[7]

            break

          case   '_15': //下周一

            return this.days[7]

            break

          case '周一'+"("+this.days[14]+")": //下下周一

            return this.days[14]

            break

          case  '_28': //下下周一

            return this.days[14]

            break

          case '_29': //下下周一

            return this.days[14]

            break

          case '周二'+"("+this.days[1]+")": //本周二
            return this.days[1]

            break

          case  '_2': //本周二
            return this.days[1]

            break

          case '_3': //本周二
            return this.days[1]

            break

          case '周二'+"("+this.days[8]+")"://下周二
            return this.days[8]

            break

          case  '_16' ://下周二
            return this.days[8]

            break

          case  '_17'://下周二
            return this.days[8]

            break

          case '周二'+"("+this.days[15]+")"://下下周二
            return this.days[15]

            break

          case '_30' ://下下周二
            return this.days[15]

            break

          case '_31'://下下周二
            return this.days[15]

            break

          case '周三'+"("+this.days[2]+")" : //本周三
            return this.days[2]

            break

          case  '_4': //本周三
            return this.days[2]

            break

          case '_5': //本周三
            return this.days[2]

            break

          case '周三'+"("+this.days[9]+")": //下周三
            return this.days[9]

            break

          case  '_18' : //下周三
            return this.days[9]

            break

          case '_19': //下周三
            return this.days[9]

            break

          case '周三'+"("+this.days[16]+")": //下下周三
            return this.days[16]

            break

          case '_32' : //下下周三
            return this.days[16]

            break

          case  '_33': //下下周三
            return this.days[16]

            break

          case '周四'+"("+this.days[3]+")":
            return this.days[3]

            break

          case '_6' :
            return this.days[3]

            break

          case  '_7':
            return this.days[3]

            break

          case '周四'+"("+this.days[10]+")":

            return this.days[10]

            break

          case '_20' :

            return this.days[10]

            break

          case  '_21':

            return this.days[10]

            break

          case '周四'+"("+this.days[17]+")":

            return this.days[17]

            break

          case '_34':

            return this.days[17]

            break

          case  '_35':

            return this.days[17]

            break
          case '周五'+"("+this.days[4]+")":
            return this.days[4]

            break

          case  '_8':
            return this.days[4]

            break

          case  '_9':
            return this.days[4]

            break
          case '周五'+"("+this.days[11]+")" :
            return this.days[11]

            break

          case '_22' :
            return this.days[11]

            break

          case  '_23':
            return this.days[11]

            break
          case '周五'+"("+this.days[18]+")":
            return this.days[18]

            break

          case '_36':
            return this.days[18]

            break

          case  '_37':
            return this.days[18]

            break

          case '周六'+"("+this.days[5]+")":
            return this.days[5]

            break

          case  '_10' :
            return this.days[5]

            break

          case '_11':
            return this.days[5]

            break
          case '周六'+"("+this.days[12]+")":
            return this.days[12]

            break

          case  '_24' :
            return this.days[12]

            break

          case  '_25':
            return this.days[12]

            break

          case '周六'+"("+this.days[19]+")":
            return this.days[19]

            break

          case '_38' :
            return this.days[19]

            break

          case '_39':
            return this.days[19]

            break
          case '周日'+"("+this.days[6]+")" :
            return this.days[6]

            break

          case  '_12' :
            return this.days[6]

            break

          case  '_13':
            return this.days[6]
            break
          case '周日'+"("+this.days[13]+")":
            return this.days[13]

            break

          case  '_26' :
            return this.days[13]

            break

          case  '_27':
            return this.days[13]

            break
          case '周日'+"("+this.days[20]+")":
            return this.days[20]

            break

          case '_40':
            return this.days[20]

            break

          case '_41':
            return this.days[20]
            break
          default:
            return '0'
            break

        }

      }

    },
    created() {
      this.getWeekPanels()
      this.getInitDate()
    }
  }
</script>

<style rel="stylesheet/css" lang="scss">

  .schedule-table-cell-disable {
    background-color: #F5F5F5 !important;
  }
</style>

