<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" clearable style="width: 150px">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <el-button
          icon="el-icon-search"
          plain
          size="small"
          style="margin-left: 5px"
          type="primary"
          @click="searchTable()">查询
        </el-button>
        <el-button icon="el-icon-circle-plus-outline" size="small" type="distribution" @click="addTo">增加</el-button>
<!--        <el-button icon="el-icon-circle-plus-outline" size="small" type="distribution" @click="addErrorStopWork">-->
<!--          故障停机记录-->
<!--        </el-button>-->
        <el-button type="info" plain size="small" icon="el-icon-download" @click="exportExcel()">导出</el-button>
<!--        <el-button icon="el-icon-time" size="small" type="primary" @click="showErrorStopWork">-->
<!--          故障停机记录查询-->
<!--        </el-button>-->
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        center
        height="740"
        highlight-current-row
        size="mini"
        style="width: 50%">
        <el-table-column width="80" 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="物料排序" width="90">
          <template slot-scope="scope">
            {{ scope.row.物料排序 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="物料编号" width="90">
          <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="创建时间" width="170">
          <template slot-scope="scope">
            {{ scope.row.操作时间 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="updateValue(scope.row)" >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :visible.sync="dialogFormVisible"
      center
      style="min-height: 200px;"
      :title="'物料厂家' + (tableObject.ID == 0 ? '增加' : '修改')"
      width="400px">
      <el-form ref="form1" :model="tableObject" label-position="right" label-width="110px">
        <el-form-item label="工位号:" prop="number" style="display: inline-block">
          <el-select v-model="tableObject.opName" placeholder="请选择工位号" size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="物料排序:" prop="number" style="display: inline-block">
          <el-input-number v-model="tableObject.sort" :min="0" label="物料排序"/>
        </el-form-item>
        <el-form-item label="物料编号:" prop="number" style="display: inline-block">
          <el-input-number v-model="tableObject.code" :min="0" label="物料编号"/>
        </el-form-item>
        <el-form-item label="物料厂家:" prop="number" style="display: inline-block">
          <el-input v-model="tableObject.factory"  label="物料厂家"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="this.tableObject.ID != 0" size="small" type="primary" @click="submitUpdate()">确定</el-button>
        <el-button v-else size="small" type="primary" @click="submitAdd()">确定</el-button>
      </div>
    </el-dialog>
    <!--// 停机结束弹出框-->
    <el-dialog
      :append-to-body="true"
      :visible.sync="isShowFaultEnd"
      center
      style="text-align: center"
      title="停机结束"
      width="500px">
      <div style="text-align: center">
        <el-form
          ref="faultStartFormRef"
          label-position="left"
          label-width="130px"
          style="width: 100%;margin: auto"
        >
          <el-form-item label="工位号" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-select
              v-model="faultEndForm.opName"
              placeholder="请选择工位号"
              size="small">
              <el-option
                v-for="item in stationNumber"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                size="mini"/>
            </el-select>
          </el-form-item>
          <el-form-item label="故障开始时间" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-date-picker
              v-model="faultEndForm.startTime"
              placeholder="请选择故障开始时间"
              size="small"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="faultStartEndTimeChange()"/>
          </el-form-item>
          <el-form-item label="故障结束时间" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-date-picker
              v-model="faultEndForm.endTime"
              placeholder="请选择故障结束时间"
              size="small"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="faultStartEndTimeChange()"/>
          </el-form-item>
          <el-form-item label="停机时长(秒)" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-input-number
              v-model="faultEndForm.stopTIme"
              :max="9999999999999999999999"
              :precision="0"
              :step="1"
              size="small"
              style="width:200px;"/>
          </el-form-item>
          <el-form-item label="停机时长" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-input v-model="faultEndForm.stopTimeStr" placeholder="停机时长" size="small" style="width:200px;"/>
          </el-form-item>
          <el-form-item label="故障处理人工号" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-input
              v-model="faultEndForm.handlinger"
              placeholder="请输入故障处理人"
              size="small"
              style="width:200px;"/>
          </el-form-item>
          <el-form-item label="停机原因" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-input
              v-model="faultEndForm.stopCause"
              :rows="2"
              placeholder="请输入停机原因"
              style="height: 42px;width: 200px"
              type="textarea"/>
          </el-form-item>
          <el-form-item label="故障停机类型" prop="engineTypeValue" style="display: inline-block;width: 60%">
            <el-select
              v-model="faultEndForm.faultType"
              placeholder="请选择故障类型"
              size="small">
              <el-option
                v-for="item in faultTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                size="mini"/>
            </el-select>
          </el-form-item>
          <!--底部按钮-->
          <div style="text-align: center; margin-top: 20px">
            <el-button class="el-icon-check" type="primary" @click="faultEndSubmit()">确定</el-button>
            <el-button class="el-icon-close" style="margin-left: 50px" @click="isShowFaultEnd = false">关闭</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      expands: [],
      expandsDetail: [],
      dataType: [], // 日志类型数组
      dataTypeValue: '1', // 班次类型
      valueType: [],
      valueTypeValue: '',
      alarmType: [],
      alarmTypeValue: '',
      dateTime: '', // 时间
      passTableData: [],
      faultEndTable:[],
      dialogFormVisible: false,
      planStopTimeFrom: {},
      faultEndForm: {
        opName: '',
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        handlinger: '', // 故障处理人
        stopTIme: '', // 停机时间
        stopCause: '', // 停机原因
        stopTimeStr: '', // 停机时间中文
        faultType: '',
        stationName: ''
      },
      tableObject:{
        ID:0,
        opName:'',
        sort:1,
        code:1,
        factory:''
      },
      stationNumber: [], // 工位号
      stationNumberValue: '',
      faultTypeList: [
        {
          value: '1',
          label: '生产停机'
        },
        {
          value: '2',
          label: '质量停机'
        },
        {
          value: '3',
          label: '设备停机'
        },
        {
          value: '4',
          label: '物流停机'
        }
      ], // 故障类型列表
      isShowFaultEnd: false,
      // 显示故障结束表格
      isShowFaultEndTable : false
    }
  },
  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
    this.searchTable()
    this.getStationNumber()
  },
  methods: {// 工位号
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 获取展开列的工单号
    getRowKeys(row) {
      return row.工位号
    },
    searchTable() {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', '合力_HMI物料厂家_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.loading = false
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        var param = []
        param[0] = ['工位号', this.stationNumberValue]
        var Data = this.CreateData('2001', '合力_HMI物料厂家_查询', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    // 故障结束提交
    faultEndSubmit() {
      var param = []
      param[0] = ['工位号', this.faultEndForm.opName]
      param[1] = ['故障处理人', this.faultEndForm.handlinger]
      param[2] = ['结束时间', this.faultEndForm.endTime]
      param[3] = ['开始时间', this.faultEndForm.startTime]
      param[4] = ['停机时长', this.faultEndForm.stopTIme]
      param[5] = ['停机时长统计', this.faultEndForm.stopTimeStr]
      param[6] = ['停机原因', this.faultEndForm.stopCause]
      param[7] = ['停机类型', this.faultEndForm.faultType]
      var Data = this.CreateData('12', '合力_停机记录_结束', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message({
            message: '停机结束提交成功',
            type: 'success'
          })
          // 查询更新后的功能按钮列表
          this.searchTable()
          this.isShowFaultEnd = false
        } else {
          this.$message({
            message: '停机结束提交失败',
            type: 'error'
          })
        }
      })
    },
    // 异常停机记录
    addErrorStopWork() {
      this.isShowFaultEnd = true
    },
    showErrorStopWork(){
      var param = []
      var Data = this.CreateData('11', '合力_停机记录_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.faultEndTable = response.data
      })
      this.isShowFaultEndTable = true
    },
    addTo() {
      this.tableObject.ID = 0
      this.tableObject.opName = ''
      this.tableObject.sort = 1
      this.tableObject.code = 1
      this.tableObject.factory = ''

      this.dialogFormVisible = true
    },
    updateValue(row){
      this.tableObject.ID = row.ID
      this.tableObject.opName = row.工位号
      this.tableObject.sort = row.物料排序
      this.tableObject.code = row.物料编号
      this.tableObject.factory = row.物料厂家

      this.dialogFormVisible = true
    },
    submitAdd() {
      // 判断是否为空
      if (this.tableObject.opName == '') {
        this.$message.error('工位号不能为空！')
        return
      }
      if (this.tableObject.sort == '') {
        this.$message.error('物料排序不能为空！')
        return
      }
      if (this.tableObject.code == '') {
        this.$message.error('物料编号不能为空！')
        return
      }
      if (this.tableObject.factory == '') {
        this.$message.error('物料厂家不能为空！')
        return
      }
      var param = []
      param.push(['工位号',this.tableObject.opName])
      param.push(['物料排序',this.tableObject.sort])
      param.push(['物料编号',this.tableObject.code])
      param.push(['物料厂家',this.tableObject.factory])
      var Data = this.CreateData('12', '合力_HMI物料厂家_增加', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result == 1) {
          this.$message.success('增加成功！')
          this.searchTable()
          this.dialogFormVisible = false
        } else {
          this.$message.error('增加失败！')
        }
      })
    },
    submitUpdate() {
      // 判断是否为空
      if (this.tableObject.opName == '') {
        this.$message.error('工位号不能为空！')
        return
      }
      if (this.tableObject.sort == '') {
        this.$message.error('物料排序不能为空！')
        return
      }
      if (this.tableObject.code == '') {
        this.$message.error('物料编号不能为空！')
        return
      }
      if (this.tableObject.factory == '') {
        this.$message.error('物料厂家不能为空！')
        return
      }
      var param = []
      param.push(['ID',this.tableObject.ID])
      param.push(['工位号',this.tableObject.opName])
      param.push(['物料排序',this.tableObject.sort])
      param.push(['物料编号',this.tableObject.code])
      param.push(['物料厂家',this.tableObject.factory])
      var Data = this.CreateData('12', '合力_HMI物料厂家_修改', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result == 1) {
          this.$message.success('修改成功！')
          this.searchTable()
          this.dialogFormVisible = false
        } else {
          this.$message.error('修改失败！')
        }
      })
    }
  }
}
</script>

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