<template>
  <div style="overflow-y: auto; height: 500px">
    <el-form ref="form" :model="form" label-width="150px">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">电动汽车</el-radio>
          <el-radio :label="2">电动自行车</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 50%"
          placeholder="请输入模板名称"
        />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input
          v-model="form.remark"
          style="width: 50%"
          placeholder="请输入模板备注"
        />
      </el-form-item>
      <el-divider />
      <!-- 设置费率 -->

      <!-- 电动汽车费率设置  -->
      <div style="width: 80%; margin-left: 90px" v-if="form.type === 1">
        <!-- 电汽车 -->
        <el-row :gutter="20">
          <el-col :span="6">
            <div class="grid-content bg-purple" style="margin: 2px 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-row :gutter="20" style="margin-bottom: 5px">
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-button type="danger" class="btn">尖时段</el-button>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.electricityPriceA"
                type="number"
                placeholder="0"
                min="0"
                clearable
              />
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.servicePriceA"
                placeholder="0"
                clearable
                type="number"
                min="0"
              />
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-button type="warning">峰时段</el-button>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.electricityPriceB"
                placeholder="0"
                type="number"
                clearable
                min="0"
              />
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.servicePriceB"
                placeholder="0"
                type="number"
                clearable
                min="0"
              />
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-button type="success">平时段</el-button>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.electricityPriceC"
                placeholder="0"
                type="number"
                clearable
                min="0"
              />
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.servicePriceC"
                placeholder="0"
                type="number"
                clearable
                min="0"
              />
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-button type="info">谷时段</el-button>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.electricityPriceD"
                min="0"
                type="number"
                placeholder="0"
                clearable
              />
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-input
                v-model.number="form.servicePriceD"
                min="0"
                type="number"
                placeholder="0"
                clearable
              />
            </div>
          </el-col>
        </el-row>
        <span>24小时尖峰平谷分布设置</span>
        <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="addTimeVisible = true">增加计费时段</el-button>
          <el-button>占桩收费模板</el-button>
        </div>

        <el-dialog
          title="设置计费时段"
          :visible.sync="addTimeVisible"
          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>

        </el-dialog>
      </div>

      <!-- 电动自行车费率设置  -->
      <div v-else>
        <el-form-item label="名称:">
          <el-input v-model="form.name" placeholder="电费1元服务1元" />
        </el-form-item>
        <el-form-item label="描述:">
          <el-input v-model="form.region" placeholder="全天2元" />
        </el-form-item>
        <div class="txt">
          <p>
            <b
              >以下配置应用于电动汽车占桩费计算。例如
              免费占桩时间30分钟，封顶金额25元 占桩费单价2元/分钟</b
            >
          </p>
          <p>
            表示：充电结束30分钟内不收取费用，超过30分钟的时间按每分钟2元收费，封顶25元。
          </p>
        </div>
        <el-form-item label="最小单位费用(元):">
          <el-input
            v-model.number="form.type"
            type="number"
            min="0"
            placeholder="0"
          />
        </el-form-item>
        <div class="txt">
          <p>
            <b
              >本设置只应用于电动自行车（按功率时长）的计费策略，当费用不足最小单位费用的整数倍时，将会安装最小单位费用的整数倍收取（前几分钟不参与计费）。</b
            >
          </p>
          <p>
            如，最小单位费用的是0.5元，当费用为1.1元时，将会收取1.5元；当费用为1.5元时，将会收取1.5元
          </p>
          <p>
            如，<i>免费充电时长的是5分钟</i>，当充电时长是4分钟时，将不会收取费用；当充电时长是6分钟，将会根据计费模板收取6分钟的费用
          </p>
        </div>
        <el-form-item label="免费充电时长(分钟):">
          <el-input
            v-model.number="form.minute"
            type="number"
            min="0"
            placeholder="5"
          />
        </el-form-item>

        <!-- 按钮 -->
        <div class="btn">
          <div class="p1">
            功率-0W至300W
            <el-link :underline="false"
            ><i
              class="el-icon-edit el-icon--right"
              @click="dialogFormVisible = true"
            /></el-link>
            <el-link :underline="false"><i class="el-icon-close" /></el-link>
          </div>
          <div class="energy">电费:1元<span>小时</span></div>
        </div>
        <el-button
          style="width: 170px"
          type="text"
          @click="dialogFormVisible = true"
        >增加功率分段</el-button>

        <el-dialog
          title="设置功率分段"
          :visible.sync="dialogFormVisible"
          append-to-body
        >
          <el-form :model="form">
            <el-form-item label="充电功率(W):" :label-width="formLabelWidth">
              <el-input
                v-model.number="form.name"
                min="0"
                type="number"
                autocomplete="off"
              />
            </el-form-item>
            <el-form-item label="费用(1元X小时):" :label-width="formLabelWidth">
              <el-input
                v-model.number="form.region"
                type="number"
                placeholder="2"
                min="0"
              />
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>

      <!-- 电动汽车和电动自行车 公用一组按钮 -->
      <span slot="footer" class="dialog-footer">
          <el-button @click="addTimeVisible = false">取 消</el-button>
          <el-button type="primary" @click="clickSelectStartTimeConfirm">确 定</el-button>
      </span>

    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      typeCar: "1",
      form: {
        name: "",
        type: 1,
        electricityPriceA: "",
        servicePriceA: "",
        electricityPriceB: "",
        servicePriceB: "",
        electricityPriceC: "",
        servicePriceC: "",
        electricityPriceD: "",
        servicePriceD: "",
        remark: "", // 备注
        timeList: [], // 汽车
      },
      // 新增时间段弹框
      addTimeVisible: false,
      // 开始时间
      startTime: "",
      // 标签 单选框
      radio: 3,
      selectDisabled: false,
      // 选择的开始时间 默认00:00开始，24:00结束（云快充协议需要24:00,不认23:59）
      selectStartTime: [
        {
          startTime: "00:00",
          type: 1,
        },
      ],
      dialogFormVisible: false,
      formLabelWidth: "100px",
    };
  },
  methods: {
    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.addTimeVisible = 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();
    },
    // 生成timeArray
    generateTimeArray() {
      // 原来的timeArray初始化为空数组
      this.form.timeList = [];
      // 使用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.form.timeList.push(time);
      }
    },
    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.addTimeVisible = false;
      // 调生成方法
      this.generateTimeArray();
    },
  },
  computed: {
    dynamicTags() {
      let tags = [];
      this.form.timeList.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 lang="scss" scoped></style>
