<template>
  <view class="ciy-page">
    <view class="ciy-title mk bg2">
      <view class="title">新增实验室成员</view>
      <button class="btn def sm" @click="goBack">返回</button>
    </view>
    <view class="main bg4 flex1 overflow-auto px4 py3">
      <form class="ciy-form-group">
		<view class="ciy-form">
			<label class="imp">成员姓名</label>
				<view>
					<ciy-input name="name" v-model="form.name" placeholder="请输入成员姓名" required></ciy-input>
				</view>
		</view>
        <view class="ciy-form">
		  <label>头像</label>
		  <view>
		  	<ciy-upload imgwidth="600" imgheight="600" path="demo" name="avar" v-model="form.icon"></ciy-upload>
		  </view>
        </view>
		<view class="ciy-form">
			<label>头衔</label>
			<view>
				<ciy-select :range="g.usertitle" name="menuid" v-model="form.usertitle"></ciy-select>
			</view>
		</view>
		<view class="ciy-form">
			<label>状态</label>
			<view>
				<ciy-radio left :range="g.role" name="sigstatus" v-model="form.role"></ciy-radio>
			</view>
		</view>
		<view class="ciy-form">
			<label>学历</label>
			<view>
				<ciy-select :range="g.education" name="menuid" v-model="form.education"></ciy-select>
			</view>
		</view>
		<view class="ciy-form">
			<label class="imp">编号</label>
				<view>
					<ciy-input name="name" v-model="form.sn" placeholder="请输入成员编号" required></ciy-input>
				</view>
		</view>
		<view class="ciy-form">
			<label>性别</label>
			<view>
				<ciy-radio left :range="g.sex" name="sex" v-model="form.sex"></ciy-radio>
			</view>
		</view>
		<view class="ciy-form">
			<label>加入日期</label>
			<view>
				<ciy-inputdatetime name="date" v-model="form.addtimes"></ciy-inputdatetime>
			</view>
		</view>
		<view class="ciy-form">
			<label>手机号</label>
			<view>
				<ciy-input name="mobile" v-model="form.mobile" placeholder="请输入11位手机号" required></ciy-input>
			</view>
		</view>
		<view class="ciy-form">
			<label>邮箱</label>
			<view>
				<ciy-input name="email" v-model="form.email" placeholder="请输入邮箱地址" required></ciy-input>
			</view>
		</view>
        <view class="ciy-form">
          <label class="char4">初始密码</label>
          <input 
            type="password" 
            v-model="form.pass" 
            placeholder="默认：1"
            class="ciy-edit px2 py1 bg1 rounded"
            :value="form.pass || '1'"
          />
        </view>
      </form>
      <view class="ciy-form-bottom mt4">
        <button 
          class="btn man lgg long" 
          @click="submitForm"
          :disabled="isSubmitting"
        >
          {{ isSubmitting ? '提交中...' : '提交成员信息' }}
        </button>
      </view>
    </view>
  </view>
</template>
<script>
import md5 from '@/util/md5.js';
export default {
  data() {
    return {
      isSubmitting: false, 
      form: {
		    laborgid: 0,
		    stpstatus: 10, 
		    userlevel:10,
		    dvotecnt:0,
        name: '',         
        icon: '',         
        usertitle: 0,      
        role: 30,         
        education: 50,   
        sn: '',          
        sex: 90,          
        addtimesText: '',  
        addtimes: 0,       
        mobile: '',        
        email: '',         
        pass: '1' 
      },
    };
  },
  methods: {
    goBack() {
      uni.navigateBack({ delta: 1 });
    },
    async submitForm() {
      if (!this.form.name) {
        return this.toast('请输入成员姓名');
      }
      if (!this.form.addtimes) {
        return this.toast('请选择加入日期');
      }
      if (this.form.mobile && !/^1[3-9]\d{9}$/.test(this.form.mobile)) {
        return this.toast('请输入有效的11位手机号');
      }
      if (this.form.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.form.email)) {
        return this.toast('请输入有效的邮箱地址');
      }
      this.form.pass = this.form.pass || '1';
      this.isSubmitting=true;
      try{
        const app=getApp();
        const postData={...this.form}
        postData.auth = (new Date()).getTime();
        postData.pass = md5.md5(postData.pass + app.globalData.tokensalt);
        const retjson = await this.callfunc({
          func: 'adduser.add',
          data: postData     
        });
        if (retjson.code !== 1) { 
          return this.toast(retjson.msg || '新增成员失败');
        }
        this.toast('新增成员成功');
        setTimeout(() => {
          this.goBack();
        }, 1500);
        console.log('提交的成员信息（加密后）：', postData);
      }catch(err){
        this.toast('提交失败：' + err.message);
        console.error('提交异常：', err);
      }finally{
         this.isSubmitting = false;
      }
    }
  },
  async saveUser() {
    try {
      // 原有保存逻辑（已确认数据库更新成功）
      const ret = await this.callfunc({ func: 'adduser.edit', data: xxx });
      if (ret.code === 1) {
        this.toast('编辑成功');
      // 返回列表页，并触发刷新
        const pages = getCurrentPages(); // 获取当前页面栈
        const listPage = pages[pages.length - 2]; // 列表页是上一页
        if (listPage && listPage.methods.getList) {
          listPage.methods.getList(); // 直接调用列表页的getList方法
        }
        uni.navigateBack({ delta: 1 });
      }
    } catch (err) {
    // 错误处理
    }
  }
};
</script>

<style scoped>
.ciy-edit {
  width: 100%;
  height: 2rem;
  border: 1px solid var(--bg6);
  border-radius: 0.3rem;
}
.w-12 { width: 3rem; }
.h-12 { height: 3rem; }
</style>