<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <span class="demonstration">开始加工时间:</span>
        <el-date-picker
          v-model="startTime1"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 160px;margin-top: 10px"
          type="date"
          placeholder="选择日期"/>
        <span class="demonstration">~</span>
        <el-date-picker
          v-model="endTime1"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 160px"
          type="date"
          placeholder="选择日期"/>
        <span class="demonstration">完成加工时间:</span>
        <el-date-picker
          v-model="startTime2"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 160px"
          type="date"
          placeholder="选择日期"/>
        <span class="demonstration">~</span>
        <el-date-picker
          v-model="endTime2"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 160px"
          type="date"
          placeholder="选择日期"/>
      </el-row>
      <el-row>
        <span>操作者:</span>
        <el-input v-model="workerName" placeholder="操作者" size="small" clearable style="width:200px; margin-top: 10px;margin-bottom: 10px"/>
        <span>零件图号:</span>
        <el-input v-model="partNumber" placeholder="零件图号" size="small" clearable style="width:200px"/>
        <span>工艺名称:</span>
        <el-input v-model="technologyName" placeholder="工艺名称" size="small" clearable style="width:200px"/>
        <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" class="el-icon-plus" size="small" style="margin: 10px 0 0 10px" @click="addSupplement();">新增</el-button>
      </el-row>
    </el-card>

    <el-card>
      <el-table
        :data="tableData"
        height="440"
        stripe="true"
        size="mini"
        highlight-current-row
        style="width: 100%;"
        border>
        <el-table-column label="序号" type="index" align="center" width="55"/>
        <el-table-column label="零件图号" align="center" min-width="150px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="完成数量" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.完成数量 }}
          </template>
        </el-table-column>
        <el-table-column label="工艺名称" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.工艺名称 }}
          </template>
        </el-table-column>
        <el-table-column label="操作者" align="center" min-width="80px">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="实际开始时间" align="center" min-width="160px">
          <template slot-scope="scope">
            {{ scope.row.加工开始时间 || '—' }}
          </template>
        </el-table-column>
        <el-table-column label="实际完成时间" align="center" min-width="160px">
          <template slot-scope="scope">
            {{ scope.row.加工完成时间 || '—' }}
          </template>
        </el-table-column>
        <el-table-column label="实际工时" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.实际加工时间段 || '—' }}
          </template>
        </el-table-column>
        <el-table-column label="修补数量" align="center" min-width="100px">
          <template slot-scope="scope">
            <template v-if="scope.row.isEditing">
              <el-input v-model="scope.row.数量" class="edit-input" size="mini"/>
            </template>
            <span v-else>{{ scope.row.数量 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修补工时" align="center" min-width="100px">
          <template slot-scope="scope">
            <template v-if="scope.row.isEditing">
              <el-input v-model="scope.row.修补工时" class="edit-input" size="mini"/>
            </template>
            <span v-else>{{ scope.row.修补工时 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修补操作时间" align="center" min-width="160px">
          <template slot-scope="scope">
            {{ scope.row.修补操作时间 || '—' }}
          </template>
        </el-table-column>
        <el-table-column label="修补人" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.修补人 || '—' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="300" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row.isEditing" type="mini" plain size="mini" icon="el-icon-circle-check-outline" @click="confirmEdit(scope.row)">确定</el-button>
            <el-button v-if="scope.row.isEditing" class="cancel-btn" plain size="mini" type="warning" @click="cancelEdit(scope.row)">取消</el-button>
            <el-button
              v-else
              :disabled="!(scope.row.加工开始时间 || scope.row.加工完成时间)"
              size="mini"
              plain
              type="primary"
              class="el-icon-edit"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              :disabled="!!(scope.row.加工开始时间 || scope.row.加工完成时间)"
              size="mini"
              plain
              type="danger"
              class="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="650px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="80px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目" prop="project">
              <el-select v-model="form.project" filterable size="small" @change="getMachine">
                <el-option
                  v-for="item in projects"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机床" prop="machine">
              <el-select v-model="form.machine" filterable size="small" @change="getGroup">
                <el-option
                  v-for="item in machines"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分组" prop="group">
              <el-select v-model="form.group" filterable size="small" @change="getParts">
                <el-option
                  v-for="item in groups"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="零件" prop="part">
              <el-select v-model="form.part" filterable size="small" @change="getTechnology">
                <el-option
                  v-for="item in parts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工艺" prop="technology">
              <el-select v-model="form.technology" size="small">
                <el-option
                  v-for="item in technologys"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修补工时" prop="timeNumber">
              <el-input v-model="form.timeNumber" placeholder="修补工时" clearable size="small" style="width: 200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量" prop="number">
              <el-input v-model="form.number" placeholder="数量" clearable size="small" style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作工" prop="opNum">
              <el-select v-model="form.opNum" size="small" filterable>
                <el-option
                  v-for="item in opNums"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取消</el-button>
        <el-button type="primary" @click="submitAdd()" >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchTable, editTime, initProject, getMachines, getGroups, getParts, submitAdd } from '@/api/ManufacturingCenter/ActualTimeSupplement'
import { mapGetters } from 'vuex'
import request from '@/utils/request'

export default {
  name: 'ActualTimeSupplement',
  data() {
    return {
      workerName: '',
      partNumber: '',
      technologyName: '',
      startTime1: '',
      endTime1: '',
      startTime2: '',
      endTime2: '',
      pageCurrent: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      dialogFormVisible: false,
      title: '',
      form: {
        project: '',
        machine: '',
        group: '',
        part: '',
        technology: '',
        timeNumber: '',
        number: '',
        opNum: ''
      },
      rules: {
        part: [{ required: true, message: '请选择零件' }],
        technology: [{ required: true, message: '请选择工艺' }],
        timeNumber: [{ required: true, message: '请填写修补工时' }],
        number: [{ required: true, message: '请填写数量' }],
        opNum: [{ required: true, message: '请选择操作工' }]
      },
      projects: [],
      machines: [],
      groups: [],
      parts: [],
      technologys: [],
      opNums: []
    }
  },
  computed: {
    ...mapGetters([
      'id' // 人员编号
    ])
  },
  created() {
    this.initData()
    this.searchTable()
  },
  methods: {
    // 新增修补工时
    addSupplement() {
      this.dialogFormVisible = true
    },
    // 提交新增
    submitAdd() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          submitAdd(this.form.technology, this.form.timeNumber, this.form.number, this.id).then(res => {
            if (res.data[0].result === '1') {
              this.dialogFormVisible = false
              this.$message.success('操作成功')
              this.searchTable()
            } else {
              this.$message.error('操作失败')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 提交编辑
    confirmEdit(row) {
      editTime(this.id, row.完成过程流水号, row.修补工时, row.数量).then(response => {
        if (response.data[0].result === '1') {
          row.isEditing = false
          this.$message.success('修改成功')
          this.searchTable()
        } else {
          this.$message.error('修改失败')
        }
      })
    },
    // 取消编辑
    cancelEdit(row) {
      row.修补工时 = row.原修补工时
      row.isEditing = false
      this.$message('取消修改')
    },
    // 初始化数据
    initData() {
      this.projects = []
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.projects.push({
            label: response.data[i].项目名称,
            value: response.data[i].项目流水号
          })
        }
      })
      this.opNums = []
      request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select *
                from 人事档案管理_人员名单
                where 考核部门编号 = 12`
        }
      }).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.opNums.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    // 查机床
    getMachine() {
      this.form.machine = ''
      this.machines = []
      this.form.group = ''
      this.groups = []
      this.form.part = ''
      this.parts = []
      this.form.technology = ''
      this.technologys = []
      if (this.form.project !== '') {
        getMachines(this.form.project).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号
            })
          }
        })
      }
    },
    // 查分组
    getGroup() {
      this.form.group = ''
      this.groups = []
      this.form.part = ''
      this.parts = []
      this.form.technology = ''
      this.technologys = []
      getGroups(this.form.machine).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groups.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    // 查零件
    getParts() {
      this.form.part = ''
      this.parts = []
      this.form.technology = ''
      this.technologys = []
      let check1, check2, check3
      this.form.project ? check1 = 1 : check1 = 0
      this.form.machine ? check2 = 1 : check2 = 0
      this.form.group ? check3 = 1 : check3 = 0
      this.parts = []
      getParts(check1, this.form.project, check2, this.form.machine, check3, this.form.group).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.parts.push({
            label: response.data[i].零件图号,
            value: response.data[i].工艺计划流水号
          })
        }
      })
    },
    // 查工艺
    getTechnology() {
      this.form.technology = ''
      this.technologys = []
      request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select 工艺计划流水号,车间生产管理工艺_基础表_工艺名称.工艺编号,工艺名称,工序流水号
                from 车间生产管理工艺_零件工序 inner join 车间生产管理工艺_基础表_工艺名称
                on 车间生产管理工艺_零件工序.工艺编号 = 车间生产管理工艺_基础表_工艺名称.工艺编号
                where 工艺计划流水号 = ${this.form.part}`
        }
      }).then(res => {
        for (let i = 0; i < res.data.length; i++) {
          this.technologys.push({
            label: res.data[i].工艺名称,
            value: res.data[i].工序流水号
          })
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row.修补工时流水号)
        request({
          url: '',
          method: 'post',
          data: {
            type: '4',
            name: `delete from 车间生产管理_修补工时
                where 修补工时流水号 = ${row.修补工时流水号}`
          }
        }).then(res => {
          if (res.data[0].result === '1') {
            this.searchTable()
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    // 编辑
    handleEdit(row) {
      row.isEditing = !row.isEditing
    },
    // 查询加工工时统计表
    searchTable() {
      let check1, check2, check3, check4, check5
      this.technologyName ? check1 = 1 : check1 = 0
      this.workerName ? check2 = 1 : check2 = 0
      this.startTime1 && this.endTime1 ? check3 = 1 : check3 = 0
      this.partNumber ? check4 = 1 : check4 = 0
      this.startTime2 && this.endTime2 ? check5 = 1 : check5 = 0
      searchTable(this.technologyName, check1, this.workerName, check2, check3, this.endTime1, this.startTime1, this.partNumber, check4, check5, this.endTime2, this.startTime2, this.pageCurrent, this.pageSize).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableData.map(v => {
          this.$set(v, 'isEditing', false)
          v.原修补工时 = v.修补工时
          return v
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .el-card{
    margin: 0px;
  }
</style>
