<template>
  <cr-form scroll-to-error @submit="onSubmit" @failed="onFailed" class="container">
    <nav-tab :nav-list="navBarOptions" />
    <detail-header :det="headerInfo" />
    <div class="det-body">
      <card title="保险计划" :option="['查看详情']" @option-click="openIframePupop(4)">
        <cr-radio-btn v-model="formData.planValue" :radio-data="planOptions" />
        <compactCellGroup :cell-data="planCellData" :text-bold="true" />
        <plan-tip :tip-list="planTipOptions" />
      </card>
      <div id="det_insure">
        <insure-person-form v-model="formData" :radio-options="personFormOptions" />
        <auto-deduct
          v-model="formData.auto"
          :cell-data="autoCellInfo"
          @cell-click="openIframePupop"
        />
        <protocol-read
          v-model="formData.autoRenewPolicy"
          :cell-data="noticeCellData"
          @cell-click="openIframePupop"
        />
      </div>
      <card class="intro" id="det_intro">
        <cr-image
          :src="item"
          v-for="(item, index) in introImg"
          :key="index"
          width="100%"
          height="auto"
        />
      </card>
      <cps-case :case-data="caseInfo" />
      <div id="det_process">
        <cps-process :process-data="processDetail" @click="openIframePupop(5)" />
      </div>
      <cps-qa :qa-data="qaInfo" />
    </div>
    <detail-footer :company-info="companyInfo" />
    <copyright />
    <good-action @leftClick="goInsureState = true" :content="goodActionInfo" />
    <popup-with-iframe
      v-model="popupShow"
      :title="pupopData.title"
      :content="pupopData.content"
      :url="pupopData.url"
    >
      <cps-process-detail v-if="currentPupopIndex === 5" :process-data="processDetail" />
      <cps-rate v-if="currentPupopIndex === 6" />
    </popup-with-iframe>
    <go-insure-dialog
      v-model="goInsureState"
      :title="headerInfo.title"
      :pay-data="goodBuyModalInfo"
      @confirm="nextStep"
      @read-protocol="openIframePupop"
    />
  </cr-form>
</template>

<script>
/**
 * @description: 华太轻重疾险 Critical illness insurance
 */
import CrRadioBtn from "@/components/CrRadioBtn";
import Card from "@/components/Card";
import Copyright from "@/components/Copyright";
import GoodAction from "@/components/GoodAction";
import Collapse from "@/components/Collapse";

import DetailHeader from "./modules/DetailHeader";
import DetailFooter from "./modules/DetailFooter";
import CompactCellGroup from "@/components/CompactCellGroup";
import CpsCase from "./modules/CpsCase";
import CpsProcess from "./modules/CpsProcess";
import CpsQa from "./modules/CpsQA";
import PopupWithIframe from "@/components/PopupWithIframe";
import GoInsureDialog from "./modules/GoInsureDialog";
import CpsProcessDetail from "./modules/CpsProcessDetail";
import CpsRate from "./modules/CpsRate";
import PlanTip from "./modules/PlanTip";
import InsurePersonForm from "./modules/InsurePersonForm";
import AutoDeduct from "./modules/AutoDeduct";
import ProtocolRead from "./modules/ProtocolRead";
import NavTab from "./modules/NavTab";

import Detail from "@/api/detail.huatai.zhongjixian";
import { detail, trail } from "@/api/product";

export default {
  name: "GoodsDetail-CII",
  components: {
    DetailHeader,
    DetailFooter,
    CompactCellGroup,
    CrRadioBtn,
    Card,
    // eslint-disable-next-line
    Collapse,
    CpsCase,
    CpsProcess,
    CpsQa,
    Copyright,
    PopupWithIframe,
    GoodAction,
    GoInsureDialog,
    CpsProcessDetail,
    CpsRate,
    PlanTip,
    InsurePersonForm,
    AutoDeduct,
    ProtocolRead,
    NavTab
  },
  data() {
    return {
      ...Detail,
      goodId: "",
      calTimer: null,
      goodActionInfo: {},
      goodBuyModalInfo: [],
      popupShow: false,
      currentPupopIndex: null,
      formData: {
        name: "",
        idNo: "",
        socialSecurity: "1",
        insured: "1",
        type: "1",
        planValue: "3",
        auto: false,
        autoRenewPolicy: true
      },
      goInsureState: false
    };
  },
  watch: {
    headerInfo: {
      deep: true,
      handler(val) {
        const { price, subPrice } = val;
        this.goodActionInfo = {
          title: price,
          sub: subPrice
        };
        this.goodBuyModalInfo = [
          { title: `${price[2]}保费`, value: `${price[0]}${price[1]}` },
          { title: `${subPrice[2]}保费`, value: `${subPrice[0]}${subPrice[1]}${subPrice[3]}` }
        ];
      }
    },
    formDataCpu: {
      deep: true,
      handler(val, oldVal) {
        if (this.calTimer) {
          clearTimeout(this.calTimer);
        }
        const { name, idNo, type } = val;
        const { name: oldName, idNo: oldNo, type: oldType } = oldVal;
        if (name === oldName && idNo === oldNo && type === oldType) return;
        this.calTimer = setTimeout(() => {
          this.calFee({ name, idNo, type });
        }, 600);
      }
    }
  },
  computed: {
    pupopData() {
      const { popupArray, currentPupopIndex } = this;
      return popupArray[currentPupopIndex] || {};
    },
    planCellData() {
      const { planCellInfo } = this;
      const { planValue } = this.formData;
      return planCellInfo[planValue];
    },
    noticeCellData() {
      const { noticeCellInfo } = this;
      const { planValue } = this.formData;
      return noticeCellInfo[planValue];
    },
    personFormOptions() {
      const { insuredOptions, paywayOptions } = this;
      return {
        insuredOptions,
        paywayOptions
      };
    },
    formDataCpu() {
      return JSON.parse(JSON.stringify(this.formData));
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    openIframePupop(index) {
      if (index === 4) {
        this.$router.push({ path: "/goods/plan", query: { type: this.type } });
        return;
      }
      this.currentPupopIndex = index;
      this.popupShow = true;
    },
    onSubmit() {
      this.goInsureState = true;
      sessionStorage.setItem("policyFormData", JSON.stringify(this.formData));
    },
    onFailed(errorInfo) {
      const { errors } = errorInfo;
      this.$notify({ type: "warning", message: errors[0].message });
    },
    nextStep() {
      this.$router.push("/goods/inform");
    },
    async getDetail() {
      this.goodId = this.$route.query.id;
      const res = await detail({ productNo: this.goodId });
      if (res) {
        this.headerInfo.price[0] = res.firstMonthPrice;
        this.headerInfo.subPrice[0] = res.price;
        this.headerInfo.company = res.companyName;
      }
    },
    async calFee({ name, idNo, type }) {
      let { price, subPrice } = this.headerInfo;
      const res = await trail({ name, idNo, type, productNo: this.goodId });
      if (res) {
        price[0] = res.firstPrice;
        subPrice[0] = res.regularPrice;
        this.goodActionInfo = {
          title: price,
          sub: subPrice
        };
        this.goodBuyModalInfo = [
          { title: `${price[2]}保费`, value: `${price[0]}${price[1]}` },
          { title: `${subPrice[2]}保费`, value: `${subPrice[0]}${subPrice[1]}${subPrice[3]}` }
        ];
      }
    }
  }
};
</script>
<style lang="less" src="./index.less" scoped></style>
