<template>
  <div class="app-container home">
    <el-button type="primary" :icon="Download" @click="drawerIshow = true">策略导入</el-button>
    <div class="dev_control_wrapper">
      <div class="dev_control_item" v-for="item in deviceArr" :key="item.deviceId">
        <div class="item_header">
          <span>{{ item.deviceName}}</span>
<!--          <el-switch v-model="item.deviceId" class="switch" @change="handleChange(item)" style="&#45;&#45;el-switch-on-color: #13ce66; &#45;&#45;el-switch-off-color: #dddddd"/>-->
          <el-switch v-model="item.deviceId" class="switch" style="--el-switch-on-color: #13ce66; --el-switch-off-color: #dddddd"/>
        </div>
        <div class="item_bottom">
          <div class="item_bottom_img">
            <img :src="item.photoUrl" alt="">
          </div>
          <div class="item_bottom_form">
            <el-form :model="form1">
              <el-form-item label="当前状态："><span :class="{'green':item.deviceRunstatus === '开启','red':item.deviceRunstatus === '关闭'}">{{ item.deviceRunstatus }}</span></el-form-item>
              <el-form-item v-show="isShow1" label="进水温度："><el-input v-model="form1.intemp" /></el-form-item>
              <el-form-item v-show="isShow1" label="出水温度："><el-input v-model="form1.outtemp" /></el-form-item>
<!--              <el-form-item label="当前温度："><el-input v-model="form1.now" /></el-form-item>-->
              <div class="submit" @click="getSetTemperatureFun(item)">确定</div>
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <div class="dev_control_table">
      <div class="control_header"><span>调控日志</span></div>
      <div class="control_table">
        <el-table :data="controlLogList" height="305px">
          <el-table-column label="序号" align="center" width="50"/>
          <el-table-column label="设备名称" align="center" prop="deviceName"/>
          <el-table-column label="操作内容" align="center" prop="controlValue" width="700"/>
          <el-table-column label="操作时间" align="center" prop="createTime"/>
          <el-table-column label="操作人员" align="center" prop="createBy"/>
          <el-table-column label="操作结果" align="center" prop="controlResult"></el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[5, 10, 20, 50]"
            :small="small"
            :disabled="disabled"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableLength"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
      </div>
    </div>

    <el-drawer v-model="drawerIshow">
      <template #header><h4>策略导入</h4></template>
      <template #default>
          <el-radio v-for="(item,index) in celueList" :key="index" class="celueBox" v-model="radio" :label="item.sceneName" size="large">
          <span style="font-weight: bold; display: inline-block; margin-bottom: 20px;">{{ item.sceneName }}</span>
          <div class="celueItem" v-for="items in item.data" :key="items.device_id">
            <span>{{ items.device_name }}</span>
            <div class="itemsValue">
              <span class="txtInput" v-for="itemsValue in items.value" :key="itemsValue.sceneKey">
                <span class="txt">{{ itemsValue.sceneContext }}：</span>
                <input type="text" :value="itemsValue.sceneValue" disabled/>
              </span>
            </div>
          </div>
        </el-radio>
      </template>
      <template #footer>
        <div style="flex: auto">
          <el-button type="primary" @click="confirmClick">执行</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Index">
import { Download } from '@element-plus/icons-vue'  //element字体图标
import { ref,reactive,onMounted } from 'vue'
import { ElMessageBox } from 'element-plus'
import { getDevice,getControlLogList,getStrategyList,getStartAndStop,getSetTemperature } from '@/api/control/manual'

onMounted(()=>{
  getDeviceFun();
  getControlLogListFun();
  getStrategyListFun()
})

/** 获取所有设备*/
const deviceArr = ref([])
const isShow1 = ref(true)
function getDeviceFun(){
  getDevice().then((res)=>{
    console.log(res.data)
    deviceArr.value = res.data
    for(let i = 0;i < res.data.length;i++){
      if(res.data[i].deviceRunstatus == 1){
        res.data[i].deviceRunstatus = '开启'
      }else{
        res.data[i].deviceRunstatus = '关闭'
      }

      // if(res.data[i].deviceModel == '发热电缆'){
      //   isShow1.value = false
      // }
    }
  })
}

/** 设备控制处理*/
const form1 = reactive({
  intemp:'',
  outtemp:'',
  now:''
})

/** 数据表格处理*/
const controlLogList = ref([])
const tableLength = ref()
const currentPage = ref(1)
const pageSize = ref(5)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const handleSizeChange = (val) => {
  getControlLogListFun()
}
const handleCurrentChange = (val) => {
  getControlLogListFun()
}

function getControlLogListFun(){
  getControlLogList({
    "deviceId": null,
    "deviceName": null,
    "deviceSn": null,
    "controlKey": null,
    "controlValue": null,
    "controlResult": null,
    "controlContext": null,
    "pageNum": currentPage.value,
    "pageSize": pageSize.value
  }).then((res)=>{
    tableLength.value = res.total
    controlLogList.value = res.rows
    for(let i = 0;i < res.rows.length;i++){
      if(res.rows[i].controlResult == 1){
        res.rows[i].controlResult = '控制中'
      }else if(res.rows[i].controlResult == 2){
        res.rows[i].controlResult = '控制成功'
      }else if(res.rows[i].controlResult == 3){
        res.rows[i].controlResult = '控制失败'
      }
    }
  })
}


/** 策略导入处理*/
const drawerIshow = ref(false)
const radio = ref()
const celueList = ref([])
function getStrategyListFun(){
  getStrategyList().then((res)=>{
    celueList.value = res.data
    console.log(res.data)
  })
}
function confirmClick() {
  ElMessageBox.confirm(`确定执行 ${radio.value} ?`).then(() => {
    drawerIshow.value = false;
    console.log('确定')
  })
}

/** 控制设备启停*/
// function handleChange(item){
//   console.log(item)
//   // getStartAndStop({
//   //   "deviceId": 100,
//   //   "runStatus": 2
//   // }).then((res)=>{
//   //   console.log(res.data)
//   // })
// }

/** 设置温度*/
function getSetTemperatureFun(item){
  console.log(item)
  // getSetTemperature({
  //   "deviceId":100,
  //   "data":[
  //     {
  //       "controlKey":"intemp",
  //       "controlValue":"28"
  //     },
  //     {
  //       "controlKey":"outtemp",
  //       "controlValue":"90"
  //     }]
  // }).then((res)=>{
  //   console.log(res)
  // })
}

</script>

<style scoped lang="scss">
.home {
  height: calc(100vh - 118px);
  .dev_control_wrapper{
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    width: 100%;
    height: 50%;
    overflow-y: auto;
    .dev_control_item{
      width: 24%;
      height: 47%;
      margin-right: 12px;
      margin-bottom: 12px;
      background-color: #2F3D8A;
      overflow: hidden;
      &:nth-child(4n){
        margin-right: 0px;
      }

      .item_header{
        width: 100%;
        height: 20%;
        background-color: #374590;
        padding: 0 10px;
        span{
          font-size: 16px;
          font-weight: bold;
          line-height: 37px;
        }
        .switch{
          float: right;
          line-height: 37px;
          height: 37px;
        }
      }
      .item_bottom{
        display: flex;
        width: 100%;
        height: 80%;
        .item_bottom_img{
          width: 40%;
          height: 100%;
          //border: 1px solid red;
          padding: 10px;
          img{
            width: 100%;
            height: 100%;
          }
        }
        .item_bottom_form{
          width: 60%;
          height: 100%;
          //border: 1px solid red;
          padding: 0 10px 0 0;
          .green{
            color: #14FF00;
          }
          .red{
            color: red;
          }
          .submit{
            padding: 3px 15px;
            border-radius: 2px;
            background-color: #2F8EED;
            float: right;
            margin-top: 8px;
            cursor: pointer;
            &:hover{
              background-color: #5ba2f3;
            }
          }
        }
      }
    }
  }
  .dev_control_table{
    width: 100%;
    height: 50%;
    background-color: #2F3D8A;
    .control_header{
      width: 100%;
      height: 10%;
      background-color: #374590;
      padding: 0 10px;
      span{
        font-size: 16px;
        font-weight: bold;
        line-height: 37px;
      }
    }
    .control_table{
      position: relative;
      width: 100%;
      height: 90%;
    }
  }
}

.celueBox{
  width: 100%;
  height: auto;
  padding: 10px;
  margin-bottom: 12px;
  border: 1px solid #ddd;
  &:hover{
    border: 1px solid #409EFF;
  }
  .celueItem{
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    padding: 10px 0;
    border-bottom: 1px solid #dddddd50;
    .itemsValue{
      display: flex;
      justify-content: flex-start;
      flex-wrap: nowrap;
      width: 80%;
      .txtInput{
        margin-right: 10px;
        .txt{
          display: inline-block;
          width: 80px;
          text-align: right;
        }
        input{
          padding: 3px 3px;
          width: 100px;
          border: 1px solid #ddd;
        }
      }
    }
  }
}


.success {
  color: green;
}

.failure {
  color: red;
}

::v-deep .el-button{
  border-radius: 2px;
  margin-bottom: 12px;
}
::v-deep .el-switch__core .el-switch__action{
  top: 1px;
  width: 17px;
  height: 17px;
}
::v-deep .el-form-item--default{
  margin-bottom: 3px;
}
::v-deep .el-input__inner{
  color: #f3ffff;
}
::v-deep .el-pagination{
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
}
::v-deep .el-pagination__total{
  color: #f3ffff;
}
::v-deep .el-pagination__jump{
  color: #f3ffff;
}
::v-deep .el-pager{
  margin: 0 5px;
}
::v-deep .el-table--fit{
  background-color: #2D3D88;
}
::v-deep .el-radio{
  display: inline-block;
}
::v-deep .el-drawer__body{
  overflow-x: hidden;
  overflow-y: auto;
}
</style>

