<template>
  <div>
    <el-card>
      <el-row>
        <!--<span>项目名称:</span>-->
        <!--<el-select v-model="entryNameValue" clearable filterable size="small" style="margin-bottom:10px" placeholder="项目名称" @change="typeChange()">-->
        <!--<el-option-->
        <!--v-for="item in entryName"-->
        <!--:key="item.value"-->
        <!--:label="item.label"-->
        <!--:value="item.value"/>-->
        <!--</el-select>-->
        <span>机床名称:</span>
        <el-select v-model="toolNumValue" filterable clearable size="small" style="margin-top:10px;width: 220px" placeholder="机床号" @change="typeChange1()">
          <el-option
            v-for="item in toolNum"
            :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.value2 }}</div>
          </el-option>
        </el-select>
        <!--<el-select v-model="toolNumValue" clearable filterable size="small" placeholder="机床号" style="margin-bottom:10px" @change="typeChange1()">-->
        <!--<el-option-->
        <!--v-for="item in toolNum"-->
        <!--:key="item.value"-->
        <!--:label="item.label"-->
        <!--:value="item.value"/>-->
        <!--</el-select>-->
        <span>分组:</span>
        <el-select v-model="NumValue" clearable filterable size="small" style="margin-bottom:10px" placeholder="机床组号">
          <el-option
            v-for="item in Num"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>零件图号:</span>
        <el-input v-model="partName" placeholder="零件图号" size="small" clearable style="width:200px"/>
        <el-button size="small" icon="el-icon-search" type="success" style="margin-left: 10px" @click="pageSize1=10; pageCurrent1=1;searchTable()">查询</el-button>
      </el-row>
    </el-card>

    <el-card>
      <el-table v-loading="loading1" :data="tableData1" highlight-current-row height="350" style="width: 100%;" border element-loading-text="拼命加载中" @row-click="searchTable2">
        <el-table-column
          label=" "
          type="index"
          align="center"
          width="50"/>
        <el-table-column label="零件图号" align="center" width="380px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" width="320px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="材料" align="center" width="280px">
          <template slot-scope="scope">
            {{ scope.row.材料 }}
          </template>
        </el-table-column>
        <el-table-column label="种类" align="center" width="280px">
          <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 class="block">
        <el-pagination
          v-if="!loading1"
          :current-page="pageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>

    <el-card>
      <el-table v-loading="loading2" :data="tableData2" height="350" style="width: 100%;" border element-loading-text="拼命加载中" size="small">
        <el-table-column
          label=" "
          type="index"
          align="center"
          width="50"/>
        <el-table-column label="加工状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.工序状态)===3" type="success" size="small">未加工</el-tag>
            <el-tag v-if="parseInt(scope.row.工序状态)===4&&parseInt(scope.row.工艺编号)!==29" size="small">已完成</el-tag>
            <el-tag v-if="parseInt(scope.row.工艺编号)===29" size="small">备料</el-tag>
            <el-tag v-if="parseInt(scope.row.工序状态)===5" size="small">停产</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="序间外协" width="100" align="center">
          <template slot-scope="scope">
            <el-checkbox :disabled="parseInt(scope.row.工序状态)!==3||parseInt(scope.row.工艺编号)===29" v-model="checkBox[scope.$index]" @change="edit(scope.$index, scope.row)">选择</el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="是否外协" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.工序间是否外协)===2" type="success" size="small">是</el-tag>
            <el-tag v-else size="small">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="工艺名称" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.工艺名称 }}
          </template>
        </el-table-column>
        <el-table-column label="工艺要求" align="center" width="580px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.工艺要求" :autosize="{ minRows: 1, maxRows: 1}" size="mini" readonly type="textarea"/>
          </template>
        </el-table-column>
        <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="150px">
          <template slot-scope="scope">
            {{ scope.row.机加工结束时间 }}
          </template>
        </el-table-column>
        <el-table-column label="工序计划" align="center" width="150px">
          <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>
    </el-card>
  </div>
</template>

<script>
import { machine, getToolNum, getNum, searchTable, searchTable2, edit, edit2 } from '@/api/Outsourcing/PreorderAssociation'
export default {
  data() {
    return {
      entryNameValue: '',
      entryName: [], // 项目名称
      toolNumValue: '',
      toolNum: [], // 机床号
      NumValue: '',
      Num: [], // 分组
      count: 0, // 计数器  (表二是否有外协工序)
      InOut: 1, // 工序间是否外协变量 1自家 2外协
      num1: '',
      checkBox: [],
      loading1: false,
      loading2: false,
      check_partName: false,
      partName: '', // 零件名称
      tableData1: [], // 表格数据
      tableData2: [],
      total1: null, // 总条数
      pageSize1: 10, // 每页显示条数
      pageCurrent1: 1
    }
  },
  created() {
    this.typeChange()
    this.searchTable()
  },
  methods: {
    fetchData() {
      this.entryNameValue = ''
      this.entryName = []
      machine().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.entryName.push({
            label: response.data[i].项目名称,
            value: response.data[i].项目流水号
          })
        }
      })
    },
    typeChange() {
      this.toolNumValue = ''
      this.toolNum = []
      this.NumValue = ''
      this.Num = []
      getToolNum().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.toolNum.push({
            label: response.data[i].机床图号,
            value: response.data[i].机床流水号,
            value2: response.data[i].项目名称
          })
        }
      })
    },
    typeChange1() {
      this.NumValue = ''
      this.Num = []
      getNum(this.toolNumValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Num.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    searchTable() {
      this.loading1 = true
      this.tableData1 = []
      this.tableData2 = []
      if (this.entryNameValue !== null && this.entryNameValue !== '') {
        this.check_entryNameValue = 1
      } else {
        this.check_entryNameValue = 0
      }
      if (this.toolNumValue !== null && this.toolNumValue !== '') {
        this.check_toolNumValue = 1
      } else {
        this.check_toolNumValue = 0
      }
      if (this.NumValue !== null && this.NumValue !== '') {
        this.check_NumValue = 1
      } else {
        this.check_NumValue = 0
      }
      if (this.partName !== null && this.partName !== '') {
        this.check_partName = 1
      } else {
        this.check_partName = 0
      }
      searchTable(this.check_entryNameValue, this.entryNameValue, this.check_toolNumValue, this.toolNumValue, this.check_NumValue, this.NumValue, this.check_partName, this.partName, this.pageCurrent1, this.pageSize1).then(response => {
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      }).then(() => {
        this.loading1 = false
      })
    },
    searchTable2(row) {
      this.loading2 = true
      this.checkBox = []
      this.tableData2 = []
      searchTable2(row.工艺计划流水号).then(response => {
        console.log(response.data)
        this.num1 = row.工艺计划流水号
        if (response.data.length === 0) {
          this.loading0 = false
        } else {
          for (let i = 0; i < response.data.length; i++) {
            if (response.data[i].工序间是否外协 === 1) {
              this.checkBox.push(false)
            } else {
              this.checkBox.push(true)
            }
          }
          for (let i = 0; i < response.data.length; i++) {
            response.data[i].机加工开始时间 = response.data[i].机加工开始时间.split(' ')[0]
            response.data[i].机加工结束时间 = response.data[i].机加工结束时间.split(' ')[0]
            response.data[i].计划日期 = response.data[i].计划日期.split(' ')[0]
          }
          this.tableData2 = response.data
        }
      }).then(() => {
        this.loading2 = false
      })
    },
    // 修改外协状态
    edit(index, row) {
      this.count = 0
      for (let i = 0; i < this.tableData2.length; i++) {
        if (this.checkBox[i] === false) {
          this.count = this.count + 1
        }
      }
      if (this.count === this.tableData2.length) {
        edit2(row.工序流水号, row.工艺计划流水号, 0)
      }
      if (this.checkBox[index] === true) {
        this.InOut = 2
        edit(row.工序流水号, this.InOut).then(response => {
          edit2(row.工序流水号, row.工艺计划流水号, 1).then(response => {
            this.$message.success('工序外协状态更改成功')
          })
        }).then(() => {
          this.loading2 = true
          this.checkBox = []
          this.tableData2 = []
          searchTable2(this.num1).then(response => {
            if (response.data.length === 0) {
              this.loading0 = false
            } else {
              for (let i = 0; i < response.data.length; i++) {
                if (response.data[i].工序间是否外协 === 1) {
                  this.checkBox.push(false)
                } else {
                  this.checkBox.push(true)
                }
              }
              for (let i = 0; i < response.data.length; i++) {
                response.data[i].机加工开始时间 = response.data[i].机加工开始时间.split(' ')[0]
                response.data[i].机加工结束时间 = response.data[i].机加工结束时间.split(' ')[0]
                response.data[i].计划日期 = response.data[i].计划日期.split(' ')[0]
              }
              this.tableData2 = response.data
            }
          }).then(() => {
            this.loading2 = false
          })
        })
      } else {
        this.InOut = 1
        edit(row.工序流水号, this.InOut).then(response => {
          this.$message.success('工序外协状态更改成功')
        }).then(() => {
          this.loading2 = true
          this.checkBox = []
          this.tableData2 = []
          searchTable2(this.num1).then(response => {
            if (response.data.length === 0) {
              this.loading0 = false
            } else {
              for (let i = 0; i < response.data.length; i++) {
                if (response.data[i].工序间是否外协 === 1) {
                  this.checkBox.push(false)
                } else {
                  this.checkBox.push(true)
                }
              }
              for (let i = 0; i < response.data.length; i++) {
                response.data[i].机加工开始时间 = response.data[i].机加工开始时间.split(' ')[0]
                response.data[i].机加工结束时间 = response.data[i].机加工结束时间.split(' ')[0]
                response.data[i].计划日期 = response.data[i].计划日期.split(' ')[0]
              }
              this.tableData2 = response.data
            }
          }).then(() => {
            this.loading2 = false
          })
        })
      }
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable()
    }
  }
}
</script>

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