<template>
  <div style="width: 80%; margin-left: 90px; position: relative">
    <!-- 电汽车 -->
    <el-row :gutter="20">
      <el-col :span="6"
        ><div class="grid-content bg-purple" style="margin-left: 20px">
          时段
        </div></el-col
      >
      <el-col :span="8"
        ><div class="grid-content bg-purple">电费(元/度)</div></el-col
      >
      <el-col :span="6"
        ><div class="grid-content bg-purple">服务费(元/度)</div></el-col
      >
    </el-row>
    <!-- 表单数据 -->
    <el-form :model="carBillingDetail" ref="addForm" :rules="carRules">
      <!-- 尖 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="danger">尖时段</el-button>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="electricityPriceA">
            <el-input
              v-model="carBillingDetail.electricityPriceA"
              type="text"
              placeholder="0"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="servicePriceA">
            <el-input
              v-model="carBillingDetail.servicePriceA"
              placeholder="0"
              clearable
              type="text"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 峰 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="warning">峰时段</el-button>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="electricityPriceB">
            <el-input
              v-model="carBillingDetail.electricityPriceB"
              type="text"
              placeholder="0"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="servicePriceB">
            <el-input
              v-model="carBillingDetail.servicePriceB"
              placeholder="0"
              clearable
              type="text"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 平 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="success">平时段</el-button>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="electricityPriceC">
            <el-input
              v-model="carBillingDetail.electricityPriceC"
              type="text"
              placeholder="1.5"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="servicePriceC">
            <el-input
              v-model="carBillingDetail.servicePriceC"
              placeholder="0"
              clearable
              type="text"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 谷 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="info">谷时段</el-button>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="electricityPriceD">
            <el-input
              v-model="carBillingDetail.electricityPriceD"
              type="text"
              placeholder="0"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="servicePriceD">
            <el-input
              v-model="carBillingDetail.servicePriceD"
              placeholder="0"
              clearable
              type="text"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span>24小时尖峰平谷分布设置</span>
    <!--<div class="frame">
      平-0:00-24:00
      <el-link :underline="false" @click="reviseVisible = true">
        <i class="el-icon-edit el-icon&#45;&#45;right"/>
      </el-link>
      <el-link :underline="false" @click="tClose">
        <i class="el-icon-close"/>
      </el-link>
    </div>-->
    <div>
      <el-tag
        v-for="item in dynamicTags"
        :key="item.label"
        :type="item.type"
        style="margin: 10px 10px 10px 0; width: 160px; height: 30px"
        effect="dark"
      >
        {{ item.label }}
        <i
          class="el-icon-edit el-icon&#45;&#45;right"
          @click="editBtn(item.label)"
        />
        <i class="el-icon-close" @click="deleteBtn(item.label)" />
      </el-tag>
    </div>
    <div>
      <el-button @click="reviseVisible = true">增加计费时段</el-button>
      <el-button>占桩收费模板</el-button>
      <div style="position: absolute; bottom: 0px; right: 10px">
        <el-button @click="hCancel">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </div>
    <el-dialog
      title="设置计费时段"
      :visible.sync="reviseVisible"
      width="40%"
      append-to-body
    >
      <p>开始时间</p>
      <el-time-select
        v-model="startTime"
        :picker-options="{
          start: '00:00',
          step: '00:30',
          end: '24:00',
        }"
        placeholder="选择开始时间"
        :disabled="selectDisabled"
      />
      <p>标签</p>
      <el-radio-group v-model="radio">
        <el-radio :label="1">尖</el-radio>
        <el-radio :label="2">峰</el-radio>
        <el-radio :label="3">平</el-radio>
        <el-radio :label="4">谷</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="reviseVisible = false">取 消</el-button>
        <el-button type="primary" @click="clickSelectStartTimeConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addBillingTemplate } from "@/api/billing/template.js";
export default {
  data() {
    return {
      //这是 子组件
      carBillingDetail: {
        electricityPriceA: "",
        servicePriceA: "",
        electricityPriceB: "",
        servicePriceB: "",
        electricityPriceC: "",
        servicePriceC: "",
        electricityPriceD: "",
        servicePriceD: "",
        timeArray: [
          {
            type: "1",
            startTime: "00:00",
            endTime: "24:00",
          },
        ],
      },
      // 修改弹框
      reviseVisible: false,
      // 开始时间
      startTime: "",
      // 结束时间
      endTime: "",
      // 标签 单选框
      radio: 3,
      selectDisabled: false,
      // 选择的开始时间 默认00:00开始，24:00结束（云快充协议需要24:00,不认23:59）
      selectStartTime: [
        {
          startTime: "00:00",
          type: 1,
        },
      ],
      // 校验规则
      carRules: {
        electricityPriceA: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        servicePriceA: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        electricityPriceB: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        servicePriceB: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        electricityPriceC: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        servicePriceC: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        electricityPriceD: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
        servicePriceD: [
          { required: true, message: "请输入数字", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: "可以保留两位小数",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    async doAdd() {
      try {
        // 通知父组件关闭弹窗,更新数据 子组件
        this.$emit("success");
        // this.carBillingDetail = {
        //   electricityPriceA: "",
        //   servicePriceA: "",
        //   electricityPriceB: "",
        //   servicePriceB: "",
        //   electricityPriceC: "",
        //   servicePriceC: "",
        //   electricityPriceD: "",
        //   servicePriceD: "",
        //   timeArray: [
        //     {
        //       type: "1",
        //       startTime: "00:00",
        //       endTime: "24:00",
        //     },
        //   ],
        // };
        // await addBillingTemplate(this.carBillingDetail)
      } catch (error) {
        this.$message.error("error");
      }
    },
    // 取消按钮
    hCancel() {
      this.$emit("carClose");
      // console.log(1);
    },
    // 确认按钮
    onSubmit() {
      this.$emit("valied", this.carBillingDetail);
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.doAdd();
        }
      });
    },
    // 重置表单
    resetForm() {
      this.$refs.addForm.resetFields();
      console.log("电汽车重置");
    },
    editBtn(param) {
      console.log("点击修改按钮", param);
      const splitArr = param.split("-");
      const editType = splitArr[0].trim();
      if (editType === "尖") {
        this.radio = 1;
      } else if (editType === "峰") {
        this.radio = 2;
      } else if (editType === "平") {
        this.radio = 3;
      } else {
        this.radio = 4;
      }
      // console.log("radio", this.radio);
      this.startTime = splitArr[1].trim();
      // 打开对话框
      this.reviseVisible = true;
      this.selectDisabled = true;
    },
    deleteBtn(param) {
      const splitArr = param.split("-");
      const delObj = splitArr[1].trim();
      if (delObj === "00:00") {
        return;
      }
      this.selectStartTime = this.selectStartTime.filter(
        (item) => item.startTime !== delObj
      );
      // 调生成方法
      this.generateTimeArray();
    },
    clickSelectStartTimeConfirm() {
      let e = {
        startTime: this.startTime,
        type: this.radio,
      };
      // startTime在原数组中存在，把老的删掉
      this.selectStartTime = this.selectStartTime.filter(
        (item) => item.startTime !== this.startTime
      );
      // 新时间插入数组最后
      this.selectStartTime.push(e);
      // selectStartTime根据startTime排序
      this.selectStartTime.sort((a, b) =>
        a.startTime.localeCompare(b.startTime)
      );
      // 关闭对话框
      this.reviseVisible = false;
      // 调生成方法
      this.generateTimeArray();
    },
    // 生成timeArray
    generateTimeArray() {
      // 原来的timeArray初始化为空数组
      this.carBillingDetail.timeArray = [];
      // 使用selectStartTime中的数据重新生成数据
      for (let i = 0; i < this.selectStartTime.length; i++) {
        let start = this.selectStartTime[i];
        let end = this.selectStartTime[i + 1];
        // 遍历到最后一位时，取不到end，就给默认结束时间24:00
        if (end === undefined) {
          end = {
            startTime: "24:00",
          };
        }
        let time = {
          type: String(start.type),
          startTime: start.startTime,
          endTime: end.startTime,
        };
        this.carBillingDetail.timeArray.push(time);
      }
    },
  },
  watch: {
    reviseVisible(newName, oldName) {
      // console.log("reviseVisible发生变化", newName);
      if (newName === false) {
        this.selectDisabled = false;
      }
    },
  },
  computed: {
    dynamicTags() {
      let tags = [];
      this.carBillingDetail.timeArray.map((x) => {
        // console.log(x.type);
        let type;
        let desc;
        if (x.type === "1") {
          type = "danger";
          desc = "尖";
        } else if (x.type === "2") {
          type = "warning";
          desc = "峰";
        } else if (x.type === "3") {
          type = "success";
          desc = "平";
        } else {
          type = "info";
          desc = "谷";
        }
        let label = desc + " - " + x.startTime + " - " + x.endTime;
        let tag = {
          type: type,
          label: label,
        };
        tags.push(tag);
      });
      return tags;
    },
  },
};
</script>

<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.frame {
  width: 126px;
  height: 36px;
  line-height: 36px;
  background-color: #409eff;
}

.el-tag + .el-tag {
  margin-left: 10px;
  // min-height: 100px;
  // padding-bottom: 95px;
}
</style>
