<template>
  <BlockRadius
    :borRadius="8"
    :paddingNum="32"
    class="service_info apaas_button"
    v-if="data && showItem"
  >
    <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">
        <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 v-if="data.openness == 1" class="tags shared">共享</span>
          <span v-else-if="data.openness == 2" class="tags restricted">
            受限
          </span>
          <span v-else class="tags sensitive">敏感</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>
        <p class="des_p">{{ data.descript }}</p>
        <div class="jiage_d">
          <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 class="guige_title">计次收费：</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"
          >
            <div
              v-for="(item, index) in data.serviceRequestSpcs.spcs_type_1"
              :key="'spcs_' + index"
              :class="
                item.id === specification.id ? 'spec_btn_act' : 'spec_btn'
              "
              @click="changeSpecification(item, 0)"
            >
              {{ item.money }}金币/{{ item.spcs_count }}次
            </div>
          </div>
        </div>
        <div class="spec_des">
          {{
            specification && specification.spcs_type == 1
              ? specification.descript
              : data.serviceRequestSpcs.spcs_type_1[0].descript
          }}
        </div>
        <div
          class="commodity_information"
        >
          <span class="guige_title">时长收费：</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">
            <div
              v-for="(item, index) in data.serviceRequestSpcs.spcs_type_2"
              :key="'spcs_' + index"
              :class="
                item.id === specification.id ? 'spec_btn_act' : 'spec_btn'
              "
              @click="changeSpecification(item, 1)"
            >
              {{ item.money }}金币/月
            </div>
          </div>
        </div>
        <div
          class="spec_des"
          v-if="
            !(
              (data.serviceRequestSpcs.spcs_type_2 &&
                data.serviceRequestSpcs.spcs_type_2.length == 0) ||
              !data.serviceRequestSpcs.spcs_type_2
            )
          "
        >
          {{ data.serviceRequestSpcs.spcs_type_2[0].descript || "-" }}
        </div>
        <!-- <div class="commodity_information">
          <span class="guige_title">规格说明：</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 class="guige_title">购买时长：</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">
            <div class="spec_btn_act">不限时长</div>
          </div>
        </div>
        <div class="commodity_action">
          <el-button
            class="service_now_buy"
            @click="applyImmediately"
            :disabled="actionDisabled"
          >
            立即申请
          </el-button>
          <el-button
            class="service_add_cart"
            @click="addToCart"
            :disabled="actionDisabled"
          >
            加入购物车
          </el-button>
        </div>
      </div>
    </div>
  </BlockRadius>
</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";
import BlockRadius from "@/components/general/block-radius";

export default {
  components: {
    MapView,
    workflowsView,
    processView,
    BlockRadius,
  },
  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>
.main_container {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin: 8px 8px 0;
}
.main_container-left {
  width: 200px;
  height: 200px;
  padding: 2px;
  background-color: #f6f7fb;
  font-size: 0;
  position: relative;
}
.main_container-left img {
  width: 100%;
  height: 100%;
  border-radius: 6px;
}
.main_container-left > .map_view {
  position: absolute;
  right: 3px;
  bottom: 3px;
}
.main_container-right {
  width: 896px;
}
.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: 4px;
  font-size: 12px;
  color: #fff;
  line-height: 22px;
  margin-left: 10px;
}

.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: 12px;
  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,
.guige_title {
  color: #8890a7;
  width: 100px;
  margin-right: 20px;
  text-align: right;
}
.service_base_info > li > span:nth-child(2) {
  color: #242c43;
}
.commodity_information {
  margin-top: 16px;
}
.commodity_information > span:nth-child(2) {
  color: #58617a;
}
.commodity_information > span:first-child {
  line-height: 36px;
}
.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;
}
.spec_btn {
  height: 36px;
  padding: 7px 18px 6px;
  background-color: #ffffff;
  border-radius: 6px;
  border: solid 1px #dcdfe6;
  font-size: 14px;
  line-height: 20px;
  color: #58617a;
  margin-right: 8px;
  margin-bottom: 8px;
  cursor: pointer;
}
.spec_btn_act {
  height: 36px;
  padding: 7px 18px 6px;
  background-color: #515fe7;
  border-radius: 6px;
  border: solid 1px #515fe7;
  font-size: 14px;
  line-height: 20px;
  color: #f4f7fc;
  margin-right: 8px;
  margin-bottom: 8px;
  cursor: pointer;
}
.spec_btn:hover {
  background-color: #f2f5fe;
  border: solid 1px #c3caf8;
  color: #58617a;
}
.commodity_action {
  margin-top: 16px;
  margin-left: 120px;
}
.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;
  font-weight: bold;
}
.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;
}
.service_add_cart {
  width: 192px;
  height: 40px;
  background-color: #fcefd6;
  border-radius: 6px;
  border: solid 1px #fac266;
  color: #e56600;
  font-size: 16px;
}
.service_now_buy {
  width: 192px;
  height: 40px;
  background-color: #e56600;
  border: solid 1px #e56600;
  border-radius: 6px;
  color: #fcefd6;
  font-size: 16px;
  margin-right: 24px;
}
.tags {
  display: inline-block;
  margin-left: 8px;
  height: 24px;
  padding: 0 12px;
  font-size: 12px;
  font-weight: 500;
  line-height: 24px;
  border-radius: 4px;
  position: relative;
  top: 1px;
  flex-shrink: 0;
  margin-right: auto;
}
.map_service {
  background-color: #626de9;
  border: solid 1px #626de9;
  color: #fff;
}
.shared {
  background-color: #e7fdfc;
  border: solid 1px #8bd6d0;
  color: #25bdb1;
}
.restricted {
  background-color: #fffee8;
  border: solid 1px #e4c884;
  color: #ef9433;
}
.sensitive {
  background-color: #ffefef;
  border: solid 1px #d7a4a9;
  color: #e15260;
}
.des_p {
  color: #8890a7;
  font-size: 14px;
  margin-top: 8px;
  margin-bottom: 12px;
}
.jiage_d {
  background: url("~@/assets/imgs/img_bg_jiage.png");
  width: 896px;
  height: 72px;
  margin-bottom: 16px;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.spec_des {
  margin-left: 128px;
  color: rgba(88, 97, 122, 0.5);
  font-size: 14px;
  line-height: 14px;
  margin-top: 8px;
}
</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;
}
.service_info .el-input-number {
  width: 128px;
}
</style>
