<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" clearable size="small" style="width: 240px">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="margin-left: 10px">接口名称：</span>
        <el-select v-model="interFaceValue" placeholder="请选择接口名称" clearable size="small" style="width: 200px">
          <el-option
            v-for="item in interFace"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 20px;" @click="getTableData()">查询</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="small" style="margin-left: 20px;" @click="addStationInterface()">增加</el-button>
        <el-button :disabled="multipleSelection.length===0" type="danger" plain icon="el-icon-delete" size="small" style="margin-left: 20px;" @click="deleteStationInterfaceInformationMultiple()">删除</el-button>
        <el-button :disabled="multipleSelection.length===0" type="warning" plain icon="el-icon-remove-outline" size="small" style="margin-left: 20px;" @click="disableStationInterfaceInformationMultiple()">禁用</el-button>
        <el-button :disabled="multipleSelection.length===0" type="success" plain icon="el-icon-circle-check" size="small" style="margin-left: 10px;" @click="enableStationInterfaceInformationMultiple()">启用</el-button>
      </div>
      <div style="margin-top: 5px">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="tableData"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          class="main"
          tooltip-effect="dark"
          height="650px"
          style="width: 70%"
          border
          size="mini"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" align="center" width="55" />
          <el-table-column type="index" width="60" label="序号" align="center"/>
          <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="150">
            <template slot-scope="scope">
              {{ scope.row.工位名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="接口名称" width="220">
            <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="100">
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.启用" @change="submitEdit_StationInterface(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-delete" size="small" style="color: #ff0000" @click="deleteStationInterfaceInformation(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog :visible.sync="dialogFormVisible_AddStationInterface" title="添加接口" center style="min-height: 230px;" width="400px">
        <el-form ref="form_stationInterFace" :model="form_stationInterFace" :rules="rules_stationInterFace" label-position="right" label-width="110px">
          <el-form-item label="工位号" prop="工位号" style="display: inline-block">
              <el-select v-model="form_stationInterFace.工位号" size="small" placeholder="请选择工位号" style="width: 240px">
              <el-option
                v-for="item in stationNumber"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="接口名称" prop="接口名称" style="display: inline-block">
            <el-select v-model="form_stationInterFace.接口名称" size="small" placeholder="请选择接口名称">
              <el-option
                v-for="item in interFace"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="callOff_StationInterface('form_stationInterFace')">取消</el-button>
          <el-button type="primary" size="small" @click="submitAdd_StationInterface('form_stationInterFace')">确定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      multipleSelection: [],
      dialogFormVisible_AddStationInterface: false,
      form_stationInterFace: {
        工位号: '',
        接口名称: ''
      },
      rules_stationInterFace: {
        工位号: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        接口名称: [{ required: true, message: '请选择接口名称', trigger: 'change' }]
      },
      interFaceValue: '',
      interFace: [],
      stationNumberValue: '',
      stationNumber: [],
      loading: false,
      tableData: [],
      tableDataParams: [],
      expandsDetail: [],
      expandedRows: null
    }
  },
  created() {
    this.getInterFace()
    this.getStationNumber()
    this.getTableData()
  },
  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].工位号}】${response.data[i].工位名称}`,
            value: response.data[i].工位号
          })
        }
      })
    },
    getInterFace() {
      this.interFace = []
      var param = []
      var Data = this.CreateData('11', 'MOM_基础数据_接口列表_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.interFace.push({
            label: response.data[i].接口名称,
            value: response.data[i].接口名称
          })
        }
      })
    },
    getTableData() {
      this.loading = true
      var param = []
      param.push(['工位号', this.stationNumberValue])
      param.push(['接口名称', this.interFaceValue])
      var Data = this.CreateData('11', 'MOM_基础数据_接口列表_工位_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = []
        if (response.data.length !== 0) {
          this.tableData = response.data
          // 是否启用 0/1 转成bool类型
          this.tableData = this.tableData.map(item => ({
            ...item,
            启用: item['是否启用'] === 1
          }))
          console.log(this.tableData)
        }
        this.loading = false
      }).catch(error => {
        this.tableData = []
        this.loading = false
        this.$message.error('数据加载失败，请稍后重试！')
        console.error('Error fetching interface data:', error)
      })
    },
    // **************************************************************
    addStationInterface() {
      this.form_stationInterFace.接口名称 = ''
      this.form_stationInterFace.工位号 = ''
      this.dialogFormVisible_AddStationInterface = true
    },
    submitAdd_StationInterface(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          var param = []
          param[0] = ['接口名称', this.form_stationInterFace.接口名称]
          param[1] = ['工位号', this.form_stationInterFace.工位号]
          var Data = this.CreateData('12', 'MOM_基础数据_接口列表_工位_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.getTableData()
            } else {
              this.$message.error('添加失败！')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    submitEdit_StationInterface(row) {
      var param = []
      param[0] = ['接口名称', row.接口名称]
      param[1] = ['工位号', row.工位号]
      param[2] = ['是否启用', row.启用 ? 1 : 0]
      var Data = this.CreateData('12', 'MOM_基础数据_接口列表_工位_编辑_是否启用', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getTableData()
        } else {
          this.$message.error('修改失败！')
        }
      })
    },
    callOff_StationInterface(form) {
      this.dialogFormVisible_AddStationInterface = false
      this.$refs[form].resetFields()
    },
    deleteStationInterfaceInformation(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['工位号', row['工位号']]
        param[1] = ['接口名称', row['接口名称']]
        var Data = this.CreateData('12', 'MOM_基础数据_接口列表_工位_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getTableData()
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteStationInterfaceInformationMultiple() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择需要删除的接口！')
        return
      }
      let num = 0
      for (let i = 0; i < this.multipleSelection.length; i++) {
        const row = this.multipleSelection[i]
        var param = []
        param[0] = ['工位号', row['工位号']]
        param[1] = ['接口名称', row['接口名称']]
        var Data = this.CreateData('12', 'MOM_基础数据_接口列表_工位_删除', param)
        this.ExecDatabase(Data).then(response => {
          num++
          if (num === this.multipleSelection.length) {
            this.$message.warning('删除成功')
            this.getTableData()
          }
        })
      }
    },
    disableStationInterfaceInformationMultiple() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择需要禁用的接口！')
        return
      }
      let num = 0
      for (let i = 0; i < this.multipleSelection.length; i++) {
        const row = this.multipleSelection[i]
        var param = []
        param[0] = ['工位号', row['工位号']]
        param[1] = ['接口名称', row['接口名称']]
        param[2] = ['是否启用', 0]
        var Data = this.CreateData('12', 'MOM_基础数据_接口列表_工位_编辑_是否启用', param)
        this.ExecDatabase(Data).then(response => {
          num++
          if (num === this.multipleSelection.length) {
            this.$message.success('禁用成功')
            this.getTableData()
          }
        })
      }
    },
    enableStationInterfaceInformationMultiple() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择需要启用的接口！')
        return
      }
      let num = 0
      for (let i = 0; i < this.multipleSelection.length; i++) {
        const row = this.multipleSelection[i]
        var param = []
        param[0] = ['工位号', row['工位号']]
        param[1] = ['接口名称', row['接口名称']]
        param[2] = ['是否启用', 1]
        var Data = this.CreateData('12', 'MOM_基础数据_接口列表_工位_编辑_是否启用', param)
        this.ExecDatabase(Data).then(response => {
          num++
          if (num === this.multipleSelection.length) {
            this.$message.success('启用成功')
            this.getTableData()
          }
        })
      }
    }
  }
}
</script>

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