<template>
  <view class="device-detail">
    <view class="device-detail__header">
      <u-icon
        size="36"
        class="arrow-left"
        name="arrow-left"
        @click="handleBack"
      ></u-icon>
      {{ title }}
    </view>
    <view class="device-detail__body">
      <view class="tabs">
        <view class="tab-container">
          <view class="tab-item" v-if="myuser_type" :class="activeTab === 'pestAnalysis'?'active':''" @click="handleTabClick('pestAnalysis')">
            管理员
          </view>
          <view class="tab-item" :class="activeTab === 'viewImage'?'active':''" @click="handleTabClick('viewImage')">
            设置
          </view>
        </view>
      </view>
      <view class="device-detail-content" v-if="activeTab === 'pestAnalysis'">
        <view class="device-detail-item">
          <text class="device-detail-label">联网模块</text>
          <view class="device-detail-btn-container">
            <view class="device-detail-btn" @click="setDeviceContorl('dtu_update')">升级</view>
            <view class="device-detail-btn" @click="setDeviceContorl('dtu_reboot')">重启</view>
            <!-- <view class="device-detail-btn" @click="showMqttConfig">MQTT配置</view> -->
          </view>
        </view>
        <view class="device-detail-item">
          <text class="device-detail-label">板子设置</text>
          <view class="device-detail-btn-container">
            <!-- <view class="device-detail-btn">查看原始IMEI</view> -->
            <view class="device-detail-btn" @click="showMqttConfig('imei')">更改IMEI</view>
          </view>
        </view>
        <view class="device-detail-item">
          <text class="device-detail-label">强制操作</text>
          <view class="device-detail-btn-container">
            <view class="device-detail-btn force-btn" @click="setDeviceContorl('takephoto')">拍照</view>
            <view class="device-detail-btn force-btn" @click="setDeviceContorl('update')">升级</view>
            <view class="device-detail-btn force-btn" @click="setDeviceContorl('reboot')">重启</view>
            <view class="device-detail-btn force-btn" @click="setDeviceContorl('open_shake')">震动开启</view>
            <view class="device-detail-btn force-btn" @click="setDeviceContorl('close_shake')">震动关闭</view>
          </view>
        </view>
      </view>
      <view class="device-detail-content" style="padding-bottom: 50rpx" v-if="activeTab === 'viewImage'">
        <view class="device-detail-viewImage">
          <text class="device-detail-label">设备开关:</text>
          <u-switch v-model="equipContrlForm.ds" size="40" active-color="#0BBC58" active-value="1" inactive-value="0"></u-switch>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">定时模式</text>
        </view>
        <u-radio-group v-model="equipContrlForm.ts" @change="radioGroupChange" active-color="#0BBC58">
          <u-radio 
            :name="0"
          >光控
          </u-radio>
          <u-radio 
            :name="1"
          >时控
          </u-radio>
        </u-radio-group>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">定时时长(h){{ equipContrlForm.tt }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.tt" class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">1</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'tt', 1, 10)"
            @touchmove.stop.prevent="onSliderTouchMove($event, 1, 10)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'tt', 1, 10)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.tt, 1, 10) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.tt, 1, 10) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">10</view>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">落虫时间(min){{ equipContrlForm.collt }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.collt" class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">1</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'collt', 1, 20)"
            @touchmove.stop.prevent="onSliderTouchMove($event, 1, 20)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'collt', 1, 20)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.collt, 1, 20) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.collt, 1, 20) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">20</view>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">加热时间(min){{ equipContrlForm.htim }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.htim" class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">1</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'htim', 1, 20)"
            @touchmove.stop.prevent="onSliderTouchMove($event, 1, 20)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'htim', 1, 20)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.htim, 1, 20) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.htim, 1, 20) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">20</view>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">加热温度(℃){{ equipContrlForm.hst }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.hst"  class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">75</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'hst', 75, 120)"
            @touchmove.stop.prevent="onSliderTouchMove($event, 75, 120)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'hst', 75, 120)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.hst, 75, 120) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.hst, 75, 120) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">120</view>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">高温保护阀值(℃){{ equipContrlForm.tph }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.tph" class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">50</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'tph', 50, 70)"
            @touchmove.stop.prevent="onSliderTouchMove($event, 50, 70)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'tph', 50, 70)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.tph, 50, 70) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.tph, 50, 70) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">70</view>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">低温保护阀值(℃){{ equipContrlForm.tpl }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.tpl" class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">-30</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'tpl', -30, 20)"
            @touchmove.stop.prevent="onSliderTouchMove($event, -30, 20)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'tpl', -30, 20)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.tpl, -30, 20) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.tpl, -30, 20) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">20</view>
        </view>
        <view class="device-detail-viewImage">
          <text class="device-detail-label">数据上传间隔(min){{ equipContrlForm.datt }}</text>
        </view>
        <!-- <view class="slider-container">
          <view class="slider-min-value">0</view>
          <u-slider v-model="equipContrlForm.datt" class="slider" max="100" min="0" active-color="#0BBC58"></u-slider>
          <view class="slider-max-value">100</view>
        </view> -->
        <view class="slider-container">
          <view class="slider-min-value">10</view>
          <view
            class="custom-progress"
            @touchstart.stop="onSliderTouchStart($event, 'datt', 10, 60)"
            @touchmove.stop.prevent="onSliderTouchMove($event, 10, 60)"
            @touchend.stop="onSliderTouchEnd"
            @tap.stop="onSliderTap($event, 'datt', 10, 60)"
          >
            <view class="progress-track">
              <view class="progress-fill" :style="{ width: getProgressWidth(equipContrlForm.datt, 10, 60) + '%' }"></view>
              <view class="progress-thumb" :style="{ left: getProgressWidth(equipContrlForm.datt, 10, 60) + '%' }"></view>
            </view>
          </view>
          <view class="slider-max-value">60</view>
        </view>
      </view>
    </view>
    <view class="device-detail-btn-footer" v-if="activeTab === 'viewImage'">
      <view class="device-detail-btn" @click="saveSettings">保存</view>
    </view>
    <u-popup v-model="show" mode="bottom">
      <view class="device-detail-content" style="height: 50vh">
        <view class="device-detail-item">
          <text class="device-detail-label">请输入imei</text>
          <u-input v-model="equipContrlForm.imei" placeholder="请输入imei"></u-input>
        </view>
      </view>
      <view class="device-detail-btn-footer">
        <view class="device-detail-btn" @click="setDeviceContorl('imei')">保存</view>
      </view>
		</u-popup>
  </view>
</template>
<script>

export default {
  data(){
    return {
      value: 10,
      imageList: [],
      equipContrlForm: {},
      time: '',
      activeTab: 'pestAnalysis',
      title: '设置控制',
      myuser_type: false,
      deviceType: '',
      location: '',
      d_id: '',
      show: false,
      checked: false
    }
  },
  onLoad(options){
    this.d_id = options.d_id
    this.getControlDeviceConfigInfo()
    uni.getStorage({
      key:"myuser_type",
      success:(res)=>{
        if(Number(res.data) == 1){
          this.myuser_type = true
          this.activeTab = 'pestAnalysis'
        }else{
          this.activeTab = 'viewImage'
        }
      }
    })
  },
  methods: {
    getProgressWidth(value, min, max) {
      if (max === min) return 0
      return ((value - min) / (max - min)) * 100
    },
    onSliderTouchStart(e, field, min, max) {
      this.sliderField = field
      this.sliderMin = min
      this.sliderMax = max
      const query = uni.createSelectorQuery().in(this)
      query.select('.custom-progress').boundingClientRect(rect => {
        this.sliderRect = rect
      }).exec()
      this.updateSliderValue(e.touches[0].clientX, field, min, max)
    },
    onSliderTouchMove(e, min, max) {
      if (!this.sliderField || !this.sliderRect) return
      this.updateSliderValue(e.touches[0].clientX, this.sliderField, min, max)
    },
    onSliderTouchEnd() {
      this.sliderField = ''
    },
    onSliderTap(e, field, min, max) {
      const query = uni.createSelectorQuery().in(this)
      query.select('.custom-progress').boundingClientRect(rect => {
        this.sliderRect = rect
        this.updateSliderValue(e.detail.x + rect.left, field, min, max)
      }).exec()
    },
    updateSliderValue(clientX, field, min, max) {
      if (!this.sliderRect) return
      let ratio = (clientX - this.sliderRect.left) / this.sliderRect.width
      ratio = Math.max(0, Math.min(1, ratio))
      const value = Math.round(min + ratio * (max - min))
      this.$set(this.equipContrlForm, field, value)
    },
    handleBack() {
      uni.navigateBack({
        delta: 1
      });
    },
    handleTabClick(tab) {
      this.activeTab = tab;
    },
    showMqttConfig(){
      this.show = true;
    },
    closeMqttConfig(){
      this.show = false;
    },
    async setDeviceContorl(type){
      const data = {
        device_type_id: 3,
        d_id: this.d_id,
        cmd: type,
      }
      if(type === 'imei'){
        data.imei = this.equipContrlForm.imei
      }else{
        delete data.imei
      }
      this.$myRequest({
        url: '/api/api_gateway?method=forecast.send_control.admin_device_control',
        method: 'POST',
        data
      }).then(res => {
        this.closeMqttConfig()
        if (res) {
          uni.showToast({
            title: '设备控制修改成功！',
            icon: 'success',
          });
        } else {
          uni.showToast({
            title: '设备控制修改失败',
            icon: 'error',
          });
        }
      });
    },
    saveSettings(){
      let newForm = Object.assign({}, this.equipContrlForm) // 深拷贝
      newForm.st = newForm.st + ''
      newForm.et = newForm.et + ''
      newForm.st && newForm.st.slice(0, 2).charAt(0) != '0'
        ? newForm.st.slice(0, 2)
        : newForm.st.slice(1, 2)
      newForm.et =
        newForm.et && newForm.et.slice(0, 2).charAt(0) != '0'
          ? newForm.et.slice(0, 2)
          : newForm.et.slice(1, 2)

      for (let k in newForm) {
        if (typeof newForm[k] === 'number') {
          newForm[k] = newForm[k] + ''
        }
      }
      newForm.st = newForm.st.replace(':00', '')
      this.$myRequest({
        url: '/api/api_gateway?method=forecast.send_control.device_control',
        method: 'POST',
        data: {
          device_type_id: 3,
          d_id: this.d_id,
          config: JSON.stringify(newForm)
        }
      }).then(res => {
        if (res) {
          // 设备控制修改成功
          uni.showToast({
            title: '设备控制修改成功！',
            icon: 'success',
          });
        } else {
          uni.showToast({
            title: '设备控制修改失败',
            icon: 'error',
          });
        }
      })
    },
    async getControlDeviceConfigInfo(){
      const res = await this.$myRequest({
        url: '/api/api_gateway?method=forecast.send_control.device_control_info',
        method: 'POST',
        data: {
          d_id: this.d_id,
          cmd:'paramconf'
        },
      });
      this.equipContrlForm = res
    },
  }
}
</script>
<style scoped lang="scss">
.device-detail {
  display: flex;
  width: 100%;
  height: calc(100vh - 112rpx);
  padding-top: 112rpx;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(180deg, #ffffff00 0%, #F5F6FA 23.64%, #F5F6FA 100%), linear-gradient(102deg, #BFEADD 6.77%, #B8F1E7 40.15%, #B9EEF5 84.02%);
  .device-detail__header {
    width: 100%;
    font-size: 28rpx;
    color: #999;
    color: #042118;
    font-family: 'Source Han Sans CN VF';
    font-weight: 700;
    position: relative;
    text-align: center;
    .arrow-left {
      position: absolute;
      left: 32rpx;
      margin-right: 12rpx;
    }
  }
  .device-detail__body {
    width: calc(100% - 64rpx);
    margin: 0 auto;
    border-radius: 16rpx;
    overflow-x: hidden;
    overflow-y: auto;
    // 隐藏滚动条
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  .tabs {
    margin: 24rpx 0;
    border-radius: 16rpx;
    padding: 16rpx 0;
    padding-top: 0;
    .tab-container{
      display: flex;
      width: 100%;
      height: 88rpx;
      line-height: 88rpx;
      text-align: center;
      font-size: 28rpx;
      font-weight: 700;
      color: #042118;
      font-family: 'Source Han Sans CN VF';
    }
    .tab-item {
      margin-right: 40rpx;
      color:#999999;
    }
    .active{
      position: relative;
      color: #303133;
      text-align: center;
      font-family: "Source Han Sans CN VF";
      font-size: 28rpx;
      font-weight: 700;
      &::after {
        content: '';
        position: absolute;
        bottom: 10rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 100%;
        height: 36rpx;
        border-bottom: 6rpx solid #303133;
      }
    }
  }
  .device-detail-content{
    display: flex;
    padding: 24rpx 32rpx;
    flex-direction: column;
    align-items: flex-start;
    gap: 20rpx;
    border-radius: 16rpx;
    background: #FFF;
    .device-detail-item{
      .device-detail-label{
        color: #303133;
        font-family: "Source Han Sans CN VF";
        font-size: 28rpx;
        font-weight: 400;
      }
      .device-detail-btn-container{
        display: flex;
        gap: 24rpx;
        margin-top: 12rpx;
      }
      .device-detail-btn{
        display: flex;
        padding: 10rpx 16rpx;
        justify-content: center;
        align-items: center;
        gap: 16rpx;
        border-radius: 16rpx;
        background: #0BBC58;
        color: #ffffff;
        font-family: "Source Han Sans CN VF";
        font-size: 24rpx;
        font-weight: 400;
      }
      .force-btn{
        background: #FB4E52;
      }
    }
    .device-detail-viewImage{
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-bottom: 20rpx;
    }
  }
  .slider-container{
    position: relative;
    margin-bottom: 20rpx;
    .slider-min-value{
      position: absolute;
      left: 0;
      top: -50rpx;
    }
    .slider{
      width: 600rpx;
    }
    .slider-max-value{
      position: absolute;
      right: 0;
      top: -50rpx;
    }
  }
  .custom-progress{
    width: 600rpx;
    padding: 0;
    .progress-track{
      position: relative;
      height: 12rpx;
      background-color: #ebedf0;
      border-radius: 6rpx;
    }
    .progress-fill{
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      background-color: #0BBC58;
      border-radius: 6rpx;
      transition: width 0.2s;
    }
    .progress-thumb{
      position: absolute;
      top: 50%;
      width: 28rpx;
      height: 28rpx;
      margin-left: -14rpx;
      margin-top: -14rpx;
      border-radius: 50%;
      background-color: #fff;
      box-shadow: 0 1px 4px rgba(0,0,0,0.3);
      transition: left 0.2s;
    }
  }
  .device-detail-btn-footer{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 112rpx;
    line-height: 112rpx;
    text-align: center;
    font-size: 28rpx;
    font-weight: 700;
    color: #042118;
    font-family: 'Source Han Sans CN VF';
    background: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    .device-detail-btn{
      display: flex;
      height: 80rpx;
      width: 90%;
      margin: 0 auto;
      padding: 0rpx 20rpx;
      justify-content: center;
      align-items: center;
      gap: 8rpx;
      border-radius: 16rpx;
      background:#0BBC58;
      color: #ffffff;
      text-align: center;
      font-family: "Source Han Sans CN VF";
      font-size: 14px;
      font-style: normal;
      font-weight: 500;
    }
  }
}
</style>