<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <div>
          <span style="margin-left: 10px">工件编号：</span>
          <el-input v-model="engineId" placeholder="工件编号" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">订货号：</span>
          <el-input v-model="orderCode" placeholder="订货号" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">订货号ID：</span>
          <el-input v-model="orderCodeId" placeholder="订货号ID" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">机型号：</span>
          <el-input v-model="engineType" placeholder="机型号" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">工单ID：</span>
          <el-input v-model="workOrderId" placeholder="工单ID" clearable size="small" style="width: 150px; margin-left: 5px"/>
          <span style="margin-left: 10px">工单号：</span>
          <el-input v-model="workOrderCode" placeholder="工单号" clearable size="small" style="width: 150px; margin-left: 5px"/>
        </div>
        <div>
          <span style="margin-left: 10px">开始日期：</span>
          <el-date-picker v-model="startPlanTime" style="width: 140px" type="date" size="small" value-format="yyyy-MM-dd" placeholder="选择开始日期" />
          <span style="margin-left: 10px">结束日期：</span>
          <el-date-picker v-model="endPlanTime" style="width: 140px" type="date" size="small" value-format="yyyy-MM-dd" placeholder="选择结束日期" />
          <span style="margin-left: 10px">订单状态：</span>
          <el-select v-model="orderStatus" placeholder="订单状态" clearable size="small" style="width: 100px">
            <el-option label="上线" value="2" size="mini"/>
            <el-option label="未上线" value="1" size="mini"/>
            <el-option label="全部" value="0" size="mini"/>
          </el-select>
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-search"
            style="margin-left: 5px"
            @click="pageSize=30;pageCurrent=1;searchTable()">查询
          </el-button>
          <el-button
            type="danger"
            plain
            size="small"
            icon="el-icon-finished"
            style="margin-left: 5px;float: right"
            @click="resetWorkAll()">一键强制完工
          </el-button>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        size="mini"
        height="740"
        center
        style="width: 100%">
        <el-table-column width="60" label="序号" type="index" align="center"/>
        <el-table-column align="center" label="计划生产时间" width="120">
          <template slot-scope="scope">
            {{ scope.row.计划开始时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="AMS顺序" width="80">
          <template slot-scope="scope">
            {{ scope.row.AMS计划顺序 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="MES顺序" width="80">
          <template slot-scope="scope">
            {{ scope.row.MES计划顺序 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单状态" width="80">
          <template slot-scope="scope">
            <span style="font-weight: 700" :style="{color: scope.row.订单状态==='已上线'?'green':'orange'}">{{ scope.row.订单状态 }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="上线时间" width="150">
          <template slot-scope="scope">
            {{ scope.row.执行时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工件编号" width="100">
          <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="订货号ID" width="80">
          <template slot-scope="scope">
            {{ scope.row.订货号ID }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工单号" width="130">
          <template slot-scope="scope">
            {{ scope.row.工单号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工单ID" width="100">
          <template slot-scope="scope">
            {{ scope.row.工单ID }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="打印编号" width="80">
          <template slot-scope="scope">
            {{ scope.row.传递编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="打印类型" width="110">
          <template slot-scope="scope">
            {{ scope.row.位置名称 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="170">
          <template slot-scope="scope">
            <el-button v-if="scope.row.执行状态 == 1" size="mini" type="text" icon="el-icon-refresh" style="color: #097e00" @click="updateSort(scope.row)">调整</el-button>
            <el-button v-if="scope.row.执行状态 == 2" size="mini" type="text" icon="el-icon-refresh" style="color: #ff9a02" @click="workOver(scope.row)">重新上线</el-button>
            <el-button v-if="scope.row.执行状态 == 1" size="mini" type="text" icon="el-icon-finished" style="color: red" @click="resetWork(scope.row)">强制完工</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[30, 50, 100, 200]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible1" :title="formTitle" center style="min-height: 200px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px">
        <el-form-item label="订货号" prop="订货号" style="display: inline-block">
          <el-input v-model="form.orderCode" clearable size="small" placeholder="订货号" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="位置名称" prop="位置名称" style="display: inline-block">
          <el-select v-model="form.positionCode" placeholder="位置名称" size="small" style="width: 200px">
            <el-option v-for="item in dataType" :label="item.label" :value="item.value" size="mini" />
          </el-select>
        </el-form-item>
        <el-form-item label="小机型号" prop="小机型号" style="display: inline-block">
          <el-input v-model="form.miniPartType" clearable size="small" placeholder="小机型号" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="系列" prop="系列" style="display: inline-block">
          <el-input v-model="form.series" clearable size="small" placeholder="系列" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="类别" prop="类别" style="display: inline-block">
          <el-input v-model="form.category" clearable size="small" placeholder="类别" style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible1 = false">取消</el-button>
        <el-button v-if="formTitle == '增加关联'" type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
        <el-button v-if="formTitle == '修改关联'" type="primary" size="small" @click="submitUpdate('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      dataType: [], // 日志类型数组
      dataTypeValue: '', // 日志类型
      valueType: [],
      valueTypeValue: '',
      parentId: '',
      parentList: [],
      alarmType: [],
      alarmTypeValue: '',
      dialogFormVisible: false,
      dialogFormVisible4: false,
      form4: {
        engineTypeValue: ''
      },
      rules4: { // 订单下发验证
        engineTypeValue: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      title: '',
      form: {
        id:'',
        orderCode:'',  // 订货号
        positionCode:'', // 打印位置
        miniPartType:'', // 小机型
        series:'',  // 系列
        category:''  // 类别
      },
      formTitle:'', // 修改框标题
      rules: { // 表单验证规则
      },
      total: 0, // 总条数
      dateTime: '', // 时间
      startPlanTime: '',
      endPlanTime: '',
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      dialogFormVisible1:false,
      engineId:'',  // 工件编号
      orderCode:'', // 订货号
      orderCodeId:'', // 订货号ID
      engineType:'', // 机型号
      workOrderId:'', // 工单ID
      workOrderCode:'', // 工单号
      orderStatus:'0', // 订单状态
      getOrderInterval:null
    }
  },
  created() {
    var year, month, day, audiTime
    this.date = new Date() // 修改数据date
    year = new Date().getFullYear()
    month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
    day = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
    audiTime = year + '-' + month + '-' + day
    console.log(audiTime, 'audiTime')
    this.dateTime = [audiTime, audiTime]
    // this.getDataType()
    this.searchTable()

    // 定时重新读取数据
    this.getOrderInterval = setInterval(() => {
       this.searchTable()
    }, 1000 * 5)
  },
  beforeDestroy(){
    clearInterval(this.getOrderInterval)
  },
  methods: {
    // 初始化获取日志类型
    // getDataType() {
    //   this.dataType = []
    //   var param = []
    //   var Data = this.CreateData('11', '潍柴_打印位置_基本表_查询', param)
    //   this.ExecDatabase(Data).then(response => {
    //     for (const item of response.data) {
    //       this.dataType.push({
    //         label: item.打印位置,
    //         value: item.传递编号
    //       })
    //     }
    //   })
    // },
    addData(){
      this.dialogFormVisible1 = true
      this.form.orderCode = ''
      this.form.positionCode = ''
      this.form.miniPartType = ''
      this.form.series = ''
      this.form.category = ''
      this.formTitle = '增加关联'
    },
    submitAdd1(){
      if(!this.form.orderCode){
        this.$message.error("请填写订货号后提交！")
        return
      }
      var param = []
      param[0] = ['订货号', this.form.orderCode]
      param[1] = ['传递编号', this.form.positionCode]
      param[2] = ['小机型号', this.form.miniPartType]
      param[3] = ['系列', this.form.series]
      param[4] = ['类别', this.form.category]
      var Data = this.CreateData('11', '潍柴_打印位置_对应表_增加', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result == 1) {
          this.$message.success("添加成功！")
          this.dialogFormVisible1 = false
          this.searchTable()
        }
      })
    },
    submitUpdate(){
      if(!this.form.orderCode ){
        this.$message.error("请填写订货号后提交！")
        return
      }
      var param = []
      param[0] = ['订货号', this.form.orderCode]
      param[1] = ['传递编号', this.form.positionCode]
      param[2] = ['小机型号', this.form.miniPartType]
      param[3] = ['系列', this.form.series]
      param[4] = ['类别', this.form.category]
      param[5] = ['Id', this.form.id]
      var Data = this.CreateData('11', '潍柴_打印位置_对应表_修改', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result == 1) {
          this.$message.success("添加成功！")
          this.dialogFormVisible1 = false
          this.searchTable()
        }
      })
    },
    update(row){
      this.form.id = row.Id
      this.form.orderCode = row.订货号
      this.form.positionCode = row.传递编号
      this.form.miniPartType = row.小机型
      this.form.series = row.系列
      this.form.category = row.类别
      this.dialogFormVisible1 = true
      this.formTitle = '修改关联'
    },
    deleteData(row){
      this.$confirm('此操作将永久删除该打印位置，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', row.id]
        var Data = this.CreateData('11', '潍柴_打印位置_对应表_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == 1) {
            this.$message.success("删除成功！")
            this.searchTable()
          }
        })
      })
    },
    updateSort(row){
      // 弹出提示框 提示输入重新排序到哪个位置 输入框中只能输入数字，点击确认 提交
      this.$prompt('请输入新的排序位置', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[0-9]*$/,
        inputErrorMessage: '请输入数字'
      }).then(({ value }) => {
        var param = []
        param[0] = ['id', row.AID]
        param[1] = ['sort', value]
        var Data = this.CreateData('11', '潍柴_接口_生产计划_执行计划_排序修改', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == 1) {
            this.$message.success("排序成功！")
            this.searchTable()
          }
        })
      })
    },
    workOver(row){
      // 弹出确认框 二次确认 是否重新上线
      this.$confirm('此操作将重新上线该工单，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', row.AID]
        var Data = this.CreateData('11', '潍柴_接口_生产计划_执行计划_重新上线', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == 1) {
            this.$message.success("操作成功！")
            this.searchTable()
          }
        })
      })
    },
    resetWork(row){
      // 弹出确认框 二次确认 是否重新上线
      this.$confirm('此操作将强制下线该工单，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', row.AID]
        var Data = this.CreateData('11', '潍柴_接口_生产计划_执行计划_强制下线', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == 1) {
            this.$message.success("操作成功！")
            this.searchTable()
          }
        })
      })
    },
    resetWorkAll(){
      // 弹出确认框 二次确认 是否重新上线
      this.$confirm('此操作将强制上线所有工单，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', '-99']
        var Data = this.CreateData('11', '潍柴_接口_生产计划_执行计划_强制下线', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == 1) {
            this.$message.success("操作成功！")
            this.searchTable()
          }
        })
      })
    },
    searchTable() {
      let  engineId_check,orderCode_check,orderCodeId_check,engineType_check,workOrderId_check,
        workOrderCode_check,startPlanTime_check,endPlanTime_check
      this.engineId ? engineId_check = 1 : (engineId_check = 0, this.engineId = '')
      this.orderCode ? orderCode_check = 1 : (orderCode_check = 0, this.orderCode = '')
      this.orderCodeId ? orderCodeId_check = 1 : (orderCodeId_check = 0, this.orderCodeId = '')
      this.engineType ? engineType_check = 1 : (engineType_check = 0, this.engineType = '')
      this.workOrderId ? workOrderId_check = 1 : (workOrderId_check = 0, this.workOrderId = '')
      this.workOrderCode ? workOrderCode_check = 1 : (workOrderCode_check = 0, this.workOrderCode = '')
      this.startPlanTime ? startPlanTime_check = 1 : (startPlanTime_check = 0, this.startPlanTime = '')
      this.endPlanTime ? endPlanTime_check = 1 : (endPlanTime_check = 0, this.endPlanTime = '')
      var param = []
      param[0] = ['PageCurrent', this.pageCurrent]
      param[1] = ['PageSize', this.pageSize]
      param[2] = ['PageCount', '1111', 'int', '1']
      param[3] = ['ItemCount', '1111', 'int', '1']
      param[4] = ['计划开始时间_check', startPlanTime_check]
      param[5] = ['计划开始时间', this.startPlanTime]
      param[6] = ['计划完成时间_check', endPlanTime_check]
      param[7] = ['计划完成时间', this.endPlanTime]
      param[8] = ['工件编号_ischeck', engineId_check]
      param[9] = ['工件编号', this.engineId]
      param[10] = ['订货号_ischeck', orderCode_check]
      param[11] = ['订货号', this.orderCode]
      param[12] = ['订货号ID_ischeck', orderCodeId_check]
      param[13] = ['订货号ID', this.orderCodeId]
      param[14] = ['机型号_ischeck', engineType_check]
      param[15] = ['机型号', this.engineType]
      param[16] = ['工单ID_ischeck', workOrderId_check]
      param[17] = ['工单ID', this.workOrderId]
      param[18] = ['工单号_ischeck', workOrderCode_check]
      param[19] = ['工单号', this.workOrderCode]
      param[20] = ['订单状态', this.orderStatus]
      var Data = this.CreateData('11', '潍柴_接口_生产计划_执行计划_查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.result.length !== 0) {
          if(this.tableData.length > 0){
            if(this.tableData[0].AID != response.data.result[0].AID || this.tableData[0].订单状态 != response.data.result[0].订单状态) {
              this.tableData = response.data.result
              this.loading = true
            }
          }else{
            this.tableData = response.data.result
          }
        }else{
          this.tableData = []
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    callOff() {
      this.dialogFormVisible = false
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
.topDiv {
  margin-bottom: 10px;
}
</style>
