<template>
    <div class="carItem">
      <div class="cars-item" @click="showDetail">
        <div class="car-header">
         
          <img
            :src="carData.brand_logo"
            alt=""
            style="width: 70px; height: 34px"
          />
          <div class="car-name">
            <span class="car-type">{{ carData.CarLicense }}</span>
            <span class="car-size">{{ car_type }}汽车 5座</span>
          </div>
        </div>
        <div class="car-body">
          <div class="car-info">
            <h5 class="car-plate">{{ carData.CarLicense }}</h5>
            <div class="car-h">
              <ul class="car-ele">
                <li class="ele-active" v-for="i of car_ele" :key="i"></li>
                <li v-for="index of no_car_ele" :key="index * 10"></li>
              </ul>
              <span class="car-minter">
                约 <b>{{ carData.km }}</b> KM
              </span>
            </div>
          </div>
          <!-- <img src="../../../assets/images/pic001.jpg" alt="" /> -->
          <img :src="carData.CarPhoto" alt="" style="width: 48%" />
        </div>
        <div class="car-foot">
          <div class="car-part">{{ carData.parkingName }}</div>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div
        class="cars-item car-detail"
        v-if="car_info_show"
        :style="'height:' + car_info_height"
      >
        <div class="car-title">
          <div class="car-tpart">{{ carData.parkingName }}</div>
          <i class="el-icon-close" @click="closeDetail"></i>
        </div>
        <div class="car-header">
          <!-- <img src="../../../assets/images/cars-logo.png" alt="" /> -->
          <img
            :src="carData.brand_logo"
            alt=""
            style="width: 7vh"
          
          />
          <div class="car-name">
            <span class="car-type">{{ carData.CarModel }}</span>
            <span class="car-size">{{ car_type }}汽车 5座</span>
          </div>
        </div>
        <div class="car-img">
          <!-- <img src="../../../assets/images/pic001.jpg" alt=""> -->
          <img :src="carData.CarPhoto" alt="" style="width: 80%; margin: auto" />
        </div>
        <div class="car-detail-info">
          <div class="car-name">
            <span class="name">{{ carData.CarLicense }}</span>
            <span class="m"
              >约 <b>{{ carData.km }}</b> KM</span
            >
          </div>
          <div class="car-ele">
            <div class="ele-no">
              <div
                class="ele"
                :style="{ width: (carData.km / 672) * 100 + '%' }"
              ></div>
            </div>
          </div>
          <div class="car-tip">取车约支付12.0元停车费，实际补贴12.0元</div>
          <ul class="car-price-list">
            <li @click="choosePrice" class="price-li">
              <span>日租车</span>
              <span>￥{{ carData.CarDayMoney }}/天</span>
            </li>
            <li @click="choosePrice" class="price-li">
              <span>15日租车</span>
              <span>￥{{ parseInt((carData.CarDayMoney * 15 *0.9) / 15) }}/天</span>
            </li>
            <li @click="choosePrice" class="price-li">
              <span>月租车</span>
              <span>￥{{ parseInt(carData.CarDayMoney / 30 *0.8) }}/天</span>
            </li>
            <li @click="choosePrice" class="price-li">
              <span>小时租车</span>
              <span>￥{{ carData.CarHoursMoney }}/小时</span>
            </li>
          </ul>
        </div>
        <div class="car-deal">
          <!-- <span>参保《全面保障服务》用车更放心</span> -->
          <el-button type="text" @click="open">参保《全面保障服务》用车更放心</el-button>
          <i
            class="el-icon-success"
            :style="{ color: isChecked ? '#000' : '#cccccc' }"
            @click="isChecked = !isChecked"
          ></i>
        </div>
        <div class="car-use" @click="useCar(carData.CarId)">预约用车</div>
      </div>
      <!-- 上传照片弹窗 -->
      <el-dialog
      title="上传车辆照片"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="true"
      :modal="false"
      :center="true"
      :style="{ 'margin-top': '-200px', 'max-height': '80vh' }"
    >
      <Upload @upload-success="handleUploadSuccess"></Upload>
      <img v-if="imageUrl" :src="imageUrl" alt="Uploaded Image" style="width: 100%;">
    </el-dialog>
    </div>
  </template>
  
  <script>
  import { mapActions, mapState } from 'vuex';
  import Upload from '@/components/Upload/index'
  export default {
    name: "CarItem",
    props: ["carData"],
    components: {
    Upload,
    },
    data() {
      return {
        dialogVisible: false,
        imageUrl: "",
        carId:'',
        car_info_show: false,
        car_info_height: 0,
        timer: null,
        // 勾选服务
        isChecked: false,
        // 租赁类型
        leaseType: {
          type: "",
          price: "",
        },
        // 用户id
        id: this.$store.state.user.userId,
      };
    },
    methods: {
      
      showDialog() {
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    handleUploadSuccess(url) {
      this.imageUrl = url;
      // 检查是否上传了图片，如果上传了，则执行预约逻辑
    if (this.imageUrl !== '' && this.imageUrl !== null) {
      setTimeout(() => {
        this.reserveCar();
      },1500);
      
    }
    },reserveCar() {
    let data = {
      userId: window.localStorage.getItem("userId"),
      carId: this.carId,
      mode: this.leaseType.type,
      price: this.leaseType.price,
      orderMoney: 0,
      outPicture: this.imageUrl,
      orderStatus: 0
    }
    data.mode = this.leaseType.type === '小时租车' ? 1 : 0;

    this.$API.car.carRent(data).then(result => {
      if (result.status == 200) {
        this.$message.success(result.message);
        this.$router.push({ path: '/carManage/carList' });
      } else {
        this.$message.error(result.message);
      }
    }).catch(error => {
      console.error(error);
    });
  },
      // 显示详情
      showDetail() {
        this.car_info_show = true;
        this.timer && clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.car_info_height = "800px";
          clearTimeout(this.timer);
        }, 20);
      },
      // 关闭详情
      closeDetail() {
        this.car_info_show = false;
        this.car_info_height = 0;
      },
    //   参保《全面保障服务》用车更放心
      open() {
        this.$alert(`<h2 style="white-space: pre-line;">共享汽车全面参保的意义与重要性</h2>
    <p>共享汽车作为一种新兴的出行方式，为人们提供了便捷、经济的交通选择。然而，共享汽车运营涉及到众多风险，如交通事故、车辆损坏、第三方责任等，因此，进行全面的保险参保对共享汽车运营至关重要。</p>
    <h2 style="white-space: pre-line;">全面参保的内容和范围</h2>
    <ol>
        <li>车辆保险：包括车辆损失险、第三者责任险、车上人员责任险等，以覆盖车辆因意外损坏或交通事故造成的损失和责任。</li>
        <li>人身意外险：保障共享汽车用户在使用过程中因意外事故导致的人身伤害，包括医疗费用、伤残赔偿等。</li>
        <li>车辆盗抢险：保障车辆被盗抢后的损失，包括车辆全损、部分损失等。</li>
        <li>其他附加保障：根据实际情况，还可以考虑购买附加的保障，如玻璃单独破碎险、自燃险等。</li>
    </ol>
    <h2 style="white-space: pre-line;">全面参保的意义</h2>
    <ul>
        <li>保障用户权益：全面参保能够保障共享汽车用户的权益，确保用户在使用共享汽车时遇到问题能够得到及时有效的赔偿和保障。</li>
        <li>降低运营风险：全面参保能够降低共享汽车运营过程中的风险，保障企业利益，确保运营的稳定性和可持续性。</li>
        <li>提升行业信誉：全面参保能够提升共享汽车企业的行业信誉，树立良好的企业形象，吸引更多用户选择共享汽车出行。</li>
    </ul>
    <p>在共享经济蓬勃发展的今天，共享汽车全面参保已成为共享汽车企业应尽的责任和义务，也是保障用户权益、维护行业健康发展的必要措施。共享汽车企业应积极配合相关部门，加强监管，确保全面参保的有效实施。</p>`, {
          dangerouslyUseHTMLString: true
        });
      },
      // 选择价格
      choosePrice() {
        const _this = this;
        const price_list = document.querySelectorAll(".price-li");
        price_list.forEach((item) => {
          item.addEventListener("click", function () {
            price_list.forEach((ele) => {
              ele.className = "price-li";
            });
            this.className = "price-li price-active";
            _this.leaseType.type = this.children[0].innerHTML;
            _this.leaseType.price = this.children[1].innerHTML;
          });
        });
      },
      // 预约用车
      useCar(id) {
        this.carId = id;
        if (!this.leaseType.type) {
          this.$message({ type: "warning", message: "请选择租车类型" });
        } else if (!this.isChecked) {
          this.$message({
            type: "warning",
            message: "请勾选《全面保障服务》协议",
          });
        } else {
  // 检查用户登录情况
  !this.$store.state.user.token && this.$message({ type: "warning", message: "您还未登录，请先登录" });
  // 检查用户实名情况
    let data = {
      UserId: window.localStorage.getItem("userId"),
    };
      this.$API.userinfo.getInfo(data).then(result => {
        if (result.status === 200) {
          if (result.data[0].idCard == null || result.data[0].idCard == ''||result.data[0].driverLicense == null||result.data[0].driverLicense == '') {
            this.$message({ type: "warning", message: "无法下单，您未进行实名认证请先前往个人中心认证" })
          };
        } else {
          this.$message.error(result.message);
        }
      }).catch(error => {
        console.error(error);
      });
    }
   if(this.imageUrl==''||this.imageUrl==null)
    {
      this.showDialog()
    }

      
      },
      ...mapActions('userAbout', ['IsCompleteCertification']),
      ...mapActions('orderAbout', ['OrderCar'])
    },
    computed: {
      //燃油汽车energy为0使用emass  新能源为1使用omass
      car_type() {
        return this.carData.energy == 1
          ? "新能源"
          : this.carData.energy == 0
          ? "燃油"
          : "混合动力";
      },
      car_ele() {
        return this.carData.energy == 1
          ? parseInt(this.carData.omass / 10)
          : this.carData.energy == 0
          ? parseInt(this.carData.emass / 10)
          : parseInt((this.carData.emass + this.carData.omass) / 20);
          console.log(this.car_ele());
      },
      no_car_ele() {
        return 10 - this.car_ele;
      },
      
      ...mapState('userAbout', ['authentication'])
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .carItem {
    position: relative;
    .cars-item {
      cursor: pointer;
      width: 505px;
      height: 258px;
      background-color: #fff;
      border-radius: 30px;
      padding: 30px 30px 20px 30px;
      box-sizing: border-box;
      box-shadow: 1px 0 18px 0 rgba(0, 0, 0, 0.2);
      .car-header {
        img {
          display: inline-block;
          vertical-align: middle;
          margin-right: 12px;
          position: relative;
        }
        .car-name {
          display: inline-block;
          .car-type {
            margin-right: 140px;
            font-size: 16px;
          }
          .car-size {
            font-size: 14px;
            color: #adaeb0;
            position: absolute;
            right: 36px;
          }
        }
      }
      .car-body {
        margin-top: 8px;
        width: 100%;
        height: 120px;
        display: flex;
        justify-content: space-between;
        border-bottom: 2px solid #e1e1e1;
        .car-info {
          .car-plate {
            font-size: 18px;
            font-weight: 400;
            margin-top: 20px;
            margin-bottom: 20px;
          }
          .car-h {
            display: flex;
          }
          .car-ele {
            display: inline-block;
            margin-right: 8px;
          }
          .car-ele li {
            display: inline-block;
            width: 8px;
            height: 24px;
            border-radius: 12px;
            margin-right: 3px;
            background-color: #e1e1e1;
          }
          .ele-active {
            background: linear-gradient(#11a5f8, #0c8ddb);
          }
          .car-minter {
            font-size: 12px;
            line-height: 31px;
            b {
              font-size: 16px;
            }
          }
        }
      }
      .car-foot {
        width: 100%;
        display: flex;
        justify-content: space-between;
        margin-top: 18px;
        color: #6e7174;
        font-size: 14px;
        line-height: 22px;
        i {
          color: #e1e1e1;
          font-size: 20px;
        }
      }
    }
    .car-detail {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      @keyframes showUp {
        0% {
          height: 0;
        }
      }
      animation: showUp 0.3s ease 0s;
      cursor: auto;
      .car-title {
        font-size: 18px;
        color: #6e7174;
        display: flex;
        line-height: 24px;
        justify-content: space-between;
        margin-bottom: 20px;
        i {
          font-size: 24px;
          cursor: pointer;
        }
      }
      .car-img {
        width: 100%;
        height: 200px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .car-detail-info {
        .car-name {
          display: flex;
          justify-content: space-between;
          .name {
            font-size: 20px;
            color: #34393f;
          }
          .m {
            font-size: 12px;
            b {
              font-size: 18px;
            }
          }
        }
        .car-ele {
          height: 40px;
          width: 100%;
          border-radius: 20px;
          background-color: #fff;
          padding: 15px 12px;
          box-sizing: border-box;
          box-shadow: 1px 0 8px 0 rgba(0, 0, 0, 0.2);
          margin-top: 20px;
          .ele-no {
            background-color: #e1e1e1;
            width: 100%;
            height: 100%;
            border-radius: 20px;
            position: relative;
            .ele {
              background-color: #0f9cee;
              width: 70%;
              height: 100%;
              border-radius: 20px;
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
        .car-tip {
          font-size: 14px;
          text-align: center;
          color: #6e7174;
          margin-top: 20px;
          margin-bottom: 15px;
        }
        .car-price-list {
          margin-bottom: 10px;
          li {
            background-color: #f3f3f3;
            height: 45px;
            box-sizing: border-box;
            border-radius: 8px;
            line-height: 45px;
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            padding-right: 20px;
            padding-left: 15px;
            cursor: pointer;
          }
          .price-active {
            border: 2px solid #0f9cee;
          }
        }
      }
      .car-deal {
        margin-top: 30px;
        font-size: 14px;
        padding-left: 5px;
        padding-right: 5px;
        display: flex;
        line-height: 20px;
        justify-content: space-between;
        i {
          font-size: 20px;
          color: #cccccc;
        }
      }
      .car-use {
        font-size: 26px;
        background-color: #353b3f;
        text-align: center;
        line-height: 84px;
        height: 84px;
        width: 240px;
        margin: auto;
        margin-top: 35px;
        // position: absolute;
        // left: 50%;
        // transform: translate(-50%);
        // bottom: -42px;
        border-radius: 42px;
        color: #fff;
        cursor: pointer;
      }
    }
  }
  </style>