<template>
  <div class="app-container">

    <el-card>
      <span>项目名称:</span>
      <el-select
        v-model="entryNameValue"
        placeholder="项目名称"
        style="width:200px"
        size="small"
        clearable
        filterable
        @change="empty">
        <el-option
          v-for="item in entryName"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>机床号:</span>
      <el-input v-model="machineToolNumber" placeholder="机床号" size="small" readonly clearable style="width:200px" @change="empty1">
        <el-button slot="append" icon="el-icon-search" @click="SearchMachine" />
      </el-input>
      <span>组号:</span>
      <el-input v-model="groupNumber" placeholder="组号" size="small" readonly clearable style="width:200px">
        <el-button slot="append" icon="el-icon-search" @click="searchGroupList" />
      </el-input>
      <span>零件号:</span>
      <el-input v-model="partNumber" placeholder="零件号" size="small" clearable style="width:200px"/>
      <span>零件状态:</span>
      <el-select v-model="partStateValue" placeholder="零件状态" size="small" clearable style="width:200px">
        <el-option
          v-for="item in partState"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <!--<span>零件类型:</span>-->
      <!--<el-select v-model="partTypeValue" placeholder="零件类型" size="small" clearable style="margin-top: 15px;width: 200px">-->
      <!--<el-option-->
      <!--v-for="item in partType"-->
      <!--:key="item.value"-->
      <!--:label="item.label"-->
      <!--:value="item.value"/>-->
      <!--</el-select>-->
      <el-button type="success" icon="el-icon-search" size="small" style="margin: 15px 0 0 10px" @click="pageCurrent=1;pageSize=10;total=0;searchData()">查询</el-button>
    </el-card>

    <el-card>
      <el-table
        v-loading="loading0"
        :data="tableData"
        style="width: 100%;max-height: 600px"
        height="600"
        size="mini"
        highlight-current-row
        border>
        <el-table-column align="center" label="序号" type="index" width="60px"/>
        <el-table-column align="center" label="机床图号" width="170px">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="组号" width="140px">
          <template slot-scope="scope">
            {{ scope.row.组号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件图号" width="200px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件名称" width="180px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="数量" width="60px">
          <template slot-scope="scope">
            {{ scope.row.投产总数量 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="投产时间" width="130px">
          <template slot-scope="scope">
            {{ scope.row.传递时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="零件状态" width="750px">
          <template slot-scope="scope">
            <el-steps :active="scope.row.考核状态" finish-status="success" simple>
              <el-step title="投产" />
              <el-step title="工艺" />
              <el-step title="定额" />
              <el-step title="平衡" />
              <el-step title="完成" />
              <el-step title="入库" />
              <el-step title="出库" />
            </el-steps>
          </template>
        </el-table-column>
        <!--<el-table-column label="操作" fixed="right" align="center" width="120px">-->
        <!--<template slot-scope="scope">-->
        <!--<el-button-->
        <!--size="mini"-->
        <!--type="primary"-->
        <!--icon="el-icon-edit"-->
        <!--@click="handleEdit(scope.row)">编辑</el-button>-->
        <!--</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>

    <el-dialog :title="title" :visible.sync="dialogFormVisible4" center style="min-height: 300px" width="400px">
      <el-form label-position="left" label-width="110px" class="demo-ruleForm">
        <el-form-item label="零件状态">
          <el-select v-model="partStateValue1" placeholder="零件状态" size="small">
            <el-option
              v-for="item in partState"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="submitEdit()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="选择机床" center style="min-height: 300px">
      <el-table
        :data="tableData6"
        style="width:100%;max-height: 300px;display: inline-block"
        height="300"
        highlight-current-row
        border
        @row-click="selectMachine">
        <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-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="pageCurrent1"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="pageSize1"
          :total="total1"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitName">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible3" title="选择组号" center style="min-height: 300px">
      <el-table
        :data="tableData7"
        style="width:100%;max-height: 300px;display: inline-block"
        height="300"
        highlight-current-row
        border
        @row-click="selectGroup">
        <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="pageCurrent2"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="pageSize2"
          :total="total2"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange3"
          @current-change="handleCurrentChange3"/>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitGroup">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { initProject, intPartState, searchTable, searchmachine, searchgroup, editData } from '@/api/ManufacturingCenter/StatusQuery'
import elInput from 'element-ui/packages/input'

export default {
  components: {
    elInput
  },
  data() {
    return {
      loading0: false,
      entryNameValue: '', // 项目流水号
      entryName: [], // 项目数组
      machineToolNumber: '', // 机床图号
      machineNumber: '',
      machine: null, // 机床流水号
      groupNumber: '', // 组号
      groupNumber1: '',
      groupNum: null, // 组件流水号
      partNumber: '', // 零件号
      partStateValue: null, // 零件状态
      partStateValue1: null,
      partState: [], // 零件状态数组
      partTypeValue: null, // 零件类型
      num: null,
      pageCurrent: 1,
      pageSize: 10,
      pageCurrent1: 1,
      pageSize1: 10,
      pageCurrent2: 1,
      pageSize2: 10,
      tableData7: [],
      tableData6: [],
      tableData: [],
      state: null,
      total: null,
      total1: null,
      total2: null,
      title: '',
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogFormVisible4: false,
      List: [],
      check: null,
      check1: null,
      check2: null,
      check3: null,
      check4: null,
      check5: null,
      partType: [{
        value: 1,
        label: '自家生产'
      }, {
        value: 2,
        label: '外协生产'
      }]
    }
  },
  created() {
    this.fetchData()
    this.getList()
  },
  methods: {
    fetchData() {
      intPartState().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.partState.push({
            label: response.data[i].考核状态名称,
            value: response.data[i].考核状态
          })
        }
      })
    },
    searchData() {
      if (this.entryNameValue !== null && this.entryNameValue !== '') {
        this.check = 1
      } else {
        this.check = 0
      }
      if (this.machineToolNumber !== '') {
        this.check1 = 1
      } else {
        this.check1 = 0
      }
      if (this.groupNumber !== '') {
        this.check2 = 1
      } else {
        this.check2 = 0
      }
      if (this.partNumber !== '') {
        this.check3 = 1
      } else {
        this.check3 = 0
      }
      if (this.partStateValue !== null && this.partStateValue !== '') {
        this.check4 = 1
      } else {
        this.check4 = 0
      }
      if (this.partTypeValue !== null && this.partTypeValue !== '') {
        this.check5 = 1
      } else {
        this.check5 = 0
      }
      this.loading0 = true
      searchTable(this.check, this.entryNameValue, this.check1, this.machine, this.check2, this.groupNum, this.check3, this.partNumber, this.check4, this.partStateValue, this.check5, this.partTypeValue, this.pageSize, this.pageCurrent).then(response => {
        for (let i = 0; i < response.data.rows.length; i++) {
          response.data.rows[i].考核状态 = parseInt(response.data.rows[i].考核状态) - 2
          if (response.data.rows[i].传递时间 !== undefined || response.data.rows[i].hasOwnProperty('传递时间')) {
            response.data.rows[i].传递时间 = response.data.rows[i].传递时间.split(' ', 2)[0]
          }
        }
        this.loading0 = false
        this.tableData = response.data.rows
        this.total = parseInt(response.data.total)
      })
    },
    getList() { // 初始化项目名称
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.entryName.push({
            label: response.data[i].项目名称,
            value: response.data[i].项目流水号
          })
        }
      })
    },
    empty() {
      this.machineToolNumber = ''
      this.groupNumber = ''
    },
    SearchMachine() { // 查询机床
      if (this.entryNameValue !== null && this.entryNameValue !== '') {
        this.dialogFormVisible2 = true
        this.check6 = 1
        searchmachine(this.entryNameValue, this.check6, this.pageCurrent1, this.pageSize1).then(response => {
          for (let i = 0; i < response.data.total; i++) {
            if (response.data.rows[i].机床开始时间 !== '') {
              response.data.rows[i].机床开始时间 = response.data.rows[i].机床开始时间.substr(0, 10)
            }
            if (response.data.rows[i].机床结束时间 !== '') {
              response.data.rows[i].机床结束时间 = response.data.rows[i].机床结束时间.substr(0, 10)
            }
            if (response.data.rows[i].工位属性 === '请选择') {
              response.data.rows[i].工位属性 = ''
            }
            if (response.data.rows[i].机床主管 === '请选择') {
              response.data.rows[i].机床主管 = ''
            }
          }
          this.tableData6 = response.data.rows
          this.total1 = parseInt(response.data.total)
        })
      } else {
        this.$message.warning('请先选择项目')
      }
    },
    selectMachine(row) { // 选择机床号
      this.machine = row.机床流水号
      this.machineNumber = row.机床图号
    },
    submitName() { // 提交机床号
      this.machineToolNumber = this.machineNumber
      this.dialogFormVisible2 = false
    },
    empty1() {
      this.groupNumber = ''
    },
    searchGroupList() { // 查询组号
      if (this.machineToolNumber !== '') {
        this.tableData7 = []
        this.dialogFormVisible3 = true
        searchgroup(this.machine, this.pageCurrent2, this.pageSize2).then(response => {
          this.tableData7 = response.data.rows
          this.total2 = parseInt(response.data.total)
        })
      } else {
        this.$message.warning('请先选择机床')
      }
    },
    selectGroup(row) { // 选择组号
      this.groupNumber1 = row.组号
      this.groupNum = row.组件流水号
    },
    submitGroup() { // 提交组号
      this.groupNumber = this.groupNumber1
      this.dialogFormVisible3 = false
    },
    handleEdit(row) {
      this.title = '编辑'
      this.partStateValue1 = parseInt(row.考核状态) + 2
      this.dialogFormVisible4 = true
      this.num = row.工艺计划流水号
    },
    submitEdit() { // 提交编辑
      editData(this.num, this.partStateValue1).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('编辑成功')
          this.dialogFormVisible4 = false
          this.pageCurrent = 1
          this.searchData()
        } else { this.$message.error('编辑失败') }
      })
    },
    cancel() { // 取消
      this.dialogFormVisible4 = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.searchData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchData()
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.SearchMachine()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.SearchMachine()
    },
    handleSizeChange3(val) {
      this.pageSize2 = val
      this.searchGroupList()
    },
    handleCurrentChange3(val) {
      this.pageCurrent2 = val
      this.searchGroupList()
    }
  }
}
</script>

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