<template>
  <div class="sevice_detail">
    <div class="apass_breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="/shop">服务超市</el-breadcrumb-item>
        <el-breadcrumb-item to="/shop/data_service_list/5">
          感知服务
        </el-breadcrumb-item>
        <el-breadcrumb-item>服务详情信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <service-info :data="baseInfo" v-if="baseInfo"></service-info>

    <service-tabs
      :detail-data="detailData"
      :provider-data="providerData"
      :comments-data="commentsData"
      :comments-url="commentsUrl"
    ></service-tabs>
  </div>
</template>

<script>
import serviceInfo from "@/components/service-info/service-info";
import serviceTabs from "@/components/service-tabs/service-tabs";

export default {
  components: {
    serviceInfo,
    serviceTabs,
  },
  data: () => ({
    baseInfo: null,
    detailData: null,
    providerData: null,
    commentsData: null,
  }),
  computed: {
    id() {
      return this.$route.params.id;
    },
    commentsUrl() {
      return `/apaas/serviceapp/v3/servicemarket/estimates?serviceId=${this.id}`;
    },
  },
  methods: {
    init() {
      this.$http
        .get("/apaas/serviceapp/v3/servicemarket/detail", {
          params: {
            serviceId: this.id,
          },
        })
        .then(({ data }) => {
          let datas = data.data;
          this.baseInfo = {
            service_id: this.id,
            app_id: 0,
            name: datas.name,
            // type: "",
            openness: datas.openness,
            view_count: datas.view_count,
            apply_count: datas.apply_count,
            cover: datas.cover,
            organization_name: datas.organization_name,
            create_time: datas.create_time,
            update_date: datas.update_date,
            sectors_name: datas.sectors_name,
            data_service_type: [
              datas.data_service_type1_name,
              datas.data_service_type2_name,
              datas.data_service_type3_name,
            ],
            descript: datas.descript,
            serviceRequestSpcs: datas.serviceRequestSpcs || {spcs_type_1: [],spcs_type_2: []},
            scoreDetail: datas.scoreDetail,
            month_sale: datas.month_sale,
          };

          this.detailData = [
            {
              name: "获取流程",
              type: "step",
              value: ["服务申请", "信息填写", "审核确认", "服务获取"],
            },
            {
              name: "服务信息",
              type: "table-list",
              value: {
                data_service_type1_name: datas.data_service_type1_name,
                data_service_type2_name: datas.data_service_type2_name,
                sectors_name: datas.sectors_name,
                create_time: datas.create_time,
                update_date: datas.update_date,
                req_type: ["-", "GET", "POST", "PUT", "DELETE"][
                  datas.req_type || 0
                ],
                encode_method: datas.encode_method,
              },
            },
            // {
            //   name: "请求方式",
            //   type: "text",
            //   value: ["-", "GET", "POST", "PUT", "DELETE"][datas.req_type || 0],
            // },
            // {
            //   name: "编码格式",
            //   type: "text",
            //   value: datas.encode_method,
            // },
            {
              name: "请求参数",
              type: "table",
              value: {
                datas: this.getTableData(datas.req_fields),
                columns: [
                  {
                    prop: "name",
                    label: "字段编码",
                    width: 240,
                  },
                  {
                    prop: "label",
                    label: "字段名称",
                    align: "center",
                    width: 180,
                  },
                  {
                    prop: "field_type",
                    label: "字段类型",
                    align: "center",
                    width: 180,
                  },
                  {
                    prop: "descript",
                    label: "字段说明",
                  },
                  {
                    prop: "is_must",
                    label: "是否必须",
                    align: "center",
                    width: 180,
                  },
                  {
                    prop: "example",
                    label: "示例值",
                    align: "center",
                    width: 180,
                  },
                ],
              },
            },
            {
              name: "响应参数",
              type: "table",
              value: {
                datas: this.getTableData(datas.res_fields),
                columns: [
                  {
                    prop: "name",
                    label: "字段编码",
                    width: 240,
                  },
                  {
                    prop: "label",
                    label: "字段名称",
                    align: "center",
                    width: 180,
                  },
                  {
                    prop: "field_type",
                    label: "字段类型",
                    align: "center",
                    width: 180,
                  },
                  {
                    prop: "descript",
                    label: "字段说明",
                  },
                ],
              },
            },
          ];
          let arr1 = datas.serviceRequestSpcs.spcs_type_1 || [];
          let arr2 = datas.serviceRequestSpcs.spcs_type_2 || [];
          let arr = arr1.concat(arr2);
          arr.map((item) => {
            item.type = item.spcs_type == 1 ? "计次收费规格" : "时长收费规格";
            item.spce =
              item.spcs_type == 1
                ? `${item.money}金币/${item.spcs_count}次（${(
                    item.money / item.spcs_count
                  ).toFixed(2)}金币/1次）`
                : `${item.money}金币/月（次数不限）`;
            item.times = item.spcs_type == 1 ? "不限时长" : "按月";
          });
          this.providerData = {
            organization_name: datas.organization_name,
            picture_path: datas.organization_picture,
            user_name: datas.register_user_info.user_name,
            phone: datas.register_user_info.phone,
            serviceRequestSpcsTable: arr,
          };
          this.commentsData = datas.scoreDetail;
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    getTableData(tableStr) {
      let tabledata = (tableStr && JSON.parse(tableStr)) || [];
      let addRowId = (data, baseId = "") => {
        data.forEach((item, index) => {
          let rowId = baseId + (index + 1) + "";

          item.rowId = rowId;

          if (item.children && item.children.length) {
            addRowId(item.children, rowId);
          }
        });
      };

      addRowId(tabledata);

      return tabledata;
    },
  },
  mounted() {
    this.init();
  },
};
</script>
