<template>
  <view class="page-wrapper global-wrapper bgF2">
    <JHeaderTitle :title="team.teamId ? '编辑团队' : '创建团队'" bgColor="#f2f2f2" />
    <JMainCard pd="0" wrapPd="0 30rpx">
      <JInput
        v-model:value="team.teamName"
        name="团队名称"
        place="请输入团队名称"
        :isBorder="true"
        :rules="{ name: 'teamName', rule: 'REG_NotNUll' }"
      ></JInput>
      <JInput
        v-model:value="team.teamNo"
        name="团队编号"
        place="请输入团队编号"
        :rules="{ name: 'teamNo', rule: 'REG_NotNUll' }"
        :isBorder="true"
      ></JInput>
    </JMainCard>
    <JMainCard wrapPd="30rpx" pd="0">
      <JInput name="备注" place="请输入团队备注" v-model:value="team.remark" :isBorder="true"> </JInput>
    </JMainCard>
    <JButton pd="0 30rpx 50rpx" @HandleTouch="addTeam">{{ team.teamId ? "保存" : "创建团队" }}</JButton>
  </view>
</template>

<script setup>
import { ref } from "vue"
import { $addTeam, $editTeam } from "@/http/apiManager.js"
import { onLoad, onShow } from "@dcloudio/uni-app"
import useStore from "@/hooks/useStore.js"
const { getStore } = useStore()
import JHeaderTitle from "@/components/newComponents/JHeaderTitle/JHeaderTitle"
import JMainCard from "@/components/newComponents/JMainCard/JMainCard"
import JInput from "@/components/newComponents/JInput/JInput"
import JButton from "@/components/newComponents/JButton/JButton"
import { validateArray } from "@/hooks/rules"
onShow(() => {
  if (getStore("teamInfo")) {
    team.value = getStore("teamInfo")
  }
})
const team = ref({
  state: 1,
  statRangeType: "year",
})
const change = (val) => {
  team.value.state = val.detail.value ? 1 : 0
}
const addTeam = () => {
  if (team.value.teamId) return editTeam()
  if (validateArray(team.value)) {
    uni.showLoading({
      title: "创建中",
      mask: true,
    })
    $addTeam(team.value).then((res) => {
      uni.hideLoading()

      uni.showToast({
        title: "创建成功",
        icon: "success",
        mask: true,
      })
      uni.navigateBack()
    })
  }
}
const editTeam = () => {
  if (validateArray(team.value)) {
    $editTeam(team.value).then((res) => {
      uni.showToast({
        title: "修改成功",
        icon: "success",
        mask: true,
      })
      uni.navigateBack()
    })
  }
}
</script>
