<template>
  <div class="app-container" style="width: 100%;">
    <!--表格-->
    <el-table
      :data="tableData"
      ref="tableRef">
      <el-table-column prop="machineId" fixed label="线号" header-align="center" align="center" width="111px" ></el-table-column>
      <el-table-column prop="productionDigit" fixed label="生产位数" header-align="center" align="center" width="111.2px"></el-table-column>
      <el-table-column label="在机产品" header-align="center" >
        <el-table-column prop="productTypeName" fixed label="产品类型" header-align="center" align="center" width="111.2px"></el-table-column>
        <el-table-column prop="currentColorCode" label="色号" header-align="center" width="90px" align="center"></el-table-column>
        <el-table-column prop="currentSpecification" label="规格" header-align="center" width="90px" align="center"></el-table-column>
        <el-table-column prop="currentLotNumber" label="批号" header-align="center"  width="90px" align="center"></el-table-column>
        <el-table-column prop="startTime" label="上机时间" header-align="center" width="90px" align="center"></el-table-column>
        <el-table-column prop="plannedEndTime" label="下机时间" header-align="center" width="90px" align="center"></el-table-column>
      </el-table-column>
      <el-table-column prop="plannedProduction" label="待生产产品列表" header-align="center" align="center"  >
        <template v-slot:default="scope">
          <div v-if="scope.row.plannedProduction && scope.row.plannedProduction.length > 0">
            <div v-for="(chunk, chunkIndex) in splitList(scope.row.plannedProduction, 5)" :key="`chunk-${chunkIndex}`">
                  <span v-for="(item, index) in chunk" :key="`item-${chunkIndex}-${index}`">
                    {{ item }}, <!-- 替换为实际显示每个产品的代码 -->
                  </span>
            </div>
          </div>
          <div v-else>

          </div>
        </template>
      </el-table-column>
      <el-table-column   fixed="right" label="操作" header-align="center" align="center" width="150px" >
        <template v-slot:default="scope">
          <el-dropdown>
            <el-button type="primary">
              操作
              <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native.prevent="handleAddPlan(scope.row)">新增计划</el-dropdown-item>
              <el-dropdown-item @click.native.prevent="handleEditPlan(scope.row)">修改计划</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!--新增计划弹窗-->
    <el-dialog title="新增计划"  :visible.sync="nanInsertDialogTableVisible" width="1200px">
      <el-table :data="tableData2" @row-click.self="handleRowClick">
        <el-table-column prop="productId" label="产品" header-align="center" align="center" width="150px">
          <template v-slot:default="scope">
            <el-select  v-model="scope.row.productId" placeholder="请选择待排产产品" @change="selectedChangeDate(scope.row,scope.row.productId)">
              <el-option v-for="product in productsInfo" :key="product.id" :label="product.productName" :value="product.id" ></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column prop="currentColorCode" label="色号" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-select v-model="scope.row.currentColorCode" filterable placeholder="请选择母粒编码" @change="selectedML(scope.row)">
              <el-option
                v-for="(item,index) in options"
                :key="index"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
<!--            &lt;!&ndash; 如果当前行正在编辑，则显示输入框 &ndash;&gt;-->
<!--            <el-input v-if="scope.row.isEditing"  disabled="disabled"   v-model="scope.row.currentColorCode" placeholder="请输入色号" @blur.stop.prevent="handleInputBlur(scope.row)" ></el-input>-->
<!--            &lt;!&ndash; 否则显示普通文本 &ndash;&gt;-->
<!--            <span v-else>{{ scope.row.currentColorCode }}</span>-->
          </template>
        </el-table-column>
        <el-table-column prop="productionDigit" label="生产位数" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" v-model="scope.row.productionDigit" placeholder="请输入生产位数" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.productionDigit }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="currentSpecification" label="规格" header-align="center" align="center">
          <template v-slot:default="scope">
            <!-- 如果当前行正在编辑，则显示输入框 -->
            <el-input v-if="scope.row.isEditing"  disabled="disabled"  v-model="scope.row.currentSpecification" placeholder="请输入规格" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <!-- 否则显示普通文本 -->
            <span v-else>{{ scope.row.currentSpecification }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="currentLotNumber" label="批号" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" :disabled="isDisabled"  v-model="scope.row.currentLotNumber" placeholder="请输入批号" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.currentLotNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="number" label="数量" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" v-model="scope.row.number" placeholder="请输入数量" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.number }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="startTime" label="上机时间" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" v-model="scope.row.startTime" placeholder="请输入上机时间" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.startTime }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="plannedEndTime" label="计划下机时间" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" :ref="'input'+scope.$index" v-model="scope.row.plannedEndTime" placeholder="请输入计划下机时间" @blur="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.plannedEndTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="planStatus" label="计划状态" header-align="center" align="center" width="100px">
          <template v-slot:default="scope">
            <el-select  v-model="scope.row.planStatus" placeholder="计划状态" >
              <el-option v-for="stateItem in stateList0" :key="stateItem.id" :label="stateItem.name" :value="stateItem.id" ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click.stop.prevent="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" style="width: 100%;margin-top: 30px"  @click="addEditableRow">新增计划产品</el-button>
      <span slot="footer" class="dialog-footer">
            <el-button @click="nanInsertDialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="addPlanInfo">确 定</el-button>
           </span>
    </el-dialog>
    <!--修改计划弹窗-->
    <el-dialog title="修改计划"  :visible.sync="nanUpdateDialogTableVisible" width="1200px">
      <!--查询条件-->
      <el-form ref="form"  :model="formData" label-width="80px" :inline="true">
        <el-form-item label="计划状态:">
          <el-select v-model="formData.planStatus" placeholder="请选择计划状态">
            <el-option v-for="stateListItem in stateList2" :key="product.id" :label="stateListItem.name" :value="stateListItem.id" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <!--表格-->
      <el-table :data="tableData2" @row-click.self="handleRowClick">
        <el-table-column prop="productId" label="产品" header-align="center" align="center" width="150px">
          <template slot-scope="scope">
            {{ getProductChineseName(scope.row.productId) }}
          </template>
        </el-table-column>
        <el-table-column prop="productionDigit" label="生产位数" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" v-model="scope.row.productionDigit" placeholder="请输入生产位数" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.productionDigit }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="currentColorCode" label="色号" header-align="center" align="center">
          <template v-slot:default="scope">
            <!-- 如果当前行正在编辑，则显示输入框 -->
            <el-input v-if="scope.row.isEditing"  disabled="disabled"  v-model="scope.row.currentColorCode" placeholder="请输入色号" @blur.stop.prevent="handleInputBlur(scope.row)" ></el-input>
            <!-- 否则显示普通文本 -->
            <span v-else>{{ scope.row.currentColorCode }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="currentSpecification" label="规格" header-align="center" align="center">
          <template v-slot:default="scope">
            <!-- 如果当前行正在编辑，则显示输入框 -->
            <el-input v-if="scope.row.isEditing" :disabled="false" v-model="scope.row.currentSpecification" placeholder="请输入规格" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <!-- 否则显示普通文本 -->
            <span v-else>{{ scope.row.currentSpecification }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="currentLotNumber" label="批号" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing"   v-model="scope.row.currentLotNumber" placeholder="请输入批号" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.currentLotNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="number" label="数量" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" v-model="scope.row.number" placeholder="请输入数量" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.number }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="上机时间" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" v-model="scope.row.startTime" placeholder="请输入上机时间" @blur.stop.prevent="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.startTime }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="plannedEndTime" label="计划下机时间" header-align="center" align="center">
          <template v-slot:default="scope">
            <el-input v-if="scope.row.isEditing" :ref="'input'+scope.$index" v-model="scope.row.plannedEndTime" placeholder="请输入计划下机时间" @blur="handleInputBlur(scope.row)">></el-input>
            <span v-else>{{ scope.row.plannedEndTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="planStatus" label="计划状态" header-align="center" align="center" width="100px">
          <template v-slot:default="scope">
            <!--上机变停机待机-->
            <el-select v-if="scope.row.planStatus==1" v-model="scope.row.planStatus" placeholder="计划状态" >
              <el-option v-for="stateItem in stateList1" :key="stateItem.id" :label="stateItem.name" :value="stateItem.id" ></el-option>
            </el-select>
            <!--待机可以上机-->
            <el-select v-if="scope.row.planStatus==0" v-model="scope.row.planStatus" placeholder="计划状态" >
              <el-option v-for="stateItem in stateList0" :key="stateItem.id" :label="stateItem.name" :value="stateItem.id" ></el-option>
            </el-select>
            <!--停机变上机-->
            <el-select v-if="scope.row.planStatus==2" v-model="scope.row.planStatus" placeholder="计划状态" >
              <el-option v-for="stateItem in stateList2" :key="stateItem.id" :label="stateItem.name" :value="stateItem.id" ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template v-slot:default="scope">
            <el-button
              size="mini"
              type="danger"
              @click.stop.prevent="updateStatus(scope.$index, scope.row,scope.row.planStatus)">更新状态</el-button>
            <el-button
              size="mini"
              type="danger"
              @click.stop.prevent="deletePlan(scope.$index, scope.row)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <!--按钮信息-->
      <span slot="footer" class="dialog-footer">
            <el-button @click="nanUpdateDialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="nanUpdateDialogTableVisible = false">确 定</el-button>
           </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getProductsInfo,
  getProductsAll,
  getMaterielInfo,
  spinningFrameWinding,
  getSpinningPlanList,
  addSpinningPlanList,
  getPlanSpinningInfo,
  updateSpinningInfoPlanOne,getCurrentProductionLineSuperior, getIsTheOrderProductProduced
} from "@/api/plan/paln.js";
export default {
  data() {
    return {
      options: [],
      //
      isDisabled:true,
      //选中的是那个车间
      tableData:[],
      form: {
        product:"",
      },
      previousMachineId: undefined,
      currentRowspan: 1,
      prevRow: {},
      tableData2: [],
      tableDataStatus: [],
      nanInsertDialogTableVisible:false,
      nanUpdateDialogTableVisible:false,
      beiInsertDialogTableVisible:false,
      beiUpdateDialogTableVisible:false,
      editingRowIndex: -1,
      lastEditLine:0,
      isEditLine:false,
      productsInfo:[],
      salesmanInfo:[],
      product:{},
      materiel:{},
      //机台号
      machineId:"",
      macId:"",
      // 待机/上机/停产
      stateList0:[
        {"id":0,"name":"待机"},
        {"id":1,"name":"上机"},
      ],
      stateList1:[
        {"id":0,"name":"待机"},
        {"id":2,"name":"停机"},
        {"id":1,"name":"上机"},
      ],
      stateList2:[
        {"id":0,"name":"待机"},
        {"id":2,"name":"停机"},
        {"id":1,"name":"上机"},
      ],
      formData:{
        updateSelectedProductCategory:"",
        updateSelectedProductState:""
      },
    }
  },
  mounted() {
    // 得到排产计划信息
    this.getSpinningPlanList();
  },
  methods: {
    selectedML(row){
      console.log(row.currentColorCode)
      if(row.currentColorCode){
        let tarName=this.options.find(item=>item.value==row.currentColorCode).colourNumber
        console.log("aaaaa=============",tarName)
        row.colourNumber=tarName;
        console.log("aaaaa=============",row)
      }
    },
    getProductChineseName(productId) {
      return  this.productsInfo.find(product => product.id == productId).productName;
    },
    //修改计划里的删除计划
    deletePlan($index,row){
      console.log(row)
      getIsTheOrderProductProduced({"id":row.id}).then(resp=>{
        if(resp.data.length>0){
          this.$message({message: '当前计划订单产品在生产中已经不能删除', type: 'warning'});
          return;//证明有在上级的产品--zhi能有一个上级产品
        }else {
          //没有开始生产可以删除
          spinningFrameWinding(row.id).then(res=>{
            if(res.code==200){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
            this.onSubmit();
            this.getSpinningPlanList();
          })
        }
      });

    },
    // 待机产品合并（五个显示为一排）
    splitList(list, chunkSize) {
      const chunks = [];
      for (let i = 0; i < list.length; i += chunkSize) {
        chunks.push(list.slice(i, i + chunkSize));
      }
      return chunks;
    },
    // 查询排产信息
    getSpinningPlanList() {
      getSpinningPlanList({"productionLineDepartment":"络筒部"}).then((response) => {
        this.tableData = response.rows
      });
    },
    // 新增计划是得到产品信息
    getProductsInfo() {
      getProductsInfo("LT-").then((response) => {
        this.productsInfo = response.data;
        this.productsInfo.forEach(item => {
          // item.productName = item.productName+"—"+item.materieEncoding+item.materieColorNumber
          item.productName = item.productName+"—"+item.colours;
        });
      })
    },
    // 下拉框数据改变查询色号批号信息
    selectedChangeDate(row,selectedProduct) {
      //选择的那个销售产品
      this.product = this.productsInfo.find(product => product.id === selectedProduct);
      //得到选择的商品
      this.form.product=selectedProduct;
      //通过母粒编码查询到对应的母粒信息
      // let queryParams = {
      //   "materielCode": this.product.colourNumber
      // }
      // //得到对应的母粒信息
      // getMaterielInfo(queryParams).then((response) => {
      //   if (response.data!=null) {
      //     this.materiel = response.data;
      //     console.log(response.data,"规格")
      //     //色号 母粒编码+色号
      //     row.currentColorCode = this.materiel.materieEncoding+"("+this.materiel.materieColorNumber+")";
      //   }
      // });
      let queryParams = {
        // "materielCode": this.product.colourNumber,
        "materielSpecies":"1"
      }
      //得到对应的母粒信息
      getMaterielInfo(queryParams).then((response) => {
        if (response.data!=null) {
          let mls = response.data
          mls.forEach(dataKey => {
            this.options.push({
              "label":dataKey.materieEncoding+"("+dataKey.materieColorNumber+")",
              "value":dataKey.materieEncoding+"("+dataKey.materieColorNumber+")",
              "colourNumber":dataKey.materielCode
            })
          });
          //色号 母粒编码+色号
          // row.currentColorCode = this.materiel.materieEncoding+"("+this.materiel.materieColorNumber+")";
        }
      });
      //isDisabled(有批号就用，没批号就自己输入)
      if(this.product.lotNumber!=undefined&&this.product.lotNumber!=""){
        row.currentLotNumber=this.product.lotNumber;
        this.isDisabled=true;
      }else {
        this.isDisabled=false;
        row.currentLotNumber="";
      }
      //产品规格
      row.currentSpecification= this.product.productSpecifications;
      //合同号--销售单编号（以前打算存的是业务员信息后来存的是销售单编号）
      row.salesmanId=this.product.saleOrderNo;
      //数量
      row.number=this.product.productNumber;
    },
    // 新增计划产品信息
    handleAddPlan(row) {
      this.getProductsInfo();
      this.nanInsertDialogTableVisible = true
      this.tableData2=[]
      //先清空
      this.machineId=""
      //当前操作机台号
      this.machineId=row.machineId;
      //机台id
      this.macId=row.macId;
    },
    // 新增删除行
    handleDelete(index, row) {
      // console.log(index,row)
      // this.tableData2[a];
      this.tableData2.splice(index, 1);
      // 把上一编辑行置回-1
      this.editingRowIndex = -1;
    },
    // 点击编辑行
    handleRowClick(row) {
      // this.isEditLine = true;
      // //找到当前行的编辑索引
      // var rowIndex = this.tableData2.indexOf(row);
      // console.log(rowIndex)
      // if (this.lastEditLine != 0 &&
      //   this.editingRowIndex !== rowIndex &&
      //   this.editingRowIndex !== -1 &&
      //   !this.isRowFilled(this.tableData2[this.lastEditLine])) {
      //   this.$message({
      //     message: '请先完成上一条数据的填写',
      //     type: 'warning'
      //   });
      //   return;
      // }
      // //编辑索引不等于当前编辑行索引
      // if (this.editingRowIndex !== rowIndex) {
      //   // alert(this.editingRowIndex);
      //   if (this.editingRowIndex !== -1) {
      //     //把当前行变成不可编辑的
      //     this.tableData2[this.editingRowIndex].isEditing = false;
      //   }
      //   //把当前行变成可编辑的
      //   row.isEditing = true;
      //   this.editingRowIndex = rowIndex;
      // }else {
      //   //把当前行变成可编辑的
      //   row.isEditing = true;
      //   this.editingRowIndex = rowIndex;
      // }

    },
    // 光标失去焦点变成不可编辑
    handleInputBlur(row) {
      // let rowIndex = this.tableData2.indexOf(row);
      // //编辑索引不等于当前编辑行索引
      // if(row.isEditing="true"){
      //   row.isEditing = false;
      //   //对于整个表格来说
      //   this.isEditLine = false;
      //   //当前编辑行置为-1
      //   this.editingRowIndex = -1;
      // }
      //编辑索引不等于当前编辑行索引
      // if (this.editingRowIndex !== rowIndex) {
      //   if (this.isRowFilled(row)) {
      //     row.isEditing = false;
      //     //对于整个表格来说
      //     this.isEditLine = false;
      //     //当前编辑行置为-1
      //     this.editingRowIndex = -1;
      //   }
      // }
    },
    // 检查行是否填写完毕的示例方法
    isRowFilled(row) {
      return  row.productionDigit &&
        row.currentColorCode &&
        row.currentSpecification &&
        row.currentLotNumber&&
        row.number&&
        row.startTime &&
        row.plannedEndTime;
    },
    // 新增一行标记为可编辑
    addEditableRow() {
      this.getProductsInfo();
      // 检查当前编辑行是否已填写完毕
      if(this.tableData2.length!=0){
        if (this.editingRowIndex !== -1 && !this.isRowFilled(this.tableData2[this.editingRowIndex])) {
          this.$message({
            message: '请先完成上一条数据的填写',
            type: 'warning'
          });
          return;
        }
        //新增行之前先让以前的行不能编辑
        this.tableData2.forEach(item => {
          item.isEditing = false;
        })
      }
      this.tableData2.push({
        currentColorCode: '',
        currentSpecification: '',
        currentLotNumber: '',
        productionDigit: '',
        number:'',
        startTime: '',
        plannedEndTime:'',
        planStatus:0,
        productId:'',
        salesmanId:'',
        isEditing: true, // 标记为正在编辑状态
        planType:"络筒计划",
        colourNumber:"",
      });
      this.editingRowIndex = this.tableData2.length - 1; // 更新当前编辑的行索引
      this.lastEditLine = this.tableData2.length - 1
    },
    // 添加计划
    addPlanInfo(){
      if(this.tableData2.length<=0){
        this.$message({message: '请先添加表格数据', type: 'warning'});
      }
      var flag=true;
      this.tableData2.forEach(item=>{
        if(!this.isRowFilled(item)){
          flag=false
        }
      })
      if(flag==false){
        this.$message({message: '请完善表格行数据', type: 'warning'});
        return ;
      }
      const datatime=Date.now()+this.machineId;
      let i=0
      //处理表格数据
      this.tableData2.forEach(item=>{
        item.timestampRandomCode=datatime;
        item.macId=this.macId;
        item.machineId=this.machineId;
        if(item.planStatus==1){
          i++
        }
      })
      if(i>=2){
        this.$message({
          message: '不能同时添加俩上机计划',
          type: 'warning'
        });
        return;
      }
      //先查询计划表中有没有正在上机的
      getPlanSpinningInfo({"machineId":this.machineId,"macId":this.macId,"planStatus":1}).then(response => {
        if(i>=1&&response.data.length>0){
          this.$message({
            message: '只能有一个产品的状态为上机',
            type: 'warning'
          });
          return;
        }else {
          //添加计划信息
          addSpinningPlanList(this.tableData2).then(response => {
            if (response.code == 200) {
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.getSpinningPlanList();
              //最后把弹窗关掉
              this.nanInsertDialogTableVisible=false
            } else {
              this.$message({
                message: '添加失败',
                type: 'error'
              });
              //最后把弹窗关掉
              this.nanInsertDialogTableVisible=false
            }
          });
        }
      });
    },
    // 修改计划
    handleEditPlan(row) {
      //已经在待机列表了，isNull过滤掉了
      getProductsAll(row.macId).then((response) => {
        this.productsInfo = response.data;
        this.productsInfo.forEach(item => {
          item.productName = item.productName+"—"+item.materieEncoding+item.materieColorNumber
        });
        //当前操作机台号
        this.machineId=row.machineId;
        //机台id
        this.macId=row.macId;
        this.tableData2=[];
        //弹窗
        this.nanUpdateDialogTableVisible = true
        getPlanSpinningInfo({"macId":row.macId}).then((response) => {
          //得到生产中，停产的，待产的
          response.data.forEach(item => {
            item.isEditing=false;
            item.planStatus=parseInt(item.planStatus);
            item.productId=parseInt(item.productId)
          })
          this.tableData2=response.data;
        })
      })
    },
    // 弹窗里边的点击查询
    onSubmit(){
      this.formData.macId=this.macId;
      getPlanSpinningInfo(this.formData).then(response => {
        //得到生产中，停产的，待产的
        response.data.forEach(item => {
          item.isEditing=false;
          item.planStatus=parseInt(item.planStatus);
        })
        this.tableData2=response.data;
      });
    },
    // 重置
    reset(){
      getPlanSpinningInfo({"macId":this.macId}).then(response => {
        //得到生产中，停产的，待产的
        response.data.forEach(item => {
          item.isEditing=false;
          item.planStatus=parseInt(item.planStatus);
        })
        this.tableData2=response.data;
      });
    },
    // 更新状态
    updateStatus(ind, row){
      row.planType="络筒计划"
      if(!this.isRowFilled(row)){
        this.$message({message: '请完善当前行数据', type: 'warning'});
        return;
      }
      if(row.planStatus=="2"){
        //如果说我要去停机一个产品我只需要去鉴别你这个产线上有没有停机或者上其他上机的产品
        getCurrentProductionLineSuperior({"id":row.id,"macId":row.macId,"planStatus":row.planStatus}).then(response => {
          if(response.data.length>0) {
            this.$message({message: '已有停机产品不能在停机', type: 'warning'});
            return;//证明有在上级的产品--只能有一个上级或者停机的产品
          }else {
            getCurrentProductionLineSuperior({"id":row.id,"macId":row.macId,"planStatus":"1"}).then(response => {
              if(response.data.length>0){
                this.$message({message: '已有一个其他产品正在上机，不能在停机,如需要停机,请停机在机产品', type: 'warning'});
                return;//证明有在上级的产品--zhi能有一个上级产品
              }else {
                updateSpinningInfoPlanOne(row).then(response=>{
                  if (response.code == 200) {
                    this.$message({
                      message: '修改成功',
                      type: 'success'
                    });
                    this.getSpinningPlanList();
                    //最后把弹窗关掉
                    this.nanInsertDialogTableVisible=false
                  } else {
                    this.$message({
                      message: '修改失败',
                      type: 'error'
                    });
                    //最后把弹窗关掉
                    this.nanInsertDialogTableVisible=false
                  }
                })
              }
            })
          }
        });
        return;
      }
      //判断当前计划是否已经投产
      getIsTheOrderProductProduced({"id":row.id}).then(resp=>{
        if(resp.data.length>0){
          this.$message({message: '当前计划订单产品在生产中已经不能修改', type: 'warning'});
          return;//证明有在上级的产品--zhi能有一个上级产品
        }
        //不等于0待机才去校验是否重复
        if(row.planStatus!=0){
          //先查询计划表中有没有正在上机的或者停机的
          getCurrentProductionLineSuperior({"id":row.id,"macId":row.macId,"planStatus":row.planStatus}).then(response => {
            if(response.data.length>0){
              if(row.planStatus==1){
                this.$message({message: '只能有一个产品的状态为上机', type: 'warning'});
                return;//证明有在上级的产品--zhi能有一个上级产品
              }else {
                this.$message({message: '已停机不能在停机', type: 'warning'});
                return;//证明有在上级的产品--zhi能有一个上级产品
              }
            }else {
              if(row.planStatus==1){
                getCurrentProductionLineSuperior({"id":row.id,"macId":row.macId,"planStatus":"2"}).then(response => {
                  if(response.data.length>0){
                    this.$message({message: '已停机，请先处理停机产品，然后上机', type: 'warning'});
                    return;//证明有在上级的产品--zhi能有一个上级产品
                  }else {
                    updateSpinningInfoPlanOne(row).then(response=>{
                      if (response.code == 200) {
                        this.$message({
                          message: '修改成功',
                          type: 'success'
                        });
                        this.getSpinningPlanList();
                        //最后把弹窗关掉
                        this.nanInsertDialogTableVisible=false
                      } else {
                        this.$message({
                          message: '修改失败',
                          type: 'error'
                        });
                        //最后把弹窗关掉
                        this.nanInsertDialogTableVisible=false
                      }
                    })
                  }
                })
              }
              if(row.planStatus==2){
                getCurrentProductionLineSuperior({"id":row.id,"macId":row.macId,"planStatus":"1"}).then(response => {
                  if(response.data.length>0){
                    this.$message({message: '已有一个其他产品正在上机，不能在停机,如需要停机,请停机在机产品', type: 'warning'});
                    return;//证明有在上级的产品--zhi能有一个上级产品
                  }else {
                    updateSpinningInfoPlanOne(row).then(response=>{
                      if (response.code == 200) {
                        this.$message({
                          message: '修改成功',
                          type: 'success'
                        });
                        this.getSpinningPlanList();
                        //最后把弹窗关掉
                        this.nanInsertDialogTableVisible=false
                      } else {
                        this.$message({
                          message: '修改失败',
                          type: 'error'
                        });
                        //最后把弹窗关掉
                        this.nanInsertDialogTableVisible=false
                      }
                    })
                  }
                })
              }
            }
          });
        }else {
          //修改待机的话直接修改
          updateSpinningInfoPlanOne(row).then(response=>{
            if (response.code == 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.getSpinningPlanList();
              //最后把弹窗关掉
              this.nanInsertDialogTableVisible=false
            } else {
              this.$message({
                message: '修改失败',
                type: 'error'
              });
              //最后把弹窗关掉
              this.nanInsertDialogTableVisible=false
            }
          })
        }

      })

    }
  }
}
</script>

<style scoped>
.el-tabs--border-card >>> .el-tabs__content {
  padding: 0 !important;
}
.el-table__body-wrapper {
  cursor: grab;
}
.el-table__body-wrapper.grabbing {
  cursor: grabbing;
}
.container >>> .el-table .cell {
  white-space: pre-line;
}

</style>
