<template>
  <div class="service_info" v-if="data">
    <div class="service_title">
      <span class="service_name" v-text="data.name"></span>
      <span class="service_type" v-if="data.type" v-text="data.type"></span>
      <span
        class="service_level"
        v-text="['-', '共享', '受限', '敏感'][data.openness || 0]"
      ></span>
      <span class="service_access_info">
        <img :src="require('@/assets/imgs/icon_liulan.png')" />
        <span v-text="'浏览次数：' + data.view_count"></span>
      </span>
      <span class="service_access_info">
        <img :src="require('@/assets/imgs/icon_huoqu.png')" />
        <span v-text="'获取次数：' + data.apply_count"></span>
      </span>
    </div>
    <div class="main_container">
      <div class="main_container-left" v-if="data.cover">
        <img :src="data.cover" width="100%" />
      </div>
      <div class="main_container-right">
        <ul class="service_base_info">
          <li>
            <span>提供机构：</span>
            <span v-text="data.organization_name"> </span>
          </li>
          <li>
            <span>发布时间：</span>
            <span v-text="data.create_time"></span>
          </li>
          <li>
            <span>更新时间：</span>
            <span v-text="data.update_date"></span>
          </li>
          <li>
            <span>数据领域：</span>
            <span v-text="data.sectors_name"></span>
          </li>
          <li>
            <span>服务类型：</span>
            <span v-text="data.data_service_type3_str"></span>
          </li>
          <li>
            <span>资源摘要：</span>
            <span v-text="data.descript"></span>
          </li>
        </ul>
        <div class="commodity_information">
          <span>规&emsp;&emsp;格：</span>
          <div class="btn_container">
            <el-button
              v-for="(item, index) in specifications"
              :key="'spcs_' + index"
              :type="item.id === specification.id ? 'primary' : 'default'"
              @click="changeSpecification(item)"
            >
              {{ item.name }}
            </el-button>
          </div>
        </div>
        <div class="commodity_information">
          <span>规格说明：</span>
          <span
            class="commodity_text"
            v-text="specification && specification.descript"
          ></span>
        </div>
        <div class="commodity_information">
          <span>购买方式：</span>
          <div class="btn_container">
            <el-button
              v-for="(item, index) in types"
              v-text="item.name"
              :key="'type_' + index"
              :type="item.value === type ? 'primary' : 'default'"
              @click="changeType(item)"
            >
            </el-button>
          </div>
        </div>
        <div class="commodity_information">
          <span>购买时长：</span>
          <div class="btn_container">
            <el-input-number v-model="duration" :min="1"></el-input-number>
          </div>
        </div>
        <div class="commodity_action">
          <el-button type="warning" plain @click="addToCart">
            加入购物车
          </el-button>
          <el-button type="warning" @click="applyImmediately">
            立即申请
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    data: {
      type: Object,
      default: () => null,
    },
  },
  data: () => ({
    types: [
      {
        name: "按月",
        value: 1,
      },
      {
        name: "按年",
        value: 2,
      },
    ],
    type: 1, // 购买方式 1：按月，2：按年
    specification: {}, // 规格
    duration: 1, // 时长
  }),
  computed: {
    specifications() {
      let specifications = (this.data.serviceRequestSpcs || []).filter(
        (item) => item.type == this.type
      );

      return specifications;
    },
  },
  watch: {
    specifications: {
      handler() {
        this.specification = this.specifications[0];
      },
      deep: true,
    },
  },
  methods: {
    changeSpecification(specification) {
      this.specification = specification;
    },
    changeType({ value }) {
      this.type = value;
      if (this.specifications.length > 0) {
        this.specification = this.specifications[0];
      }
    },
    addToCart() {
      // console.log("addToCart");
      this.$http
        .post("/apaas/serviceapp/v3/shopcart/add", {
          service_id: parseFloat(this.data.service_id), // 0表示不是服务
          app_id: parseFloat(this.data.app_id), // 0表示不是应用
          duration: this.duration, // 申请时长
          spec_id: this.specification.id, // 当为服务时，为服务规格   当为应用时 这里是部署方式的规格 1 部署 2开发
          duration_method: this.type, // 计时方式 按年 按月
          is_subscribe: 0, // 是否订阅
        })
        .then(({ data }) => {
          if (data.success == 1) {
            this.$message({
              message: `添加购物车成功`,
              type: "success",
            });
            this.$store.commit("setMenuCartState");
          } else {
            this.$message({
              message: `添加购物车失败`,
              type: "warning",
            });
          }
        })
        .catch((error) => {
          this.$message({
            message: `添加购物车失败`,
            type: "warning",
          });
        });
    },
    applyImmediately() {
      this.$router.push({
        path: "/shop/shop_car_apply",
        query: {
          service_id: parseFloat(this.data.service_id),
          duration: this.duration,
          duration_unit: this.type,
          spec_id: this.specification.id
        },
      });
    },
  },
  mounted() {
    // 初始化购买方式
    if (this.data.serviceRequestSpcs && this.data.serviceRequestSpcs.length) {
      let types = Array.from(
        new Set(this.data.serviceRequestSpcs.map((item) => item.type))
      );

      this.types = this.types.filter((item) => types.indexOf(item.value) > -1);
      this.type = (this.types[0] && this.types[0].value) || 0;
    }

    // 初始化规格
    if (this.specifications.length > 0) {
      this.specification = this.specifications[0];
    }
  },
};
</script>

<style scoped>
.service_info {
  padding: 20px;
  background-color: #fff;
  margin-bottom: 20px;
}
.main_container {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  margin-top: 25px;
}
.main_container-left {
  width: 466px;
  padding: 3px;
  background-color: #f9fafc;
  margin-right: 30px;
  font-size: 0;
}
.main_container-left img {
  width: 100%;
}
.main_container-right {
  flex-grow: 1;
}
.service_title {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.service_title > .service_name {
  font-size: 22px;
  font-weight: bold;
  color: #0d1847;
  line-height: 36px;
}
.service_title > .service_type {
  flex-shrink: 0;
  height: 24px;
  padding: 0 12px;
  border: 1px solid #5362ee;
  background-color: #5362ee;
  border-radius: 12px;
  font-size: 12px;
  color: #fff;
  line-height: 22px;
  margin-left: 10px;
}
.service_title > .service_level {
  flex-shrink: 0;
  height: 24px;
  padding: 0 12px;
  border: 1px solid #25bdb1;
  border-radius: 12px;
  font-size: 12px;
  color: #25bdb1;
  line-height: 22px;
  margin-left: 10px;
  margin-right: auto;
}
.service_title > .service_access_info {
  flex-shrink: 0;
  font-size: 14px;
  color: #58617a;
  margin-left: 20px;
}
.service_title > .service_access_info > * {
  display: inline-block;
  vertical-align: middle;
}
.service_title > .service_access_info > img {
  width: 15px;
  margin-right: 5px;
}
.service_base_info {
  padding: 18px;
  background-color: #f9fafc;
}
.service_base_info > li,
.commodity_information {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  font-size: 14px;
  color: #242c43;
  line-height: 20px;
}
.service_base_info > li:not(:first-child) {
  margin-top: 18px;
}
.service_base_info > li > span:first-child,
.commodity_information > span:first-child {
  flex-shrink: 0;
  white-space: nowrap;
  color: #8890a7;
}
.service_base_info > li > span:nth-child(2) {
  color: #242c43;
}
.commodity_information {
  margin-top: 30px;
}
.commodity_information > span:nth-child(2) {
  color: #58617a;
}
.commodity_information > span:first-child {
  line-height: 40px;
}
.commodity_information > .commodity_text {
  flex-grow: 1;
  padding: 13px 18px;
  background-color: #f9fafc;
  border-radius: 5px;
}
.commodity_information > .btn_container {
  margin-bottom: -15px;
}
.commodity_information > .btn_container > .el-button,
.commodity_information > .btn_container > .el-input-number {
  margin: 0 20px 15px 0;
}
.commodity_action {
  margin-top: 50px;
  text-align: right;
}
.commodity_action > .el-button {
  width: 220px;
  margin-left: 20px;
}
.service_info .el-button--primary {
  background-color: #515fe7;
  border-color: #515fe7;
}
.service_info .el-button--warning.is-plain {
  background-color: #fcefd6;
  border-color: #fac266;
  color: #e56600;
}
.service_info .el-button--warning {
  background-color: #e56600;
  border-color: #e56600;
}
</style>
