<!--
 * @Author: gtz
 * @Date: 2022-03-25 11:29:17
 * @LastEditors: fzq
 * @LastEditTime: 2022-09-08 14:08:26
 * @Description: file content
 * @FilePath: \mt-bus-fe\src\views\FactoryManage\TeamInfo.vue
-->
<template>
  <div class="app-container">
    <head-form :form-config="headFormConfig" @headBtnClick="btnClick" />
    <base-table
      :top-btn-config="topBtnConfig"
      :page="listQuery.current"
      :limit="listQuery.size"
      :table-config="tableProps"
      :table-data="list"
      :is-loading="listLoading"
      @clickTopBtn="clickTopBtn"
    >
      <method-btn slot="handleBtn" :width="trueWidth" :method-list="tableBtn" @clickBtn="handleClick" />
    </base-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList()"
    />
    <team-info-detail v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
    <team-info-add
      v-if="addVisible"
      ref="addNew"
      :team-list="teamList"
      :shift-list="shiftList"
      @refreshDataList="getList"
    />
  </div>
</template>

<script>
import HeadForm from '@/components/basicData/HeadForm'
import { list, exportFile, getShiftList, getTeamList } from '@/api/team-manage/teamInfo'
import TeamInfoAdd from './components/TeamInfo-add.vue'
import TeamInfoDetail from './components/TeamInfo-detail.vue'
import BaseTable from '@/components/BaseTable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import { timeFormatter } from '@/filters'
import i18n from '@/lang'
// import data from '../pdf/content'
import { Random } from 'mockjs'

/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */
const topBtnConfig = [
  {
    type: 'add',
    btnName: 'btn.add'
  }
]
const tableBtn = [
  {
    type: 'detail',
    btnName: 'btn.detail'
  }
]
const tableProps = [
  // {
  //   prop: 'planHandoverTime',
  //   label: i18n.t('module.teamManager.teamInfo.planHandoverTime'),
  //   filter: timeFormatter,
  //   align: 'center'
  // },
  {
    prop: 'className',
    label: i18n.t('module.teamManager.teamInfo.className')
  },
  {
    prop: 'classCode',
    label: i18n.t('module.teamManager.teamInfo.classCode')
  },
  {
    prop: 'planTeamName',
    label: i18n.t('module.teamManager.teamInfo.planTeamName')
  },
  {
    prop: 'planPreTeamName',
    label: i18n.t('module.teamManager.teamInfo.planPreTeamName')
  },
  {
    prop: 'planHandoverTime',
    label: i18n.t('module.teamManager.teamInfo.onlineTime'),
    filter: timeFormatter
  },
  {
    prop: 'planOfflineTime',
    label: i18n.t('module.teamManager.teamInfo.offlineTime'),
    filter: timeFormatter
  }
  // {
  //   prop: 'workStatus',
  //   label: i18n.t('module.teamManager.teamInfo.workStatus'),
  //   align: 'center',
  //   filter: dataDict('workStatus')
  // }
]

export default {
  name: 'Factory',
  components: { Pagination, BaseTable, MethodBtn, HeadForm, TeamInfoDetail, TeamInfoAdd },
  data() {
    return {
      topBtnConfig,
      addOrUpdateVisible: false,
      addVisible: false,
      tableBtn,
      trueWidth: 80,
      tableProps,
      list: [],
      // 初始化报警级别
      alarmOption: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        planTeamId: null,
        planHandoverBegTime: null,
        planHandoverEndTime: null
      },
      workerList: {},
      teamList: [],
      shiftList: [],
      headFormConfig: [
        {
          type: 'select',
          label: this.$t('module.teamManager.teamInfo.planTeamName'),
          selectOptions: [],
          param: 'planTeamId',
          defaultSelect: ''
        },
        {
          type: 'datePicker',
          label: this.$t('module.teamManager.teamInfo.planHandoverTime'),
          dateType: 'daterange',
          rangeSeparator: '-',
          startPlaceholder: this.$t('module.orderManage.order.StartTime'),
          endPlaceholder: this.$t('module.orderManage.order.EndTime'),
          param: 'timeSlot'
        },
        {
          type: 'button',
          btnName: 'btn.search',
          name: 'search',
          color: 'primary'
        },
        {
          type: 'button',
          btnName: 'btn.export',
          name: 'export',
          color: 'success'
        }
      ],
      headFormValue: {}
    }
  },
  created() {
    this.getDict()
    this.getList()
  },
  methods: {
    handleClick(raw) {
      if (raw.type === 'detail') {
        this.addNew(raw.data.id)
      }
    },
    getDict() {
      const params = {
        current: 1,
        size: 999
      }
      getTeamList(params).then(res => {
        this.teamList = res.data.records
        this.headFormConfig[0].selectOptions = res.data.records
      })
      getShiftList(params).then(res => {
        this.shiftList = res.data.records
      })
    },
    getList() {
      this.listQuery.planTeamId = this.headFormValue.planTeamId
      this.listQuery.planHandoverBegTime = this.headFormValue.timeSlot ? this.headFormValue.timeSlot[0] : ''
      this.listQuery.planHandoverEndTime = this.headFormValue.timeSlot ? this.headFormValue.timeSlot[1] : ''
      this.$nextTick(() => {
        this.listLoading = true
        list(this.listQuery).then(response => {
          if (response.data.records) {
            // this.list = response.data.records
            // 11-mes 数据模拟修改

            this.list = response.data.records.map(item => {
              let startDate = 2022 + '-' + '11-' + Random.integer(1, 29)
              let endDate = startDate.split('-')
              endDate[2] = +endDate[2] + Random.integer(0, 3)

              let startHour = Random.integer(1, 22)

              return {
                ...item,
                planHandoverTime:
                  startDate + ' ' + (startHour < 10 ? '0' + startHour : '' + startHour) + Random.datetime(':mm:ss'),
                planOfflineTime:
                  endDate.join('-') +
                  ' ' +
                  (startHour + 1 < 10 ? +'0' + (startHour + 1) : 1 + startHour + '') +
                  Random.datetime(':mm:ss')
              }
            })

            console.log("list;', ", this.list)
          } else {
            this.list.splice(0, this.list.length)
          }
          this.total = response.data.total
          this.listLoading = false
        })
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    addNewInfo() {
      this.addVisible = true
      console.log(1)
      this.$nextTick(() => {
        console.log(2)
        this.$refs.addNew.init()
      })
    },
    exportExcel() {
      const params = {
        current: 1,
        size: 999,
        planTeamId: this.headFormValue.planTeamId,
        planHandoverBegTime: this.headFormValue.timeSlot ? this.headFormValue.timeSlot[0] : null,
        planHandoverEndTime: this.headFormValue.timeSlot ? this.headFormValue.timeSlot[1] : null
      }
      this.$nextTick(() => {
        exportFile(params).then(response => {
          console.log(response)
          let fileName = ''
          const contentDisposition = response.headers['content-disposition']
          if (contentDisposition) {
            fileName = contentDisposition.slice(contentDisposition.indexOf('filename=') + 9)
          }
          const blob = new Blob([response.data])
          const reader = new FileReader()
          reader.readAsDataURL(blob)
          reader.onload = e => {
            const a = document.createElement('a')
            a.download = fileName
            a.href = e.target.result
            document.body.appendChild(a)
            a.click()
            document.body.removeChild(a)
          }
        })
      })
    },
    btnClick(val) {
      this.headFormValue = val
      // 如果点击的是搜索栏的其他按钮在这里继续写判断
      if (this.headFormValue.btnName === 'search') {
        this.getList()
      } else if (this.headFormValue.btnName === 'export') {
        this.exportExcel()
      }
    },
    clickTopBtn(val) {
      if (val === 'add') {
        this.addNewInfo() // 新增
      }
    }
  }
}
</script>
