<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-select v-model="Person" style="width:100px" placeholder="操作工" size="small" filterable clearable>
          <el-option
            v-for="item in Persons"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-input v-model="partNumber" size="small" placeholder="零件图号及名称" style="width: 200px" clearable/>
        <el-input v-model="procedureName" size="small" placeholder="工序名称" style="width: 100px" clearable/>
        <span>数量</span>
        <el-input-number v-model="number" :min="0" :step="1" size="small" placeholder="数量" style="width: 120px" clearable/>
        <span>单件工时</span>
        <el-input-number v-model="workTime" :min="0" :step="1" size="small" placeholder="单件工时" style="width: 120px" clearable/>
      </el-row>
      <el-row>
        <el-button type="warning" size="small" icon="el-icon-plus" plain style="margin-left: 10px" @click="saveDate">保存</el-button>
        <el-button type="primary" size="small" icon="el-icon-edit" plain style="margin-left: 10px" @click="editDate">编辑</el-button>
        <el-button type="danger" size="small" icon="el-icon-delete" plain style="margin-left: 10px" @click="deleteDate">删除</el-button>
        <el-date-picker
          v-model="monthValue"
          type="month"
          size="small"
          value-format="yyyy-MM"
          style="margin-left: 400px; width: 140px"
          placeholder="选择月"/>
        <el-button type="primary" size="small" icon="el-icon-search" plain style="margin-top: 10px;margin-bottom: 10px" @click="fetchDate">查询</el-button>
      </el-row>
    </el-card>
    <el-table v-loading="" :data="tableData" border stripe style="width: 100%" height="730px" size="mini" highlight-current-row @row-click="searchTable">
      <el-table-column label="加工日期" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.加工日期 ? scope.row.加工日期.split(' ')[0] : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作工" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.操作工姓名 }}
        </template>
      </el-table-column>
      <el-table-column label="零件图号及名称" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.零件图号 }}
        </template>
      </el-table-column>
      <el-table-column label="工序名称" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.工序 }}
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" width="70">
        <template slot-scope="scope">
          {{ scope.row.数量 }}
        </template>
      </el-table-column>
      <el-table-column label="单件工时" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.单件工时 }}
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.录入人姓名 }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { searchPerson, fetchDate, saveDate, editDate, deleteDate } from '@/api/ManufacturingCenter/Enter0therData.js'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      Person: '',
      Persons: [],
      partNumber: '',
      partName: '',
      procedureName: '',
      number: '',
      workTime: '',
      tableData: [],
      monthValue: new Date()
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'userId',
      'id' // 人员编号
    ])
  },
  created() {
    this.searchPerson()
    this.getNowFormatDate()
  },
  methods: {
    searchPerson() {
      searchPerson().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Persons.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    getNowFormatDate() {
      var year = this.monthValue.getFullYear()
      var month = this.monthValue.getMonth() + 1
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      this.monthValue = year + '-' + month
    },
    fetchDate() {
      if (this.monthValue) {
        fetchDate(this.monthValue).then(response => {
          this.tableData = response.data
        })
      } else {
        this.$message.warning('请选择月份进行查询')
      }
    },
    saveDate() {
      if (this.Person && this.partNumber && this.procedureName && this.number && this.workTime) {
        saveDate(this.Person, this.partNumber, this.procedureName, this.id, this.number, this.workTime).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('增加成功')
            this.RemoveDate()
            this.fetchDate()
          } else {
            this.$message.success('增加失败')
          }
        })
      } else {
        this.$message.warning('请完善信息')
      }
    },
    editDate() {
      if (this.Person && this.partNumber && this.procedureName && this.number && this.workTime) {
        editDate(this.ID, this.Person, this.partNumber, this.procedureName, this.id, this.number, this.workTime).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('编辑成功')
            this.RemoveDate()
            this.fetchDate()
          } else {
            this.$message.success('编辑失败')
          }
        })
      } else {
        this.$message.warning('请完善信息')
      }
    },
    deleteDate() {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDate(this.ID).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.RemoveDate()
            this.fetchDate()
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    RemoveDate() {
      this.Person = ''
      this.partNumber = ''
      this.number = 0
      this.workTime = 0
      this.procedureName = ''
    },
    searchTable(row) {
      this.Person = row.操作工流水号
      this.partNumber = row.零件图号
      this.procedureName = row.工序
      this.number = row.数量
      this.workTime = row.单件工时
      this.ID = row.ID
    }
  }
}
</script>
