<template>
  <div class="service_info apaas_button" v-if="data && showItem">
    <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%" v-if="data.type !== 'workflow'" />
        <workflows-view
          :zoom="0.6"
          :id="parseInt(data.workflows_id)"
          :hideDetail="true"
          v-if="data.type == 'workflow'"
        ></workflows-view>
        <map-view
          v-if="data.type === 'Map Service'"
          :id="data.portal_id"
        ></map-view>
        <process-view
          :id="parseInt(data.workflows_id)"
          v-if="data.type == 'workflow'"
        ></process-view>
      </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="dateTransform(data.create_time)"></span>
          </li>
          <li>
            <span>更新时间：</span>
            <span v-text="dateTransform(data.update_date)"></span>
          </li>
          <li>
            <span>数据领域：</span>
            <span v-text="data.sectors_name"></span>
          </li>
          <li>
            <span>服务类型：</span>
            <span v-html="data.data_service_type.join('&ensp;&ensp;')"></span>
          </li>
          <li>
            <span>资源摘要：</span>
            <span v-text="data.descript"></span>
          </li>
        </ul>
        <div class="and_wid">
          <div class="dbn_wkdn">
            价&nbsp;&nbsp;格：<span class="ndwa_indowa">
              {{
                specification && specification.money
                  ? specification.money * (showTime ? duration : 1)
                  : 0
              }}
            </span>
            <span class="kfdf_jffa">金币</span>
          </div>
          <div class="nmd_kwd">
            月售：<span class="ndwa_dn">{{ data.month_sale }} 笔 </span
            ><img src="@/assets/imgs/shop_ic_star.png" class="dwin_diwa" />
            <span class="dn_wjd">{{ data.scoreDetail.avgScore }}</span>
          </div>
        </div>
        <div class="commodity_information">
          <span>计次收费规格：</span>
          <span
            v-if="
              (data.serviceRequestSpcs.spcs_type_1 &&
                data.serviceRequestSpcs.spcs_type_1.length == 0) ||
              !data.serviceRequestSpcs.spcs_type_1
            "
            class="commodity_text"
            >暂无</span
          >
          <div
            v-else-if="
              data.serviceRequestSpcs.spcs_type_1 &&
              data.serviceRequestSpcs.spcs_type_1.length != 0
            "
            class="btn_container"
          >
            <el-button
              v-for="(item, index) in data.serviceRequestSpcs.spcs_type_1"
              :key="'spcs_' + index"
              :type="item.id === specification.id ? 'primary' : 'default'"
              @click="changeSpecification(item, 0)"
            >
              {{ item.money }}金币/{{ item.spcs_count }}次
            </el-button>
          </div>
        </div>
        <div class="commodity_information">
          <span>时长收费规格：</span>
          <span
            v-if="
              (data.serviceRequestSpcs.spcs_type_2 &&
                data.serviceRequestSpcs.spcs_type_2.length == 0) ||
              !data.serviceRequestSpcs.spcs_type_2
            "
            class="commodity_text"
            >暂无</span
          >
          <div v-else class="btn_container">
            <el-button
              v-for="(item, index) in data.serviceRequestSpcs.spcs_type_2"
              :key="'spcs_' + index"
              :type="item.id === specification.id ? 'primary' : 'default'"
              @click="changeSpecification(item, 1)"
            >
              {{ item.money }}金币/月
            </el-button>
          </div>
        </div>
        <div class="commodity_information">
          <span>规格说明：</span>
          <span v-if="actionDisabled" class="commodity_text">暂无</span>
          <span
            v-else
            class="commodity_text"
            v-text="(specification && specification.descript) || '-'"
          ></span>
        </div>
        <div class="commodity_information">
          <span>购买时长：</span>
          <div v-if="showTime" class="btn_container">
            <el-input-number
              v-model="duration"
              :min="1"
              :disabled="actionDisabled"
            ></el-input-number>
            月
          </div>
          <div v-else class="btn_container">
            <el-button type="primary"> 不限时长 </el-button>
          </div>
        </div>
        <div class="commodity_action">
          <el-button
            type="warning"
            plain
            @click="addToCart"
            :disabled="actionDisabled"
          >
            加入购物车
          </el-button>
          <el-button
            type="warning"
            @click="applyImmediately"
            :disabled="actionDisabled"
          >
            立即申请
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import helper from "@/services/helper.js";
import MapView from "./map-view";
import workflowsView from "@/components/work-flow/workflows-view";
import processView from "@/components/service-info/process-view";

export default {
  components: {
    MapView,
    workflowsView,
    processView,
  },
  props: {
    data: {
      type: Object,
      default: () => null,
    },
  },
  data: () => ({
    types: [],
    type: 1, // 购买方式 1：按月，2：按年
    specification: {}, // 规格
    duration: 1, // 时长
    showTime: false,
    showItem: false,
  }),
  computed: {
    actionDisabled() {
      let a = this.data.serviceRequestSpcs;
      return (
        a &&
        a.spcs_type_1 &&
        a.spcs_type_1.length == 0 &&
        a.spcs_type_2 &&
        a.spcs_type_2.length == 0
      );
    },
  },
  watch: {},
  methods: {
    dateTransform(datestr = "") {
      return helper.dateStringTransform(datestr);
    },
    changeSpecification(specification, type) {
      this.specification = specification;
      if (type == 0) {
        this.showTime = false;
      } else if (type == 1) {
        this.showTime = true;
      }
    },
    changeType({ value }) {
      this.type = value;
    },
    addToCart() {
      // console.log("addToCart");
      this.$http
        .post("/apaas/serviceapp/v3/shopcart/add", {
          service_id: parseFloat(this.data.service_id),
          app_id: 0, // 0表示不是应用
          spec_id: this.specification.id, // 规格id
          duration: this.duration, // 申请时长
          duration_method: this.type, // 计时方式 按年 按月
          is_subscribe: 0, // 是否订阅
        })
        .then(({ data }) => {
          if (data.success == 1) {
            this.$message({
              message: data.errMsg,
              type: "success",
            });
            this.$store.commit("setMenuCartState");
          } else {
            this.$message({
              message: data.errMsg,
              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),
          spec_id: this.specification.id,
          spcs_type: this.specification.spcs_type,
          duration: this.duration,
          duration_unit: this.type,
        },
      });
    },
  },
  mounted() {
    // 初始化购买方式
    if (this.data.serviceRequestSpcs && this.data.serviceRequestSpcs.length) {
      if (!this.data.serviceRequestSpcs.spcs_type_1) {
        this.data.serviceRequestSpcs.spcs_type_1 = [];
      }
      if (!this.data.serviceRequestSpcs.spcs_type_2) {
        this.data.serviceRequestSpcs.spcs_type_2 = [];
      }
      let types = Array.from(
        new Set(this.data.serviceRequestSpcs.map((item) => item.type))
      );
      if (types.indexOf(3) > -1) {
        this.types = [
          {
            name: "按月",
            value: 1,
          },
          {
            name: "按年",
            value: 2,
          },
        ];
      } else if (types.indexOf(2) > -1) {
        this.types = [
          {
            name: "按年",
            value: 2,
          },
        ];
      } else if (types.indexOf(1) > -1) {
        this.types = [
          {
            name: "按月",
            value: 1,
          },
        ];
      }

      this.type = (this.types[0] && this.types[0].value) || 0;
    }

    // 初始化规格
    if (
      this.data.serviceRequestSpcs &&
      this.data.serviceRequestSpcs.spcs_type_1 &&
      this.data.serviceRequestSpcs.spcs_type_1.length > 0
    ) {
      this.specification = this.data.serviceRequestSpcs.spcs_type_1[0];
    } else if (
      this.data.serviceRequestSpcs &&
      this.data.serviceRequestSpcs.spcs_type_2 &&
      this.data.serviceRequestSpcs.spcs_type_2.length > 0
    ) {
      this.specification = this.data.serviceRequestSpcs.spcs_type_2[0];
    }
    this.showItem = true;
  },
};
</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;
  position: relative;
}
.main_container-left img {
  width: 100%;
}
.main_container-left > .map_view {
  position: absolute;
  right: 3px;
  bottom: 3px;
}
.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: 19px;
  margin-right: 5px;
}
.service_base_info {
  padding: 18px;
  background-color: #f9fafc;
  border-radius: 8px;
}
.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 {
  border-width: 2px;
}
.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;
  font-size: 16px;
}
.and_wid {
  background-color: #f9fafc;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}
.dbn_wkdn {
  color: #8890a7;
  font-size: 14px;
}
.ndwa_indowa {
  color: #e56600;
  font-size: 26px;
  font-weight: bold;
}
.kfdf_jffa {
  color: #e56600;
  font-size: 14px;
}
.nmd_kwd {
  color: #8890a7;
}
.ndwa_dn {
  color: #242c43;
}
.dn_wjd {
  color: #ea7d19;
  font-size: 18px;
  position: relative;
}
.dwin_diwa {
  width: 14px;
  height: 14px;
  margin-left: 20px;
  margin-right: 4px;
  position: relative;
}
.dn_wjd::after {
  content: "";
  position: absolute;
  width: 2px;
  height: 16px;
  background-color: #e3e5ef;
  top: 5px;
  left: -35px;
}
</style>

<style>
.service_info.apaas_button .el-button--primary {
  color: #fff;
  background-color: #515fe7;
  border-color: #515fe7;
}
.service_info .el-input-number .el-input-number__decrease {
  top: 2px;
  left: 2px;
  width: 38px;
  line-height: 36px;
  border-radius: 8px 0 0 8px;
  border-right-width: 2px;
}
.service_info .el-input-number .el-input-number__increase {
  top: 2px;
  right: 2px;
  width: 38px;
  line-height: 36px;
  border-radius: 0 8px 8px 0;
  border-left-width: 2px;
}
.service_info .el-input-number .el-input__inner {
  border-width: 2px;
}
</style>
