<template>
  <div class="step-one-container">
    <a-form
      :label-col="{ span: 2 }"
      :wrapper-col="{ offset: 1, span: 12 }"
      :colon="false"
    >
      <a-form-item label="落地页名称" v-bind="validateInfos.title">
        <a-input
          placeholder="请输入落地页名称"
          v-model:value="forms.title"
          :maxLength="30"
        />
      </a-form-item>
      <a-form-item label="公众号" v-bind="validateInfos.gzh_name">
        <a-select
          placeholder="请选择公众号"
          v-model:value="forms.gzh_name"
          @change="onGzhChange"
        >
          <template v-for="official in officials" :key="official.gzh_name">
            <a-select-option :value="official.gzh_name">
              {{ official.gzh_name }}
            </a-select-option>
          </template>
        </a-select>
      </a-form-item>
      <a-form-item label="微信号">
        <a-input placeholder="请输入微信号" disabled :value="forms.gzh_code" />
      </a-form-item>
      <a-form-item label="域名" v-bind="validateInfos.domain">
        <a-select
          placeholder="请选择域名"
          v-model:value="forms.domain"
          @change="onDomainChange"
        >
          <template v-for="domain in domains" :key="domain.domain">
            <a-select-option :value="domain.domain">
              {{ domain.domain }}
            </a-select-option>
          </template>
        </a-select>
      </a-form-item>
      <a-form-item label="公司主体">
        <a-input
          placeholder="请输入公司主体"
          disabled
          :value="forms.company_name"
        />
        <!-- <a-select placeholder="请选择公司主体">
          <a-select-option value="1">1</a-select-option>
          <a-select-option value="2">2</a-select-option>
        </a-select> -->
      </a-form-item>
      <a-form-item label="渠道" v-bind="validateInfos.link_source">
        <a-select placeholder="请选择渠道" v-model:value="forms.link_source">
          <a-select-option value="tiktok">抖音</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="公众号后缀">
        <a-radio-group v-model:value="forms.gzh_suffix_mode">
          <a-radio :value="0">无后缀</a-radio>
          <a-radio :value="3">
            官方后缀(推荐)
            <a-tooltip
              title="使用官方后缀时请先设置微信号：系统设置->服务号设置->修改服务号名称->修改微信号同时保证公众号微信官方后台也设置了微信号"
            >
              <InfoCircleOutlined />
            </a-tooltip>
          </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="跳转类型">
        <a-radio-group v-model:value="forms.jump_type">
          <a-radio value="copy_name">文本粘贴</a-radio>
          <!-- <a-radio value="xcx">小程序</a-radio> -->
        </a-radio-group>
      </a-form-item>
      <a-form-item label="关注图片" v-bind="validateInfos.sub_img">
        <image-upload
          name="sub_img"
          type="关注图片"
          :value="forms.sub_img"
          @change="onUploadChange"
        />
      </a-form-item>
      <a-form-item label="公众号头像">
        <image-upload
          name="gzh_img"
          type="公众号头像"
          :value="forms.gzh_img"
          @change="onUploadChange"
        />
      </a-form-item>
      <a-form-item label="简介">
        <a-textarea v-model:value="forms.name" :maxLength="50"></a-textarea>
      </a-form-item>
      <a-form-item :wrapper-col="{ offset: 3 }">
        <a-button @click="onBack" style="margin-right: 10px">返回</a-button>
        <a-button type="primary" @click="onNextStep">下一步</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs,watch } from "vue";
import { InfoCircleOutlined } from "@ant-design/icons-vue";
import { useForm } from "@ant-design-vue/use";
import useApp from "@/hooks/useApp";
import store from "@/store";
import { MutationType } from "@/store/modules/app/_type";
import ImageUpload from "@/components/image-upload/index.vue";

import { getLandingBooks, getLandingDomains, getLandingOfficials } from "@/api";
import { IDomainItem, IGZHItem, IPBookItem } from "#/api";

const StepOne = defineComponent({
  name: "StepOne",
  components: {
    ImageUpload,
    InfoCircleOutlined,
  },
  props: {
    content: Object,
    default() {
      return {};
    },
  },
  emits: ["next"],
  setup(props, { emit }) {
    const { route, router } = useApp();

    const state = reactive({
      officials: ref<IGZHItem[]>([]),
      domains: ref<IDomainItem[]>([]),
      forms: {
        title: "",
        name: "",
        gzh_code: "",
        gzh_name: "",
        bid: "",
        domain: "",
        company_name: "",
        link_source: "tiktok",
        gzh_suffix_mode: 0,
        gzh_suffix: "",
        jump_type: "copy_name",
        sub_img: "",
        gzh_img: "",
        content: "",
      },
    });
    const formsRules = reactive({
      title: [{ required: true, trigger: "blur", message: "请输入落地页名称" }],
      gzh_name: [
        { required: true, trigger: "change", message: "请选择公众号" },
      ],
      domain: [
        {
          required: true,
          trigger: "change",
          message: "请选择域名",
        },
      ],
      link_source: [
        {
          required: true,
          trigger: "change",
          message: "请选择渠道",
        },
      ],
      sub_img: [
        {
          required: true,
          message: "请上传关注图片",
        },
      ],
    });
    watch( (props as any)?.content,(newProps, oldProps) => {
     if (route.query && route.query.id) {
       state.forms = Object.assign(state.forms, (props as any)?.content ?? {});
      }
    });
    const { validate, validateInfos } = useForm(state.forms, formsRules);

    const initConfigData = async () => {
      try {
        const [
          { data: official },
          { data: domains },
        ] = await Promise.all([
          getLandingOfficials(),
          getLandingDomains(),
        ]);
        state.officials = official.list;
        state.domains = domains.list;
        state.forms = Object.assign(state.forms, (props as any)?.content ?? {});
        console.log(state.forms);
      } catch (error) {
        console.log("error happened in initLandingConfig:", error.message);
        router.back();
      }
    };

    initConfigData();

    const onGzhChange = (name: string) => {
      const target = state.officials.find((gzh) => gzh.gzh_name === name);
      // state.forms.channel_id = target!.channel_id.toString() ?? "";
      state.forms.gzh_img = target!.gzh_img ?? "";
      state.forms.sub_img = target!.sub_img ?? "";
      state.forms.gzh_code = target!.gzh_code ?? "";
    };

    const onDomainChange = (domain: string) => {
      const target = state.domains.find((item) => item.domain === domain);
      state.forms.company_name = target!.company_name ?? "";
    };

    const onUploadChange = (result: {
      url: string;
      type: "sub_img" | "gzh_img";
    }) => {
      state.forms[result.type] = result.url;
      //console.log(state.forms);
    };

    const onBack = () => {
      router.replace("/put/landing");
    };

    const onNextStep = async () => {
      try {
        await validate();
        store.commit(MutationType.setTemStepOne, state.forms);
        emit("next", state.forms);
      } catch (error) {
        console.log(error);
      }
    };

    return {
      ...toRefs(state),
      validateInfos,
      onGzhChange,
      onDomainChange,
      onUploadChange,
      onBack,
      onNextStep,
    };
  },
});

export default StepOne;
</script>
