<template>
  <div style="margin-top:10px">
    <el-button type="primary" icon="el-icon-s-tools" round @click="dialogTableVisible = true">远程固件升级</el-button>
    <el-dialog title="配件升级" :visible.sync="dialogTableVisible" :before-close="handleClose">
      <el-table
        :data="firmwareList"
        border
        stripe
        ref="firmwareList"
        >
        <el-table-column width="80" label="选择" @row-click="singleElection">
            <template slot-scope="scope">
                <el-radio class="radio" v-model="templateRadioId" :label="scope.$index+1">
                  <p v-html>&nbsp;</p>
                </el-radio>
            </template>
        </el-table-column>
        <el-table-column prop="name" label="固件名称"></el-table-column>
        <el-table-column prop="description" label="固件描述"></el-table-column>
        <el-table-column prop="filePath" label="路径"></el-table-column>
      </el-table>  
      <div style="margin-top: 20px">
        <el-button type="primary" @click="submit">提交</el-button>
        <!-- <el-button @click="">取消选择</el-button> -->
      </div>
    </el-dialog>  
  </div>
</template>

<script>
import { listFirmware } from "@/api/pile/firmware";
import {updateFirmware} from "@/api/pile/pileRemote";
export default {
  props:['pileSn'],
  data() {
    return {
      templateSelection: '',
      templateRadioId: null,
      dialogTableVisible: false,
       queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
            },
        multipleSelection: [],
        firmwareList:[]
    };
  },
  methods: {
    submit(){
      console.log('this.templateRadioId',this.templateRadioId)
      if(this.templateRadioId === null) return this.$modal.msgWarning('请选择文件')
      //  console.log('接收传递的 pileSn',this.pileSn,this.templateRadioId);
      const data = {
        pileSns: [this.pileSn],
        firmwareId: this.firmwareList[this.templateRadioId-1].id
      };
      console.log("远程升级固件:", data);
      updateFirmware(data).then((response) => {
        console.log("updateFirmware结果：", response);
        if(response.code === 200) {
          this.$modal.msgSuccess('升级成功')
          this.dialogTableVisible = false
        }
      });
    },
      getList() {
            this.loading = true;
            listFirmware(this.queryParams).then(response => {
              console.log('查询固件接口 firmwareList',response)
                this.firmwareList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
      // 选择单选框的点击事件
      singleElection (row) {
        this.templateSelection = this.firmwareList.indexOf(row);
        console.log('this.templateSelection',this.templateSelection)
        this.templateRadioId = row.id;
        console.log(this.templateRadioId,'this.templateRadioId');
      },
      handleClose(){
         this.dialogTableVisible = false
        this.templateRadioId = ''
      }
  },
  created() {
    // this.getList()
  }
}
</script>

<style>

</style>