<template>
  <view class="control">
    <view class="control-title"> 分时控制 </view>

    <view
      class="item-box"
      v-for="(item, index) in list"
      :key="item.w_functionid"
    >
      <view
        class="TR-state"
        hover-class="navigator-hover"
        @click.stop="changTR(index)"
        >{{
          (TRList[index].showValue.includes("投入") ? "切出" : "投入") ||
          "暂未设置"
        }}</view
      >
      <view class="item" hover-class="navigator-hover" @click="onClick(item)">
        <view class="title">
          <view>{{ item.funcname }}</view>
          <view class="pilotLamp">
            <pilotLamp :trItem="TRList[index]" />
          </view>
        </view>
        <view class="row">
          <view class="box">
            <view class=""> 开始时间 </view>
            <view class="content">
              <view class="value">{{ item.showValue.begin || "--" }}</view>
            </view>
          </view>
          <view class="box">
            <view class=""> 结束时间 </view>
            <view class="content">
              <view class="value">{{ item.showValue.end || "--" }}</view>
            </view>
          </view>
          <view class="box">
            <view class=""> 压力设定 </view>
            <view class="content">
              <view class="value">{{ item.showValue.value || "--" }}</view>
              <view class="unit">{{
                item.params[item.funcname + "压力设定"].unit || ""
              }}</view>
            </view>
          </view>
        </view>
        <view v-if="item.isfeedback" class="dot" />
      </view>
    </view>

    <My_input ref="MyInput" @customMethod="customMethod">
      <view class="change-item" v-if="changeItem.funcname">
        <!-- :end="endTime"  -->
        <!-- :start="changeItem.showValue.begin" -->
        <picker
          mode="time"
          :value="changeItem.showValue.begin"
          data-name="begin"
          @change="timeChange"
        >
          <view class="row">
            <view class="label"> 开始时间： </view>
            <view class="value day-parting-row"
              >{{ changeItem.showValue.begin || " --" }}
            </view>
          </view>
        </picker>
        <picker
          mode="time"
          :value="changeItem.showValue.end"
          data-name="end"
          @change="timeChange"
        >
          <view class="row">
            <view class="label"> 结束时间： </view>
            <view class="value day-parting-row">{{
              changeItem.showValue.end || " --"
            }}</view>
          </view>
        </picker>
        <view class="row">
          <view class="label"> 压力设定： </view>
          <view class="value">
            <u-input
              :placeholder="changeItem.showValue.value || '压力设定'"
              v-model="changeItem.showValue.value"
              :type="
                changeItem.params[changeItem.funcname1 + '压力设定']
                  .num_scale == 0
                  ? 'number'
                  : 'digit'
              "
            >
              <template slot="suffix">
                {{
                  changeItem.params[changeItem.funcname1 + "压力设定"].unit ||
                  ""
                }}
              </template>
            </u-input>
          </view>
        </view>
      </view>
    </My_input>
  </view>
</template>

<script>
import pilotLamp from "./pilotLamp";
export default {
  name: "division",
  components: { pilotLamp },
  data() {
    return {
      list: [],
      TRList: [],
      changeItem: {},
      endTime: "",
    };
  },
  methods: {
    loadData(newVal, Chinese = false) {
      let reg = /^时段\d{1,5}$/,
        count = 0,
        list = [];
      for (const key in newVal.function) {
        if (reg.test(key)) count++;
      }
      let Nzh = require("nzh");
      let nzhcn = require("nzh/cn"); //直接使用简体中文
      function formattingKey(num, prefix = "T") {
        let res = "";
        switch (String(num).length) {
          case 1:
            res = prefix + "00" + num;
            break;
          case 2:
            res = prefix + "0" + num;
            break;
          case 3:
            res = prefix + num;
            break;
        }
        return res;
      }
      let key1 = 1,
        key2 = 49,
        TRList = [];

      for (let i = 1; i <= count; i++) {
        let name = Chinese ? nzhcn.encodeS(i) : i;
        let obj = newVal.function[`时段${i}`],
          keyList = [];
        for (let index = 0; index < 4; index++) {
          keyList.push(formattingKey(key1));
          key1++;
          if (index == 3) {
            keyList.push(formattingKey(key2));
            key2++;
            TRList.push(
              this.__proto__.getControlItem([`时间段${i}`], newVal)[0]
            );
          }
        }
        let item = {
          keyList,
          index: i,
          inputType: "slot",
          paramValue: {
            begin:
              newVal.paramcmdvalues[keyList[0]] +
              ":" +
              newVal.paramcmdvalues[keyList[2]],
            end:
              newVal.paramcmdvalues[keyList[1]] +
              ":" +
              newVal.paramcmdvalues[keyList[3]],
            value: newVal.paramcmdvalues[keyList[4]],
          },
          showValue: {
            begin:
              newVal.paramvalues[keyList[0]] +
              ":" +
              newVal.paramvalues[keyList[2]],
            end:
              newVal.paramvalues[keyList[1]] +
              ":" +
              newVal.paramvalues[keyList[3]],
            value: newVal.paramvalues[keyList[4]],
          },
          params: {},
        };
        keyList.forEach((key) => {
          item.params[key] = newVal.params[key];
        });
        obj.funcname1 = "时段" + nzhcn.encodeS(i);
        item.isfeedback =
          newVal.isfeedback &&
          (obj.paramValue.begin || obj.paramValue.end || obj.paramValue.value)
            ? true
            : false;
        list.push(Object.assign(obj, item));
      }
      this.TRList = TRList;
      this.list = list;
    },
    onClick(item) {
      this.changeItem = JSON.parse(JSON.stringify(item));
      this.endTime =
        this.changeItem.showValue.end == "0:0"
          ? ""
          : this.changeItem.showValue.end;
      let toBeUpdated = [];
      if (item.paramValue.begin)
        toBeUpdated.push(`开始时间：${item.paramValue.begin}`);
      if (item.paramValue.end)
        toBeUpdated.push(`结束时间：${item.paramValue.begin}`);
      if (item.paramValue.value)
        toBeUpdated.push(`压力设定：${item.paramValue.value}`);
      if (toBeUpdated.length) {
        item.toBeUpdated = "待更新记录：" + toBeUpdated.join(",");
      }
      this.$refs.MyInput.openInput(item, item.inputType != "switch");
    },
    changTR(index) {
      this.$refs.MyInput.openInput(this.TRList[index]);
    },
    timeChange(e) {
      const name = e.currentTarget.dataset.name;
      this.changeItem.showValue[name] = e.detail.value;
    },
    customMethod() {
      const { showValue, w_functionid, params, index } = this.changeItem,
        MyInput = this.$refs.MyInput;
      if (!showValue.begin) return MyInput.submitBreak("还未填写开始时间");
      if (!showValue.end) return MyInput.submitBreak("还未填写结束时间");
      if ((showValue.value + "").length == 0)
        return MyInput.submitBreak("还未设定压力");
      let item = this.list[index - 1];
      showValue.value = (showValue.value - 0).toFixed(
        params[item.keyList[4]].num_scale
      );
      let obj = {
        [item.keyList[0]]: showValue.begin.split(":")[0],
        [item.keyList[1]]: showValue.end.split(":")[0],
        [item.keyList[2]]: showValue.begin.split(":")[1],
        [item.keyList[3]]: showValue.end.split(":")[1],
        [item.keyList[4]]: showValue.value,
      };
      console.log("发送请求", obj);
      MyInput.submit(w_functionid, obj);
    },
  },
};
</script>

<style lang="scss" scoped>
/* 边框 */
.day-parting-row {
  display: flex;
  align-items: center;
  flex: 1;
  width: 0;
  border: 1px solid #dadbde;
  border-radius: 4px;
  padding: 6px 9px;
  height: 35px;
  font-size: 14px;
  box-sizing: border-box;
}

.item-box {
  position: relative;
  .TR-state {
    position: absolute;
    top: 4px;
    right: 6px;
    font-size: 12px;
    background: #004a92;
    color: #fff;
    padding: 4px 6px;
    border-radius: 4px;
    z-index: 2;
  }
  .item {
    position: relative;
    padding: 4px 6px 6px;
    box-sizing: border-box;
    background: #fff;
    border-radius: 4px;
    margin-bottom: 5px;

    .title {
      display: flex;
      margin-bottom: 6px;
      font-weight: bold;
      .pilotLamp {
        margin-left: 10px;
      }
    }

    .row {
      display: flex;

      .box {
        width: 33.33%;

        .content {
          display: flex;
          margin-top: 6px;
          align-items: flex-end;

          .value {
            width: 0;
            flex: 1;
            color: #333;
            font-size: 16px;
            flex-shrink: 0;
            font-weight: bold;
          }

          .unit {
            font-size: 10px;
            color: #666;
            flex-shrink: 0;
            max-width: 50px;
          }
        }
      }
    }

    .dot {
      position: absolute;
      right: 2px;
      top: 2px;
      width: 10px;
      height: 10px;
      background: #d9001b;
      border-radius: 50%;
    }
  }
}

.change-item {
  .row {
    display: flex;
    align-items: center;
    line-height: 35px;
    width: 100%;
    margin-top: 4px;

    .label {
      width: 80px;
      flex-shrink: 0;
    }
  }
}
</style>