<template>
  <div class="app-container">
    <el-card>
      <span>机床编号:</span>
      <el-select v-model="machineNumberValue" filterable clearable placeholder="机床编号" size="small">
        <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>
      <span>计划完成日期:</span>
      <el-date-picker
        v-model="startEndDate"
        style="width:250px"
        size="small"
        type="daterange"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"/>
      <el-button type="success" size="small" icon="el-icon-search" style="margin-left:10px" @click="total=0;pageCurrent=1;pageSize=10;searchTable()">查询</el-button>
      <el-button type="primary" size="small" icon="el-icon-circle-plus-outline" style="margin-left:10px" @click="goDown()">下达</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        size="small"
        min-height="450px"
        height="450px"
        highlight-current-row
        border
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          align="center"
          width="35"/>
        <el-table-column align="center" label="项目名称">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="机床编号">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="机床名称">
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="计划完成日期">
          <template slot-scope="scope">
            {{ scope.row.发货节点 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="客户名称">
          <template slot-scope="scope">
            {{ scope.row.客户名称 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
  </div>
</template>

<script>
import { initProject, searchTable, goDown } from '@/api/Assembly/AssemblyPlan'
export default {
  data() {
    return {
      projectValue: '',
      project: [],
      ProjectSerialNumber: [],
      machineNumberValue: '',
      machineNumber: [],
      MachineSerialNumber: [],
      ProcessStartTime: '',
      ProcessEndTime: '',
      TimeToGo: '',
      startEndDate: '',
      time: '',
      time1: '',
      check: 0,
      check1: 0,
      check2: 0,
      check3: 0,
      tableData: [],
      loading: false,
      result: 0,
      pageSize: 10,
      pageCurrent: 1,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            value: response.data[i].机床流水号,
            name: response.data[i].项目名称
          })
        }
      })
    },
    searchTable() {
      this.loading = true
      console.log(this.startEndDate)
      if (this.startEndDate === '' || this.startEndDate === null || this.startEndDate[0] === '' || this.startEndDate[1] === '') {
        this.check2 = 0
        this.startEndDate = ''
      } else { this.check2 = 1 }
      this.machineNumberValue ? this.check1 = 1 : this.check1 = 0
      searchTable(this.check1, this.machineNumberValue, this.check2, this.startEndDate[0], this.startEndDate[1], this.pageCurrent, this.pageSize).then(response => {
        for (let i = 0; i < response.data.rows.length; i++) {
          console.log(response.data.rows[i].发货节点)
          if (response.data.rows[i].发货节点 !== '') {
            response.data.rows[i].发货节点 = (response.data.rows[i].发货节点).split(' ')[0]
          }
        }
        this.tableData = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.ProjectSerialNumber = []
      this.MachineSerialNumber = []
      this.ProcessStartTime = []
      this.ProcessEndTime = []
      for (let i = 0; i < val.length; i++) {
        this.ProjectSerialNumber[i] = val[i].项目流水号
        this.MachineSerialNumber[i] = val[i].机床流水号
      }
    },
    getCurrentTime() {
      this.ProcessStartTime = this.getTime0(0)
      this.ProcessEndTime = this.getTime0(2)
      this.TimeToGo = this.getTime0(0)
    },
    getTime0(num) {
      var date0 = new Date()
      var date = new Date(date0.getTime() + num * 24 * 60 * 60 * 1000)
      var month = this.zeroFill(date.getMonth() + 1)
      var day = this.zeroFill(date.getDate())
      var hour = this.zeroFill(date.getHours())
      var minute = this.zeroFill(date.getMinutes())
      var second = this.zeroFill(date.getSeconds())
      return date.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
    },
    zeroFill(i) {
      if (i >= 0 && i <= 9) {
        return '0' + i
      } else {
        return i
      }
    },
    goDown() {
      this.getCurrentTime()
      if (this.ProjectSerialNumber.length !== 0) {
        this.result = 0
        for (let i = 0; i < this.ProjectSerialNumber.length; i++) {
          goDown(this.ProjectSerialNumber[i], this.MachineSerialNumber[i], this.ProcessStartTime, this.ProcessEndTime, this.TimeToGo).then(response => {
            this.result += parseInt(response.data[0].result)
            if (this.result === this.ProjectSerialNumber.length) {
              this.$message.success('下达成功')
              this.searchTable()
            } else {
              this.$message.error('下达失败')
            }
          })
        }
      } else {
        this.$message.warning('至少选择一条')
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  span{
    margin: 10px 0px 10px 10px;
  }
</style>
