<!-- 性诱测报控制 -->
<template>
  <div class=''>
    <div class="equipId">设备ID：{{equipId}}</div>
	<el-form
          ref="equipContrlRef1"
          :model="equipContrlForm1"
          label-position="top"
          label-width="130px"
        >
          <el-form-item label="设备开关：" prop="ds">
            <el-select v-model="equipContrlForm1.ds">
              <el-option label="开机" :value="1"></el-option>
              <el-option label="关机" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时控开关：" prop="timctrl">
            <el-select v-model="equipContrlForm1.timctrl">
              <el-option label="开" :value="1"></el-option>
              <el-option label="关" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时控时长：">
            <div class="timeSet">
              <el-time-select
                placeholder="起始时间"
                v-model="equipContrlForm1.st"
                value="05:00"
                :picker-options="{
                  start: '0:00',
                  step: '1:00',
                  end: '23:00',
                }"
              ></el-time-select>
              <span style="margin: 0 10px">至</span>
              <el-time-select
                placeholder="结束时间"
                v-model="equipContrlForm1.et"
                :value="equipContrlForm1.et"
                :picker-options="{
                  start: '0:00',
                  step: '1:00',
                  end: '23:00',
                }"
              ></el-time-select>
            </div>
          </el-form-item>
          <el-form-item label="数据频率(min)：" prop="dat_f">
            <div class="sliderParent">
              <div class="block">
                <el-slider
                  :min="10"
                  :max="60"
                  v-model="equipContrlForm1.dat_f"
                  show-input
                ></el-slider>
              </div>
            </div>
          </el-form-item>
          <el-form-item class="handAddFormBtn">
            <el-button
              type="blueInfo"
              size="small"
              @click="equipControlSubm(equipContrlForm1)"
              >确定</el-button
            >
            <el-button type="grayInfo" size="small" @click="resetForm()"
              >取消</el-button
            >
          </el-form-item>
        </el-form>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
props: ["equipId", "equipType","d_id","model"],
//import引入的组件需要注入到对象中才能使用
components: {},
data() {
//这里存放数据
return {
  //设备控制I 型
  equipContrlForm1: {
    // st: '', //时控开始时间
    // et: '', //时控结束时间
    // dat_f: null ,//数据上传时间间隔
    // ds:''//开关，1开机，0关机
  },
  //设备控制II 型
  equipContrlForm2: {
    pic_t: "", //定时拍照时刻
    pic_f: "", //定时拍照时间间隔
    dat_t: null, //数据上传时间间隔
    turn_t: null, //定时转动沾虫板
    ds: "", //开关，1开机，0关机
  },
  equipContrlForm22: "{}",
};
},
//监听属性 类似于data概念
computed: {},
//监控data中的数据变化
watch: {},
//方法集合
methods: {
  //设备控制
  EquipControl() {
      this.$axios({
        method: "POST",
        url:"/api/api_gateway?method=forecast.send_control.device_control_info",
        data: this.qs.stringify({ d_id:this.d_id, cmd: "paramconf" }),
      }).then((res) => {
        if (res.data.message == "") {
          if (res.data.data && Object.keys(res.data.data).length > 0) {
            let data = res.data.data;
            let obj = {
              st: data.work_tim.st,
              et: data.work_tim.et,
              timctrl: data.work_tim.timctrl,
              ds: data.power.ds,
              dat_f: data.data_tim.dat_f,
            };
            obj.st =
              obj.st && obj.st < 10 ? "0" + obj.st + ":00" : obj.st + ":00";
            obj.et =
              obj.et && obj.et < 10 ? "0" + obj.et + ":00" : obj.et + ":00";
            if (this.model == 1) {
              this.equipContrlForm1 = obj;
              this.equipContrlForm22 = JSON.stringify(obj)
            } else {
              this.equipContrlForm2 = data;
              this.equipContrlForm22 = JSON.stringify(data)
            }
          }
        }
      });
    },
  equipControlSubm(form) {
      let newForm = Object.assign({}, form); //深拷贝
      if(newForm.st){
        newForm.st=newForm.st.slice(0, 2).charAt(0) != "0"
          ? newForm.st.slice(0, 2)
          : newForm.st.slice(1, 2);
      }
      if( newForm.st){
       newForm.st =newForm.st.slice(0, 2).charAt(0) != "0"
          ? newForm.st.slice(0, 2)
          : newForm.st.slice(1, 2);
      }
      let obj = {
        work_tim: { timctrl: newForm.timctrl, st: newForm.st, et: newForm.et },
        data_tim: { dat_f: newForm.dat_f },
        power: { ds: newForm.ds },
      };
      console.log(obj);
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=forecast.send_control.device_control",
        data: this.qs.stringify({
          device_type_id: 4,
          d_id: this.d_id,
          config: JSON.stringify(obj),
        }),
      }).then((res) => {
        if (res.data.message == "") {
          this.$message.success("设备控制修改成功");
        } else {
          this.$message.error("设备控制修改失败");
        }
      });
    },
  resetForm() {
    if(this.equipContrlForm22){
      this.equipContrlForm1 = JSON.parse(this.equipContrlForm22);
      this.equipContrlForm2 = JSON.parse(this.equipContrlForm22);
    }else{
      this.equipContrlForm1={
        st: '', //时控开始时间
        et: '', //时控结束时间
        dat_f: null ,//数据上传时间间隔
        ds:''//开关，1开机，0关机
      }
      this.equipContrlForm2={
        pic_t: "", //定时拍照时刻
        pic_f: "", //定时拍照时间间隔
        dat_t: null, //数据上传时间间隔
        turn_t: null, //定时转动沾虫板
        ds: "", //开关，1开机，0关机
      }
    }
  }
},
//生命周期 - 创建完成（可以访问当前this实例）
created() {

},
//生命周期 - 挂载完成（可以访问DOM元素）
mounted() {
  this.EquipControl()
},
beforeCreate() {}, //生命周期 - 创建之前
beforeMount() {}, //生命周期 - 挂载之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 销毁之前
destroyed() {}, //生命周期 - 销毁完成
activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped lang="less">
.equipId{
  line-height: 33px;
  text-align: center;
  background: #eee;
}
.handAddFormBtn{
  margin-top: 20px;
  text-align: right;
}
// 时间input框
/deep/.el-date-editor.el-input, 
/deep/.el-date-editor.el-input__inner{
  width: 120px;
}
/deep/.el-slider__input{
  width: 103px;
}
/deep/.el-input-number--small .el-input-number__decrease, /deep/.el-input-number--small .el-input-number__increase{
  width: 26px;
}
/deep/.el-slider__runway.show-input{
  margin-right: 119px;
}
</style>