<template>
  <view class="container" style="paddingBottom:1rpx;">
    <u-navbar
    leftIconSize="40rpx"
    leftIconColor="#333333"
    title="成员管理"
  >
  </u-navbar>
    <view class="section">
      <view class="section-title">{{ title}}</view>
      <view class="form-view">
        <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" label-width="auto"
          :labelStyle="{ color: '#333333', fontSize: '30rpx' }">
          <u-form-item label="姓名" prop="name" required >
              <u--input v-model="form.name"  placeholder="请填写姓名"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="昵称" prop="nickName" required >
              <u--input v-model="form.nickName"  placeholder="请填写昵称"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
          <u-form-item label="类型" prop="typeName" required @click="handleType">
              <u--input v-model="form.typeName" disabled disabledColor="#ffffff" placeholder="请选择类型"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
          
            <u-form-item label="生日" prop="birthday" required @click="selectBirthday()">
            <u--input v-model="form.birthday" disabled disabledColor="#ffffff" placeholder="请选择发生时间" inputAlign="right" border="none"></u--input>
          <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>
          <u-form-item label="排序" prop="ranking" >
              <u--input v-model="form.ranking" type="number" placeholder="请填写排序"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="身高" prop="height" >
              <u--input v-model="form.height"  placeholder="请填写身高"
                inputAlign="right" border="none">
                <template #suffix>
					<up-text
						text="CM"
					></up-text>
				</template></u--input>
            </u-form-item>
            <u-form-item label="体重" prop="weight" >
              <u--input v-model="form.weight"  placeholder="请填写体重"
                inputAlign="right" border="none">
                <template #suffix>
					<up-text
						text="KG"
					></up-text>
				</template></u--input>
            </u-form-item>
           
          <u-form-item label="备注" prop="remark"  labelPosition="top">
            <u--textarea v-model="form.remark" placeholder="请填写备注" border="none" autoHeight inputAlign="right" count
              maxlength="2000" style="padding:18rpx 0;"></u--textarea>
          </u-form-item>
        </u--form>
        <view class="form-btn">
          <u-button type="primary" text="提交" @click="submit"></u-button>
        </view>
      </view>
    </view>
    <u-toast ref="uToast"></u-toast>
        <u-picker itemHeight="88" :show="showType" :columns="typeList" keyName="dictLabel" @cancel="handleTypeCancel"
        @confirm="handleTypeConfirm"></u-picker>
      <u-datetime-picker
			:show="birthdayShow"
			mode="date"
      ref="birthdayRef" 
			@cancel="birthdayShow=false"
			@confirm="birthdayConfirm"
			itemHeight="88"
		></u-datetime-picker>
  </view>
</template>

<script setup>
import { getPerson,addPerson, updatePerson } from '@/api/health/person'
const { proxy } = getCurrentInstance()
  import { getDicts } from '@/api/system/dict/data.js'
import dayjs from 'dayjs'
import {onLoad,onReady} from "@dcloudio/uni-app";
  // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed ,getCurrentInstance }from "vue";
const birthdayShow = ref(false)
const title = ref("成员管理")

const typeList = ref([])
const showType = ref(false)
const data = reactive({
form: {
  id: null,
  id: null,
    name: null,
    type: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    birthday: null,
    nickName: null,
    height: null,
    weight: null,
    ranking: 0
},
rules: {
  name: [{ required: true, message: '姓名不能为空', trigger:['change', 'blur'] }],
  nickName: [{ required: true, message: '昵称不能为空', trigger: ['change', 'blur'] }],
  birthday: [{ required: true, message: '生日不能为空', trigger: ['change', 'blur'] }],
  typeName: [{ required: true, message: '类型不能为空', trigger: ['change', 'blur'] }],
}
})
const { form, rules} = toRefs(data)


onLoad((option) => {
     form.value.id = option.id
     if(form.value.id!=null){
      title.value="成员管理-修改"
     }else{
      title.value="成员管理-新增"
     }
    getData()
})
onReady(() => {
     form.value.birthday = dayjs(new Date().getTime()).format("YYYY-MM-DD")
})
function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
 }
function  getData() {
     // 类型
     getDicts('person_type').then(res => {
        typeList.value =[res.data] 
      })
    if(form.value.id!=null){
      getPerson(form.value.id).then(res => {
        form.value = res.data   
            // 类型
           getDicts('person_type').then(result => {
          form.value.typeName=dictStr(form.value.type, result.data)
         })
      })
    }
}


  function handleType() {
        if (typeList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '类型为空  ', type: 'warning'
          })
        } else {
          showType.value = true
        }
     }
  function   handleTypeConfirm(e) {
        form.value.typeName = e.value[0].dictLabel
        form.value.type = e.value[0].dictValue
        showType.value = false
  }
 function handleTypeCancel() {
       showType.value = false
      }

function  selectBirthday() {
      birthdayShow.value = true
      proxy.$refs['birthdayRef'].innerValue = new Date().getTime()
  }
function birthdayConfirm(e) {
      form.value.birthday = dayjs(e.value).format("YYYY-MM-DD")
      birthdayShow.value = false
  }


function submit() {
      proxy.$refs['uForm'].validate().then(() => {
        if (form.value.id != null) {
          updatePerson(form.value).then(res => {
            proxy.$refs['uToast'].show({
            message: '修改成功', complete() {
              uni.navigateTo({ url: `/pages/health/person/list` })
            }
          })
        })
        }else {
          addPerson(form.value).then(res => {
            proxy.$refs['uToast'].show({
            message: '新增成功', complete() {
              uni.navigateTo({ url: `/pages/health/person/list` })
            }
          })
        })
        }
      })
    }
</script>

<style lang="scss" scoped>
.section {
  margin: 24rpx;
  padding: 16rpx 24rpx;
  background-color: #fff;
  border-radius: 8rpx;

  .section-title {
    width: 360rpx;
    color: #333333;
    line-height: 44rpx;
    font-size: 30rpx;
    border-left: 6rpx solid #2681FF;
    padding-left: 26rpx;
  }

  .form-view {
    padding: 20rpx 0rpx 0 10rpx;

    .form-btn {
      padding-top: 20rpx;
    }
  }
}
</style>