<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="msgTipVisible"
      width="40%"
    >
      <span>如果导入成功,会将当前已有的排版会被全部清除,是否继续导入排版数据</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="msgTipVisible = false">取 消</el-button>
    <el-button type="primary" @click="export_file()">确 定</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">
        <!-- <el-col :span="3" style="text-align: right;"> -->
        <el-button v-loading="downloadLoading" v-if="template_mode&&(template_mode.mode == 1 || template_mode.mode == 2)"
                  size="small" icon="el-icon-setting" type="primary" @click="import_file()">导出模版
        </el-button>


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

        <!--<upload-excel style="float:right;" :on-success='handleSuccess' :before-upload="beforeUpload"></upload-excel>-->
        <schedule-template-upload-excel style="margin-right:10px;" :on-success='handleSuccess'
                                        :before-upload="beforeUpload"></schedule-template-upload-excel>


        <el-button size="small" icon="el-icon-setting" type="primary" @click="setScheduleTemplateAction">设置模板</el-button>
      </div>
      <!-- </el-col> -->
    </div>
    <div class="app-container">
      <!-- <el-row>
          <el-col :span="21"> -->
      <el-tabs v-model="activeName" tab-position="top">
        <el-tab-pane name="schedules">
          <span slot="label"> 排班信息 </span>
          <template-table :editable="false" :device_numbers="device_numbers" :template="this_week_schedules"
                          :patients="patients"></template-table>
        </el-tab-pane>
        <el-tab-pane name="first" :disabled="template_mode.mode == 0">
          <span slot="label"> 第一周 </span>
          <template-table :editable="true" :device_numbers="device_numbers" :template="first_template"
                          :patients="patients"></template-table>
        </el-tab-pane>

        <el-tab-pane name="second" :disabled="template_mode.mode != 2">
          <span slot="label"> 第二周 </span>
          <template-table :editable="true" :device_numbers="device_numbers" :template="second_template"
                          :patients="patients"></template-table>
        </el-tab-pane>
      </el-tabs>

      <set-up-template-dialog ref="setup_dialog" :template_mode="template_mode" :origin_mode="origin_mode"
                              @chenge_mode="didChangeTemplateMode"></set-up-template-dialog>
    </div>
  </div>
</template>

<script>
  import SetUpTemplateDialog from './components/setup_template_dialog'
  import TemplateTable from './components/template_table'
  import { getTemplateInitData } from '@/api/schedule_template/patient'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import { exportScheduleTemplate, getWeekPanels,initDate } from '@/api/schedule'
  import ScheduleTemplateUploadExcel from './components/scheduleTemplateUploadExcel/index'
  import { generateLog } from '@/api/config'

  export default {
    name: 'WorkforceTemplate',
    components: {
      ScheduleTemplateUploadExcel,
      SetUpTemplateDialog,
      TemplateTable,
      BreadCrumb
    },
    data: function() {
      return {
        crumbs: [
          { path: false, name: '排班管理' },
          { path: 'workforce/template', name: '排班模版' }
        ],
        partitions: {},
        scheduleZone: [],
        activeName: 'schedules',
        downloadLoading: false,
        isExistRepeatVisible:false,
        exportLogVisible:false,
        exportVisible:false,
        logs:[],
        generateLoading:false,

        days:[],
        msgTipVisible: false,
        dialogVisible: false,
        template_mode: {
          id: 0,
          mode: 0
        },
        origin_mode: 0,
        device_numbers: [],
        patients: [],

        first_template: {
          items: []
        },
        second_template: {
          items: []
        },
        this_week_schedules: {
          id: 0,
          items: []
        }
      }
    },
    created() {

      this.getWeekPanels()
      this.getInitDate()
      getTemplateInitData().then(rs => {
        var resp = rs.data
        if (resp.state == 1) {
          var mode = resp.data.template_mode
          var device_numbers = resp.data.device_numbers
          var templates = resp.data.templates
          var patients = resp.data.patients
          var schedules = resp.data.schedules

          this.template_mode = mode
          this.origin_mode = mode.mode
          this.device_numbers = device_numbers
          console.log(this.device_numbers)
          this.patients = patients

          this.first_template = templates[0]
          this.second_template = templates[1]

          var fakeTemplateItems = []
          for (let index = 0; index < schedules.length; index++) {
            const schedule = schedules[index]
            var item = {}
            this.$set(item, 'id', schedule.id)
            this.$set(item, 'template_id', 0)
            this.$set(item, 'device_number_id', schedule.bed_id)
            this.$set(item, 'treat_mode', schedule.mode_id)
            this.$set(item, 'weekday', schedule.schedule_week)
            this.$set(item, 'time_type', schedule.schedule_type)
            var patient = {}
            this.$set(patient, 'id', schedule.patient_id)
            this.$set(patient, 'name', schedule.patient)
            this.$set(item, 'patient', patient)

            fakeTemplateItems.push(item)
          }
          this.this_week_schedules.items = fakeTemplateItems

        } else {
          this.$message(resp.msg)
        }
      })
    },
    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() {
        this.generateLoading = true
        let params = {
          'log_type': 3
        }
        generateLog(params).then(
          response => {
            if (response.data.state === 1) {
              this.generateLoading = false

              this.logs = response.data.data.logs
              this.exportLogVisible = true
            } else {
              this.$message.error(response.data.msg)
              this.generateLoading = false

            }
          }
        ).catch(error => {
          this.generateLoading = false
        })

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

          this.days = response.data.data.days



        })
      },
      handleSuccess({ results, header }) {

        let headers = []
        for (let key in results[0]) {
          headers.push(key)
        }
        if (this.template_mode.mode == 1) {
          if(results.length > 0) {

            var isHas_1 = headers.includes('区号')
            console.log(isHas_1)
            var isHas_2 = headers.includes('机号')
            console.log(isHas_2)

            var isHas_3 = headers.includes('周一')
            console.log(isHas_3)

            var isHas_4 = headers.includes('')
            console.log(isHas_4)

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

            var isHas_6 = headers.includes('周二')
            console.log(isHas_6)

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

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

            var isHas_9 = headers.includes('周三')
            console.log(isHas_9)

            var isHas_10 = headers.includes('_4')
            console.log(isHas_1)

            var isHas_11 = headers.includes('_5')
            console.log(isHas_1)

            var isHas_12 = headers.includes('周四')
            console.log(isHas_1)

            var isHas_13 = headers.includes('_6')
            console.log(isHas_1)

            var isHas_14 = headers.includes('_7')
            console.log(isHas_1)

            var isHas_15 = headers.includes('周五')
            console.log(isHas_1)

            var isHas_16 = headers.includes('_8')
            console.log(isHas_1)

            var isHas_17 = headers.includes('_9')
            console.log(isHas_1)

            var isHas_18 = headers.includes('周六')
            console.log(isHas_1)

            var isHas_19 = headers.includes('_10')
            console.log(isHas_1)

            var isHas_20 = headers.includes('_11')
            console.log(isHas_1)

            var isHas_21 = headers.includes('周日')
            console.log(isHas_1)

            var isHas_22 = headers.includes('_12')
            console.log(isHas_1)

            var isHas_23 = headers.includes('_13')
            console.log(isHas_1)

            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 )) {

              this.dialogVisible = true
              return
            }
          }else{
            this.dialogVisible = true
            return
          }

        }else if(this.template_mode.mode == 2){
          if(results.length > 0) {

            var isHas_1 = headers.includes('区号')
            console.log(isHas_1)
            var isHas_2 = headers.includes('机号')
            console.log(isHas_2)

            var isHas_3 = headers.includes('周一')
            console.log(isHas_3)

            var isHas_4 = headers.includes('')
            console.log(isHas_4)

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

            var isHas_6 = headers.includes('周二')
            console.log(isHas_6)

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

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

            var isHas_9 = headers.includes('周三')
            console.log(isHas_9)

            var isHas_10 = headers.includes('_4')
            console.log(isHas_1)

            var isHas_11 = headers.includes('_5')
            console.log(isHas_1)

            var isHas_12 = headers.includes('周四')
            console.log(isHas_1)

            var isHas_13 = headers.includes('_6')
            console.log(isHas_1)

            var isHas_14 = headers.includes('_7')
            console.log(isHas_1)

            var isHas_15 = headers.includes('周五')
            console.log(isHas_1)

            var isHas_16 = headers.includes('_8')
            console.log(isHas_1)

            var isHas_17 = headers.includes('_9')
            console.log(isHas_1)

            var isHas_18 = headers.includes('周六')
            console.log(isHas_1)

            var isHas_19 = headers.includes('_10')
            console.log(isHas_1)

            var isHas_20 = headers.includes('_11')
            console.log(isHas_1)

            var isHas_21 = headers.includes('周日')
            console.log(isHas_1)

            var isHas_22 = headers.includes('_12')
            console.log(isHas_1)

            var isHas_23 = headers.includes('_13')
            console.log(isHas_1)

            var isHas_24 = headers.includes('周一_1')
            var isHas_25 = headers.includes('_14')
            var isHas_26 = headers.includes('_15')
            var isHas_27 = headers.includes('周二_1')
            var isHas_28 = headers.includes('_16')
            var isHas_29 = headers.includes('_17')
            var isHas_30 = headers.includes('周三_1')
            var isHas_31 = headers.includes('_18')
            var isHas_32 = headers.includes('_19')
            var isHas_33 = headers.includes('周四_1')
            var isHas_34 = headers.includes('_20')
            var isHas_35 = headers.includes('_21')
            var isHas_36 = headers.includes('周五_1')
            var isHas_37 = headers.includes('_22')
            var isHas_38 = headers.includes('_23')
            var isHas_39 = headers.includes('周六_1')
            var isHas_40 = headers.includes('_24')
            var isHas_41 = headers.includes('_25')
            var isHas_42 = headers.includes('周日_1')
            var isHas_43 = headers.includes('_26')
            var isHas_44 = headers.includes('_27')

            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)){
              this.dialogVisible = true
              return
            }
          }else{
            this.dialogVisible = true
            return
          }

        } else {
          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默认类型为数组，并且初始值为空的数组
        console.log("1111111111")
        console.log(deviceNumber)
        console.log("1111111111")

        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
          }
        }

        for (let i = 0; i < deviceNumber.length; i++) {
          if (!total_device_number.includes(deviceNumber[i])) {
            is_device_number = false
          }
        }

        //

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

        let 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['template_id'] = this.getTemplateByWeek(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)
              datas.push(tempObjTwo)
            }

          }

        }

        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) {
            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
            }
          }
        }

        let isHeader = true

        if(this.template_mode.mode == 1){
          if(headers.length > 23){
            isHeader = false
          }

        }else if(this.template_mode.mode == 2){
          if(headers.length > 44){
            isHeader = false
          }

        }

        if (!isHeader || !is_system_zone || !is_device_number) {
          this.dialogVisible = true
          return
        } else {
          if (is_exist_repeat) {
            this.isExistRepeatVisible = true
            return
          } else {

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

              }
            });

            let params = {
              schedule: 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()

            exportScheduleTemplate(params).then(response => {
              if (response.data.state == 0) {
                return false
              }
              this.getWeekPanels()
              getTemplateInitData().then(rs => {
                var resp = rs.data
                if (resp.state == 1) {
                  var mode = resp.data.template_mode
                  var device_numbers = resp.data.device_numbers
                  var templates = resp.data.templates
                  var patients = resp.data.patients
                  var schedules = resp.data.schedules

                  this.template_mode = mode
                  this.origin_mode = mode.mode
                  this.device_numbers = device_numbers
                  console.log(this.device_numbers)
                  this.patients = patients

                  this.first_template = templates[0]
                  this.second_template = templates[1]

                  var fakeTemplateItems = []
                  for (let index = 0; index < schedules.length; index++) {
                    const schedule = schedules[index]
                    var item = {}
                    this.$set(item, 'id', schedule.id)
                    this.$set(item, 'template_id', 0)
                    this.$set(item, 'device_number_id', schedule.bed_id)
                    this.$set(item, 'treat_mode', schedule.mode_id)
                    this.$set(item, 'weekday', schedule.schedule_week)
                    this.$set(item, 'time_type', schedule.schedule_type)
                    var patient = {}
                    this.$set(patient, 'id', schedule.patient_id)
                    this.$set(patient, 'name', schedule.patient)
                    this.$set(item, 'patient', patient)

                    fakeTemplateItems.push(item)
                  }
                  this.this_week_schedules.items = fakeTemplateItems

                } else {
                  this.$message(resp.msg)
                }
              })
            })
          }

        }

      }, getTemplateByWeek(key) {
        console.log(key)
        switch (key) {
          case '周一' : //本周一

            return this.first_template.id
            break

          case  '': //本周一
            return this.first_template.id

            break

          case  '_1': //本周一
            return this.first_template.id
            break

          case '周一_1': //下周一
            return this.second_template.id




            break

          case '_14': //下周一

            return this.second_template.id


            break

          case   '_15': //下周一

            return this.second_template.id


            break


          case '周二' : //本周二
            return this.first_template.id


            break

          case  '_2': //本周二
            return this.first_template.id


            break

          case '_3': //本周二
            return this.first_template.id


            break

          case '周二_1'://下周二
            return this.second_template.id


            break

          case  '_16' ://下周二
            return this.second_template.id



            break

          case  '_17'://下周二
            return this.second_template.id



            break


          case '周三' : //本周三
            return this.first_template.id


            break

          case  '_4': //本周三
            return this.first_template.id



            break

          case '_5': //本周三
            return this.first_template.id



            break

          case '周三_1': //下周三
            return this.second_template.id



            break

          case  '_18' : //下周三
            return this.second_template.id


            break

          case '_19': //下周三
            return this.second_template.id



            break



          case '周四':
            return this.first_template.id


            break

          case '_6' :
            return this.first_template.id



            break

          case  '_7':
            return this.first_template.id



            break

          case '周四_1':

            return this.second_template.id



            break

          case '_20' :

            return this.second_template.id


            break

          case  '_21':

            return this.second_template.id


            break


          case '周五':
            return this.first_template.id



            break

          case  '_8':
            return this.first_template.id



            break

          case  '_9':
            return this.first_template.id



            break
          case '周五_1' :
            return this.second_template.id



            break

          case '_22' :
            return this.second_template.id


            break

          case  '_23':
            return this.second_template.id



            break


          case '周六' :
            return this.first_template.id



            break

          case  '_10' :
            return this.first_template.id


            break

          case '_11':
            return this.first_template.id



            break
          case '周六_1' :
            return this.second_template.id



            break

          case  '_24' :
            return this.second_template.id



            break

          case  '_25':
            return this.second_template.id


            break


            break
          case '周日' :
            return this.first_template.id



            break

          case  '_12' :
            return this.first_template.id


            break

          case  '_13':
            return this.first_template.id

            break
          case '周日_1' :
            return this.second_template.id
            break

          case  '_26' :
            return this.second_template.id
            break

          case  '_27':
            return this.second_template.id
            break

        }

      },
      getWeekByResultsStr(key) {

        switch (key) {
          case '周一':
            return '1'
            break
          case  '周一_1':
            return '1'
            break

          case   '周一_2':
            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 '周二' :
            return '2'
            break
          case '周二_1':
            return '2'
            break
          case '周二_2' :
            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 '周三':
            return '3'
            break
          case '周三_1':
            return '3'
            break
          case  '周三_2':
            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 '周四':
            return '4'
            break

          case '周四_1' :
            return '4'
            break
          case'周四_2' :
            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 '周五' :
            return '5'
            break

          case  '周五_1':
            return '5'
            break
          case  '周五_2' :
            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 '周六':
            return '6'
            break
          case  '周六_1':
            return '6'
            break
          case  '周六_2' :
            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 '周日':
            return '7'
            break
          case  '周日_1' :
            return '7'
            break
          case '周日_2' :
            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 '周一' : //本周一

            return 'C'
            break

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

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

          case '周一_1': //下周一

            return 'X'

            break

          case '_14': //下周一

            return 'Y'
            break

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

          case '周一_2' : //下下周一

            return 'AS'
            break

          case  '_28': //下下周一

            return 'AT'
            break

          case '_29': //下下周一

            return 'AU'
            break

          case '周二' : //本周二
            return 'F'
            break

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

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

          case '周二_1'://下周二
            return 'AA'

            break

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

            break

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

            break

          case '周二_2' ://下下周二
            return 'AV'

            break

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

            break

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

          case '周三' : //本周三
            return 'I'

            break

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

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

          case '周三_1': //下周三
            return 'AD'

            break

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

            break

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

            break

          case '周三_2' : //下下周三
            return 'AY'

            break

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

            break

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

            break

          case '周四':
            return 'L'

            break

          case '_6' :
            return 'M'

            break

          case  '_7':
            return 'N'

            break

          case '周四_1':
            return 'AG'

            break

          case '_20' :
            return 'AH'

            break

          case  '_21':

            return 'AI'

            break

          case '周四_2' :

            return 'BB'

            break

          case '_34':

            return 'BC'

            break

          case  '_35':

            return 'BD'

            break
          case '周五':
            return 'O'

            break

          case  '_8':
            return 'P'

            break

          case  '_9':
            return 'Q'
            break
          case '周五_1' :
            return 'AJ'
            break

          case '_22' :
            return 'AK'

            break

          case  '_23':
            return 'AL'

            break
          case '周五_2' :
            return 'BE'

            break

          case '_36':
            return 'BF'

            break

          case  '_37':
            return 'BG'

            break

          case '周六' :
            return 'R'

            break

          case  '_10' :
            return 'S'

            break

          case '_11':
            return 'T'

            break
          case '周六_1' :
            return 'AM'

            break

          case  '_24' :
            return 'AN'

            break

          case  '_25':
            return 'AO'

            break

          case '周六_2' :
            return 'BH'

            break

          case '_38' :
            return 'BI'

            break

          case '_39':
            return 'BJ'

            break
          case '周日' :
            return 'U'

            break

          case  '_12' :
            return 'V'

            break

          case  '_13':
            return 'W'

            break
          case '周日_1' :
            return 'AP'

            break

          case  '_26' :
            return 'AQ'

            break

          case  '_27':
            return 'AR'

            break
          case '周日_2':
            return 'BK'

            break

          case '_40':
            return 'BL'

            break

          case '_41':
            return 'BM'
            break
        }

      },
      getDateByResultsStr(key) {

        switch (key) {
          case '周一' : //本周一

            return this.days[0]
            break

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

            break

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

          case '周一_1': //下周一
            return this.days[7]




            break

          case '_14': //下周一

            return this.days[7]


            break

          case   '_15': //下周一

            return this.days[7]


            break

          case '周一_2' : //下下周一

            return this.days[14]

            break

          case  '_28': //下下周一

            return this.days[14]


            break

          case '_29': //下下周一

            return this.days[14]


            break

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

            break

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

            break

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

            break

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

            break

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


            break

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


            break

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


            break

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

            break

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

            break

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

            break

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


            break

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


            break

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


            break

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


            break

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


            break

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


            break

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


            break

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

            break

          case '周四':
            return this.days[3]

            break

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


            break

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


            break

          case '周四_1':

            return this.days[10]


            break

          case '_20' :

            return this.days[10]

            break

          case  '_21':

            return this.days[10]

            break

          case '周四_2' :

            return this.days[17]


            break

          case '_34':

            return this.days[17]

            break

          case  '_35':

            return this.days[17]

            break
          case '周五':
            return this.days[4]


            break

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


            break

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


            break
          case '周五_1' :
            return this.days[11]


            break

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

            break

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


            break
          case '周五_2' :
            return this.days[18]


            break

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


            break

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

            break

          case '周六' :
            return this.days[5]


            break

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

            break

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


            break
          case '周六_1' :
            return this.days[12]


            break

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


            break

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

            break

          case '周六_2' :
            return this.days[19]


            break

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


            break

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


            break
          case '周日' :
            return this.days[6]


            break

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

            break

          case  '_13':
            return this.days[6]
            break
          case '周日_1' :
            return this.days[13]


            break

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


            break

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

            break
          case '周日_2':
            return this.days[20]

            break

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

            break

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

        }


      },
      getWeekPanels() {
        getWeekPanels(1).then(response => {
          if (response.data.state == 0) {
            return false
          }
          var partitions = response.data.data.partitions
          var that = this
          if (partitions.length > 0) {
            partitions.forEach(function(partition) {
              if (partition.jihaos.length == 0) {
                return false
              }
              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
                }
              })
            })
          }
        })
      },
      setScheduleTemplateAction: function() {
        this.$refs.setup_dialog.show()
      },
      didChangeTemplateMode: function(mode) {
        if (this.origin_mode != mode) {
          if (this.origin_mode == 0) { // 0 -> 1 或 0 -> 2
            // 不处理模板 item
          } else if (mode == 0) { // 1 -> 0 或 2 -> 0 清除所有模板的 item
            this.first_template.items.splice(0, this.first_template.items.length)
            this.second_template.items.splice(0, this.second_template.items.length)
            if (this.activeName != 'schedules') {
              this.activeName = 'schedules'
            }
          } else if (this.origin_mode == 1 && mode == 2) { // 1 -> 2
            // 不处理模板 item
          } else if (this.origin_mode == 2 && mode == 1) { // 2 -> 1 清除第二个模板的 item
            this.second_template.items.splice(0, this.second_template.items.length)
            if (this.activeName == 'second') {
              this.activeName = 'schedules'
            }
          }
          this.origin_mode = mode
        }
      }, formatJson(filterVal, jsonData) {
        return jsonData.map(v =>
          filterVal.map(j => {
            return v[j]

          })
        )
      }, import_file: function() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const filterVal = [
            '区号',
            '机号'
          ]
          const multiHeaderForModeOne = [['区号', '机号', '周一', '', '', '周二', '', '', '周三', '', '', '周四', '', '', '周五', '', '', '周六', '', '', '周日', '', '']]
          const multiHeaderForModeTwo = [['区号', '机号', '周一', '', '', '周二', '', '', '周三', '', '', '周四', '', '', '周五', '', '', '周六', '', '', '周日', '', '', '周一', '', '', '周二', '', '', '周三', '', '', '周四', '', '', '周五', '', '', '周六', '', '', '周日', '', '']]

          const headerForModeOne = ['', '', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上']
          const headerForModeTwo = ['', '', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上', '上午', '下午', '晚上']

          // 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']

          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++
          }
          const data = this.formatJson(filterVal, list)
          const filename = "排班模版"

          // excel.export_json_to_excel({
          //   multiHeaderForModeOne,
          //   headerForModeOne,
          //   merges,
          //   data
          // })

          if (this.template_mode.mode == 1) {
            let multiHeader = multiHeaderForModeOne
            let header = headerForModeOne

            excel.export_json_to_excel({
              multiHeader,
              header,
              merges,
              data,
              filename
            })

          } else if (this.template_mode.mode == 2) {
            let multiHeader = multiHeaderForModeTwo
            let header = headerForModeTwo

            excel.export_json_to_excel({
              multiHeader,
              header,
              merges,
              data,
              filename

            })

          }

          this.downloadLoading = false
        })

      }
    }
  }
</script>

<style scoped>

</style>
