<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="机器型号">
        <el-select v-model="queryParams.model_id" placeholder="请选择机器型号" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in modelList"
            :key="item.id"
            :label="item.model_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机器编码" >
        <el-input placeholder="请输入机器编码" v-model="queryParams.code" clearable @keyup.enter.native="handleQuery"></el-input>        
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['machine:machineDetails:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="machineList">     
      <el-table-column label="机器编码" align="center">       
        <template slot-scope="scope">
          <a @click="handleLogDialog(scope.row)" style="color: blue">{{ scope.row.code }}</a>
        </template>     
      </el-table-column>
      <el-table-column label="机器型号" align="center" prop="model" />
      <el-table-column label="状态" align="center" prop="state_desc" />
      <el-table-column label="绑定时间" align="center" prop="bind_time" />
      <el-table-column label="组件管理" align="center">
        <template slot-scope="scope">
          <a @click="handleComponentDialog(scope.row)" style="color: blue">管理组件</a>
        </template> 
      </el-table-column>
      <el-table-column label="组件属性监控" align="center">
        <template slot-scope="scope">
          <a @click="handleAttributeDialog(scope.row)" style="color: blue">查看</a>
        </template>
      </el-table-column>
      <el-table-column label="其他设置" align="center">
        <template slot-scope="scope">
          <a @click="handleOtherDialog(scope.row)" style="color: blue">查看</a>
        </template> 
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-hasPermi="['machine:machineDetails:offline']" v-show="scope.row.state == 4 || scope.row.state == 5" size="mini" type="text" @click="handleClick(scope.row,6)">下线</el-button>
          <el-button v-hasPermi="['machine:machineDetails:return']" v-show="scope.row.state == 6 || scope.row.state == 7" size="mini" type="text" @click="handleClick(scope.row,8)">返厂</el-button>
          <el-button v-hasPermi="['machine:machineDetails:operations']" v-show="scope.row.state == 6" size="mini" type="text" @click="handleClick(scope.row,7)">运维</el-button>
          <el-button v-hasPermi="['machine:machineDetails:online']" v-show="scope.row.state == 6 || scope.row.state == 7" size="mini" type="text" @click="handleClick(scope.row,4)">上线</el-button>
          <el-button v-hasPermi="['machine:machineDetails:bindShop']" v-show="scope.row.state == 2" size="mini" type="text" @click="handleBinding()">店铺绑定</el-button>
          <el-button v-hasPermi="['machine:machineDetails:reset']" v-show="scope.row.state == 8" size="mini" type="text" @click="handleClick(scope.row,1)">重置</el-button>
        </template>       
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit"
      @pagination="getList" />
    <!-- 机器日志对话框 -->
   <MachineDetailsLog ref="machineDetailsLog" ></MachineDetailsLog>
    <!-- 组件对话框 -->
   <MachineDetailsCom ref="machineDetailsCom" ></MachineDetailsCom>
    <!-- 其他组件对话框 -->
   <MachineDetailsOther ref="machineDetailsOther" ></MachineDetailsOther>
    <!-- 组件属性监控对话框 -->
   <MachineDetailsAttribute ref="machineDetailsAttribute" ></machineDetailsAttribute>
  </div>
</template>

<script>
import {getMachineList,updateState} from "@/api/machine/machineDetails";
import MachineDetailsLog from './machineDetailsLog.vue';
import MachineDetailsCom from './machineDetailsCom.vue';
import MachineDetailsOther from './machineDetailsOther.vue';
import MachineDetailsAttribute from './machineDetailsAttribute.vue';
import {getModelList} from "@/api/model/model";
import {checkPermi} from "@/utils/permission.js";
export default {
  components: { MachineDetailsLog,MachineDetailsCom,MachineDetailsOther,MachineDetailsAttribute},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      machineList: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        model_id:null       
      },
      modelList:[]
    };
  },
  created() {
    this.getList();
    this.getModelList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getMachineList(this.queryParams).then((response) => {
        if(response.code==0){
          this.machineList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }       
      });
    },
    //获取机器型号
    getModelList(){
      getModelList().then((response) => {
        if(response.code==0){
          this.modelList = response.data;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.model_id==""){
        this.queryParams.model_id=null;
      }
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.model_id=null;
      this.queryParams.code="";
      this.getList();
    }, 
    //日志
    handleLogDialog(row){
      let b=checkPermi(['machine:machineDetails:log']);
      if(!b){
        this.$message.error('暂无日志操作的权限!');
        return;
      } 
      this.$refs.machineDetailsLog.handleLogDialog(row);
    },
    //组件
    handleComponentDialog(row){
      let b=checkPermi(['machine:machineDetails:component']);
      if(!b){
        this.$message.error('暂无管理组件操作的权限!');
        return;
      } 
      this.$refs.machineDetailsCom.handleComDiaLog(row);
    },
    //属性监控
    handleAttributeDialog(row){
      let b=checkPermi(['machine:machineDetails:attribute']);
      if(!b){
        this.$message.error('暂无组件属性监控操作的权限!');
        return;
      } 
      this.$refs.machineDetailsAttribute.handleAttributeDialog(row);
    },
    //其他设置
    handleOtherDialog(row){      
    let b=checkPermi(['machine:machineDetails:other']);
      if(!b){
        this.$message.error('暂无其他设置操作的权限!');
        return;
      }   
    this.$refs.machineDetailsOther.handleOthDiaLog(row);
    },
    //店铺绑定
    handleBinding(){
      this.$router.push("/shop/shop/");
    },
    //更新状态
    handleClick(row,state){
      let obj={
        "machine_id":row.id,
        "state":state
      }
      updateState(obj).then((response) => {
        if(response.code==0){
          this.$modal.msgSuccess("操作成功!");
          this.getList();
        }       
      });
    }
  }
};
</script>

