<template>
  <div class="routineCheckDiv">
    <el-card class="routineCheckCard">
      <div class="textDiv">
        <el-select v-model="MachineValue" class="selectStation" filterable clearable size="small" placeholder="选择工位" @change="searchData">
          <el-option
            v-for="item in Machine"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span class="spanDatetime">查询时间</span>
        <el-date-picker
          v-model="dateRange1"
          size="small"
          type="year"
          align="center"
          value-format="yyyy-MM-dd"
          format="yyyy"
          placeholder="选择年"
          @change="searchData"/>
        <el-button class="buttonSeach" type="primary" plain size="small" icon="el-icon-search" @click="searchData()">查询</el-button>
        <el-button type="success" size="small" icon="el-icon-download" plain style="margin-left: 10px" @click="exportExcel()">导出</el-button>
      </div>
      <el-table :data="tableData" class="tableDate" min-height="100px" highlight-current-row border>
        <el-table-column label="项次" align="center" type="index" width="50px"/>
        <el-table-column label="周保养项目" width="150px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.周保养项目 }}
          </template>
        </el-table-column>
        <el-table-column v-for="(item,index) of calenderTableHeader" :key="item" :label="item" :prop="prop[index]" width="80px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row[ 'F' + item] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dateRange1: '', // 日期
      MachineValue: '', // 工位号
      Machine: [], // 工位号数组
      tableData: [], // 数据
      calenderTableHeader: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15',
        '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34',
        '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52'],
      prop: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
    }
  },
  // computed: {
  //   ...mapGetters([
  //     'sidebar',
  //     'avatar',
  //     'name',
  //     'id'
  //   ])
  // },
  created() {
    // this.searchDatetime()
  },
  mounted() {
    this.fetchData()
    this.dateRange1 = new Date() // 默认显示为当天时间
  },
  methods: {
    // 查询工位信息
    fetchData() {
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Machine.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 查询数据
    searchData() {
      let MachineValue_check, dateRange_check
      this.MachineValue ? MachineValue_check = 1 : MachineValue_check = 0
      this.dateRange ? dateRange_check = 1 : (dateRange_check = 0, this.dateRange = '')
      var param = []
      param[0] = ['年份_check', dateRange_check]
      param[1] = ['年份', this.dateRange1]
      param[2] = ['工位号', this.MachineValue]
      param[3] = ['工位号_check', MachineValue_check]
      var Data = this.CreateData('11', '点检日期周_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data
        console.log(response.data, '周')
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        let MachineValue_check, dateRange_check
        this.MachineValue ? MachineValue_check = 1 : MachineValue_check = 0
        this.dateRange ? dateRange_check = 1 : (dateRange_check = 0, this.dateRange = '')
        var param = []
        param[0] = ['月份_check', dateRange_check]
        param[1] = ['月份', this.dateRange]
        param[2] = ['工位号', this.MachineValue]
        param[3] = ['工位号_check', MachineValue_check]
        var Data = this.CreateData('2001', '点检日期周导出_查询数据', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    // searchDatetime() {
    //   this.calenderTableHeader = []
    //   var param = []
    //   var Data = this.CreateData('11', '点检日期_查询数据_表头', param)
    //   this.ExecDatabase(Data).then(response => {
    //     this.calenderTableHeader.push({
    //       日期: '日保养项目'
    //     })
    //     for (let i = 0; i < response.data[0].日期; i++) {
    //       this.calenderTableHeader.push({
    //         日期: (i + 1).toString()
    //       })
    //     }
    //     this.searchData()
    //   })
    // },
    // 设置列宽
    columnWidth(item) {
      let widthStr = ''
      switch (item) {
        case '日保养项目':
          widthStr = '150'
          break
        default:
          widthStr = '80'
      }
      return widthStr
    }
  }
}
</script>

<style scoped>
  .routineCheckCard{
    width: 100%;
    height: 840px;
  }

  .selectStation{
    width:160px;
  }

  .optionDiv1{
    float: left
  }

  .optionDiv2{
    float: right; color: #8492a6; font-size: 13px
  }

  .buttonSeach{
    margin-left: 15px;
  }

  .spanDatetime{
    font-size: 13px;
    color: black;
  }

  .textDiv{
    margin-top: 7px;
    margin-bottom: 7px;
    height: 100%;
  }

  .tableDate{
    width: 100%;
  }

  /deep/ .tableDate{
    border: 1px solid black!important;
  }

  /deep/ .tableDate td{
    border: 1px solid black!important;
    border-left: 0px!important;
    border-top: 0px!important;
  }
  /deep/ .tableDate th.is-leaf{
    border: 1px solid black!important;
    border-top: 0px!important;
    border-left: 0px!important;
  }

</style>
