<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <span style="margin-left: 10px">操作者:</span>
        <el-input v-model="workerName" placeholder="操作者" size="small" clearable style="width:200px"/>
        <span class="demonstration">完成加工时间:</span>
        <el-date-picker
          v-model="startTime"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 160px"
          type="date"
          placeholder="选择日期"/>
        <span class="demonstration">~</span>
        <el-date-picker
          v-model="endTime"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 160px"
          type="date"
          placeholder="选择日期"/>
        <el-button type="success" class="el-icon-search" size="small" style="margin: 10px 0 0 10px" @click="pageCurrent=1;total = 0;searchTable();">查询</el-button>
        <el-button type="primary" size="small" icon="el-icon-download" style="margin-left:10px" @click="exportUninquirySheet1()">导出</el-button>
      </el-row>
    </el-card>
    <el-card>
      <el-col :span="6">
        <el-table
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          height="380"
          size="mini"
          stripe
          style="width: 100%;"
          border
          element-loading-text="拼命加载中"
          @row-click="searchTable2">
          <el-table-column label="操作者" align="center" width="110px">
            <template slot-scope="scope">
              {{ scope.row.姓名 }}
            </template>
          </el-table-column>
          <el-table-column label="理论工时" align="center">
            <template slot-scope="scope">
              {{ scope.row.理论加工时间段 }}
            </template>
          </el-table-column>
          <el-table-column label="实际工时" align="center">
            <template slot-scope="scope">
              {{ scope.row.实际加工时间段 }}
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 10px">
          <el-pagination
            v-if="!loading"
            :current-page="pageCurrent"
            :page-size="pageSize"
            :total="total"
            layout="total, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </el-col>
      <el-col :span="17" style="margin-left: 30px;">
        <el-table v-loading="loading2" :data="tableData2" height="380" size="mini" stripe highlight-current-row style="width: 100%;" border element-loading-text="拼命加载中">
          <el-table-column label="零件图号" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.零件图号 }}
            </template>
          </el-table-column>
          <el-table-column label="零件名称" align="center" width="130px">
            <template slot-scope="scope">
              {{ scope.row.零件名称 }}
            </template>
          </el-table-column>
          <el-table-column label="工艺名称" align="center" width="120px">
            <template slot-scope="scope">
              {{ scope.row.工艺名称 }}
            </template>
          </el-table-column>
          <el-table-column label="完成数量" align="center" width="100px">
            <template slot-scope="scope">
              {{ scope.row.完成数量 }}
            </template>
          </el-table-column>
          <el-table-column label="理论工时" align="center" width="100px">
            <template slot-scope="scope">
              {{ scope.row.理论加工时间段 }}
            </template>
          </el-table-column>
          <el-table-column label="实际工时" align="center" width="100px">
            <template slot-scope="scope">
              {{ scope.row.实际加工时间段 }}
            </template>
          </el-table-column>
          <el-table-column label="完成时间" align="center">
            <template slot-scope="scope">
              {{ scope.row.加工完成时间 }}
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 10px">
          <el-pagination
            v-if="!loading2"
            :current-page="pageCurrent2"
            :page-size="pageSize2"
            :total="total2"
            layout="total, prev, pager, next, jumper"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"/>
        </div>
      </el-col>
    </el-card>
    <el-card>
      <span style="margin-left: 10px">机床编号:</span>
      <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" style="margin-top: 10px" clearable @change="searchTable4();searchTable6()">
        <el-option
          v-for="item in machineNumber"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
        </el-option>
      </el-select>
      <el-button type="primary" size="mini" icon="el-icon-download" style="margin-left:10px" @click="exportUninquirySheet2()">机床导出</el-button>
      <el-button type="primary" size="mini" icon="el-icon-download" style="margin-left:10px" @click="exportUninquirySheet3()">组件导出</el-button>
    </el-card>
    <el-card>
      <el-col :span="6">
        <el-table v-loading="loading3" :data="tableData3" stripe size="mini" highlight-current-row height="380" style="width: 100%;" border element-loading-text="拼命加载中" @row-click="searchTable5">
          <el-table-column label="机床编号" align="center" width="110px">
            <template slot-scope="scope">
              {{ scope.row.机床图号 }}
            </template>
          </el-table-column>
          <el-table-column label="理论工时" align="center">
            <template slot-scope="scope">
              {{ scope.row.理论加工时间段 }}
            </template>
          </el-table-column>
          <el-table-column label="实际工时" align="center">
            <template slot-scope="scope">
              {{ scope.row.实际加工时间段 }}
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 10px">
          <el-pagination
            v-if="!loading3"
            :current-page="pageCurrent3"
            :page-size="pageSize3"
            :total="total3"
            layout="total, prev, pager, next"
            @size-change="handleSizeChange3"
            @current-change="handleCurrentChange3"/>
        </div>
      </el-col>
      <el-col :span="17" style="margin-left: 30px;">
        <el-table v-loading="loading4" :data="tableData4" size="mini" height="380" stripe highlight-current-row style="width: 100%;" border element-loading-text="拼命加载中">
          <el-table-column label="机床编号" align="center">
            <template slot-scope="scope">
              {{ scope.row.机床图号 }}
            </template>
          </el-table-column>
          <el-table-column label="组号" align="center">
            <template slot-scope="scope">
              {{ scope.row.组号 }}
            </template>
          </el-table-column>
          <el-table-column label="理论工时" align="center">
            <template slot-scope="scope">
              {{ scope.row.理论加工时间段 }}
            </template>
          </el-table-column>
          <el-table-column label="实际工时" align="center" width="120px">
            <template slot-scope="scope">
              {{ scope.row.实际加工时间段 }}
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 10px">
          <el-pagination
            v-if="!loading4"
            :current-page="pageCurrent4"
            :page-size="pageSize4"
            :total="total4"
            layout="total, prev, pager, next, jumper"
            @size-change="handleSizeChange4"
            @current-change="handleCurrentChange4"/>
        </div>
      </el-col>
    </el-card>
  </div>
</template>
<script>
import { searchtable, searchtable2, searchtable4, searchtable5, initProject, searchtable55, searchtable11, searchtable44 } from '@/api/ManufacturingCenter/machiningHoursStatistics'

export default {
  data() {
    return {
      workerName: '',
      startTime: '',
      endTime: '',
      loading: false,
      tableData: [], // 表格数据
      total: null, // 总条数
      pageSize: 10, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      gongzuozhebianhao: '',
      machinenumber: '',
      personname: '',
      machineNumberValue: '',
      machineNumber: [],
      loading2: false,
      tableData2: [], // 表格数据
      total2: null, // 总条数
      pageSize2: 10, // 每页显示条数
      pageCurrent2: 1, // 后台获取页
      loading3: false,
      tableData3: [], // 表格数据
      total3: null, // 总条数
      pageSize3: 10, // 每页显示条数
      pageCurrent3: 1, // 后台获取页
      loading4: false,
      tableData4: [], // 表格数据
      total4: null, // 总条数
      pageSize4: 10, // 每页显示条数
      pageCurrent4: 1 // 后台获取页
    }
  },
  created() {
    this.initProject()
    this.searchTable()
    this.searchTable4()
  },
  methods: {
    // 导出
    exportUninquirySheet3() {
      if (this.machineNumberValue === null) {
        this.machineNumberValue = ''
      }
      searchtable55(this.machineNumberValue).then(res1 => {
        console.log(res1.data)
        if (res1.data.length === 0) {
          this.$message.error('请选择机床')
          return
        }
        import('./Export2Excel').then(excel => {
          const filterVal1 = ['机床图号', '组号', '理论加工时间段', '实际加工时间段'] // tableData1里的属性名
          const data1 = res1.data.map(v => filterVal1.map(j => v[j]))
          const dataGroup = []
          dataGroup.push(data1)
          const headerGroup = [['机床编号', '组号', '理论工时', '实际工时']]
          const sheetGroup = ['工时明细表']
          excel.export_json_to_excel({
            headerGroup: headerGroup,
            dataGroup: dataGroup,
            sheetGroup: sheetGroup,
            filename: res1.data[0]['机床图号'] + '_工时明细表',
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      })
    },
    // 导出
    exportUninquirySheet2() {
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check1 = 1 } else { this.check1 = 0 }
      searchtable44(0, this.machineNumberValue).then(res1 => {
        if (res1.data.length === 0) {
          this.$message.error('请选择机床')
          return
        }
        import('./Export2Excel').then(excel => {
          const filterVal1 = ['机床图号', '理论加工时间段', '实际加工时间段'] // tableData1里的属性名
          const data1 = res1.data.map(v => filterVal1.map(j => v[j]))
          const dataGroup = []
          dataGroup.push(data1)
          const headerGroup = [['机床编号', '理论工时', '实际工时']]
          const sheetGroup = ['工时明细表']
          excel.export_json_to_excel({
            headerGroup: headerGroup,
            dataGroup: dataGroup,
            sheetGroup: sheetGroup,
            filename: '机床工时明细表',
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      })
    },
    // 导出
    exportUninquirySheet1() {
      if (this.startTime !== '' && this.startTime !== null && this.endTime !== null && this.endTime !== null) {
        this.timeCheck = 1
      } else {
        this.startTime = ''
        this.endTime = ''
        this.timeCheck = 0
      }
      if (this.workerName !== '' && this.workerName !== null) {
        this.workerName_ckeck = 1
      } else {
        this.workerName_ckeck = 0
      }
      searchtable11(this.workerName_ckeck, this.workerName, this.timeCheck, this.startTime, this.endTime).then(res1 => {
        if (res1.data.length === 0) {
          this.$message.error('请选择人员')
          return
        }
        import('./Export2Excel').then(excel => {
          const filterVal1 = ['姓名', '理论加工时间段', '实际加工时间段'] // tableData1里的属性名
          const data1 = res1.data.map(v => filterVal1.map(j => v[j]))
          const dataGroup = []
          dataGroup.push(data1)
          const headerGroup = [['操作者', '理论工时', '实际工时']]
          const sheetGroup = ['人员工时明细表']
          excel.export_json_to_excel({
            headerGroup: headerGroup,
            dataGroup: dataGroup,
            sheetGroup: sheetGroup,
            filename: '工时明细表',
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      })
    },
    initProject() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    // 查询表格数据
    searchTable() {
      this.loading = true
      this.tableData = []
      this.tableData2 = []
      this.total2 = 0
      if (this.startTime !== '' && this.startTime !== null && this.endTime !== null && this.endTime !== null) {
        this.timeCheck = 1
      } else {
        this.startTime = ''
        this.endTime = ''
        this.timeCheck = 0
      }
      if (this.workerName !== '' && this.workerName !== null) {
        this.workerName_ckeck = 1
      } else {
        this.workerName_ckeck = 0
      }
      searchtable(this.workerName_ckeck, this.workerName, this.timeCheck, this.startTime, this.endTime, this.pageCurrent, this.pageSize).then(response => {
        if (response.data.total === 0) {
          this.loading = false
        } else {
          this.total = response.data.total
          this.tableData = response.data.rows
        }
      }).then(() => {
        this.loading = false
      })
    },
    searchTable2(row) {
      this.loading2 = true
      this.tableData2 = []
      this.pageCurrent2 = 1
      this.gongzuozhebianhao = row.人员编号
      this.personname = row.姓名
      searchtable2(row.人员编号, this.timeCheck, this.startTime, this.endTime, this.pageCurrent2, this.pageSize2).then(response => {
        if (response.data.total === 0) {
          this.loading2 = false
        } else {
          this.total2 = response.data.total
          this.tableData2 = response.data.rows
        }
      }).then(() => {
        this.loading2 = false
      })
    },
    searchTable3(row) {
      this.loading2 = true
      this.tableData2 = []
      searchtable2(this.gongzuozhebianhao, this.timeCheck, this.startTime, this.endTime, this.pageCurrent2, this.pageSize2).then(response => {
        if (response.data.total === 0) {
          this.loading2 = false
        } else {
          this.total2 = response.data.total
          this.tableData2 = response.data.rows
        }
      }).then(() => {
        this.loading2 = false
      })
    },
    // 查询表格数据
    searchTable4() {
      this.loading3 = true
      this.tableData3 = []
      this.tableData4 = []
      this.total3 = 0
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check1 = 1 } else { this.check1 = 0 }
      searchtable4(this.check1, this.machineNumberValue, this.pageCurrent, this.pageSize).then(response => {
        if (response.data.total === 0) {
          this.loading3 = false
        } else {
          this.total3 = response.data.total
          this.tableData3 = response.data.rows
        }
      }).then(() => {
        this.loading3 = false
      })
    },
    searchTable5(row) {
      this.loading4 = true
      this.tableData4 = []
      this.pageCurrent4 = 1
      this.machineNumberValue = parseInt(row.机床流水号)
      searchtable5(this.machineNumberValue, this.pageCurrent4, this.pageSize4).then(response => {
        if (response.data.total === 0) {
          this.loading4 = false
        } else {
          this.total4 = response.data.total
          this.tableData4 = response.data.rows
        }
      }).then(() => {
        this.loading4 = false
      })
    },
    searchTable6(row) {
      this.loading4 = true
      this.tableData4 = []
      searchtable5(this.machineNumberValue, this.pageCurrent4, this.pageSize4).then(response => {
        if (response.data.total === 0) {
          this.loading4 = false
        } else {
          this.total4 = response.data.total
          this.tableData4 = response.data.rows
        }
      }).then(() => {
        this.loading4 = false
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    },
    handleSizeChange2(val) {
      this.pageCurrent2 = 1
      this.pageSize2 = val
      this.searchTable3()
    },
    handleCurrentChange2(val) {
      this.pageCurrent2 = val
      this.searchTable3()
    },
    // 分页
    handleSizeChange3(val) {
      this.pageCurrent3 = 1
      this.pageSize3 = val
      this.searchTable4()
    },
    handleCurrentChange3(val) {
      this.pageCurrent3 = val
      this.searchTable4()
    },
    handleSizeChange4(val) {
      this.pageCurrent4 = 1
      this.pageSize4 = val
      this.searchTable6()
    },
    handleCurrentChange4(val) {
      this.pageCurrent4 = val
      this.searchTable6()
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 1px;
  }
</style>
