import { useState } from "react";
import { View } from "@tarojs/components";

import PageCustom from "@/components/page-custom/index";
import NavBarNormal from "@/components/NavBarNormal/index";

import { useComponentStore } from '@/store/componentStore'
import BottomBar from "@/components/BottomBar";
import WemetaTextarea from "@/components/wemeta-textarea/index";
import Taro, {useRouter, useUnload} from "@tarojs/taro";
import { EComponentType } from "@/consts/enum";
export default function Index() {
  let currentComponent = useComponentStore((state)=>  state.currentComponent);
  const { saveComponent } = useComponentStore();
  const [value, setValue] = useState(currentComponent?.data?.text ?? '');
  
  
  const handleSubmit = async () => {
    if(!currentComponent?.data.id){
      return
    }
    if(value.length <= 0){
      return;
    }
    const c = {
      data: {
        text: value,
        layout: currentComponent?.data?.layout ?? 'center',
        id: currentComponent?.data.id,
        index: currentComponent?.data?.index,
      },
      enabled: currentComponent?.enabled ?? true,
      type: EComponentType.title,
    };

    
    await saveComponent(c)

    Taro.navigateBack()
  };

  const onChange = (e: any) => {
    setValue(e);
  };


  
  
  

  return (
    <PageCustom bgColor="global-light-green-bg">
      <NavBarNormal backText="返回">章节标题</NavBarNormal>
      <View className="flex flex-col items-center w-full">
        <View className="w-full p-16">
            <WemetaTextarea
              value={value}
              onBlur={(value: string) => onChange(value)}
              onInput={(value: string) => onChange(value)}
              placeholder="章节标题"
              autoFocus
            />
          </View>
        <BottomBar>
          <View className="button-rounded button-primary flex-1" onClick={handleSubmit}>保存</View>
        </BottomBar>
      </View>
    </PageCustom>
  );
}
