<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-input name="recordno" v-model="form.recordno" placeholder="请输入备案编号"></ciy-input>
          </view>
        </view>
        <view class="ciy-form">
          <label>摘要</label>
          <view>
            <ciy-input type="textarea" name="descs" v-model="form.descs" placeholder="请输入摘要"></ciy-input>
          </view>
        </view>
        <view class="ciy-form">
          <label>预算金额</label>
          <view>
            <ciy-inputbet bet="1000000" unit="万元" name="budgetmoney" v-model="form.budgetmoney"></ciy-inputbet>
          </view>
        </view>
        <view class="ciy-form">
          <label>服务机构</label>
          <view>
            <ciy-input name="serviceorg" v-model="form.serviceorg" placeholder="请输入服务机构"></ciy-input>
          </view>
        </view>

        <view class="ciy-form">
          <label>负责人</label>
          <view>
            <ciy-select :range="page_users" name="principal_id" v-model="form.principal_id"></ciy-select>
          </view>
        </view>
        <view class="ciy-form">
          <label>所属机构</label>
          <view>
            <ciy-select :range="page_orgs" name="laborgid" v-model="form.laborgid"></ciy-select>
          </view>
        </view>
        <view class="ciy-form">
          <label>科研类目</label>
          <view>
            <ciy-select :range="g.rcate" name="rcate" v-model="form.rcate"></ciy-select>
          </view>
        </view>
        <view class="ciy-form">
          <label>课题状态</label>
          <view>
            <ciy-select :range="g.rprojectstatus" name="rprojectstatus" v-model="form.rprojectstatus"></ciy-select>
          </view>
        </view>
        <view class="ciy-form">
          <label>是否公开</label>
          <view>
            <ciy-radio :range="g.isweb" name="isweb" v-model="form.isweb"></ciy-radio>
          </view>
        </view>
        <view class="ciy-form">
          <label>备案方向</label>
          <ciy-selcas
            v-model="form.direction"
            name="direction"
            :range="g.direction"
            placeholder="请选择备案方向"
          ></ciy-selcas>
	
        </view>
        <view class="ciy-form">
          <label>主管部门</label>
          <view>
            <ciy-input name="mdeptname" v-model="form.mdeptname" placeholder="请输入主管部门"></ciy-input>
          </view>
        </view>
        <view class="ciy-form">
          <label>层次</label>
          <view>
            <ciy-select :range="g.mdeptlevel" name="mdeptlevel" v-model="form.mdeptlevel"></ciy-select>
          </view>
        </view>
        <view class="ciy-form">
          <label>进度</label>
          <view>
            <ciy-input name="progress" v-model="form.progress" placeholder="请输入课题进度"></ciy-input>
          </view>
        </view>
        <view class="ciy-form">
          <label>最新进展日期</label>
          <view>
            <ciy-inputdatetime name="lasttimes" v-model="form.lasttimes"></ciy-inputdatetime>
          </view>
        </view>
        <view class="ciy-form">
          <label>备案时间</label>
          <view>
            <ciy-inputdatetime name="recordtime" v-model="form.recordtime"></ciy-inputdatetime>
          </view>
        </view>
        <view class="ciy-form">
          <label>最新附件</label>
          <view>
            <ciy-upload path="demo" name="latestattachment" v-model="form.latestattachment"></ciy-upload>
          </view>
        </view>

        <view class="ciy-form">
          <label>项目成员</label>
          <view>
            <view v-for="(member, index) in form.members" :key="index" class="member-row">
              <ciy-select :range="page_users" v-model="member.uid" placeholder="选择成员"></ciy-select>
              <ciy-select :range="g.prole" v-model="member.prole" placeholder="选择角色"></ciy-select>
              <button type="button" @click="removeMember(index)">-</button>
            </view>
            <button type="button" @click="addMember()">+ 添加成员</button>
          </view>
        </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>
export default {
  props: {
    id: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      isSubmitting: false,
      form: {
        id: 0,
        recordno: '',
        laborgid: 0,
        isweb: 0,
        rcate: 0,
        name: '',
        descs: '',
        mdeptname: '',
        mdeptlevel: 0,
        budgetmoney: 0,
        serviceorg: '',
        principal_id: 0,
        rprojectstatus: 0,
        progress: 0,
        lasttimes: 0,
        recordtime: 0,
        latestcontent: '',
        latestattachment: '',
        direction: 0,
        members: [] 
      },
      page_users: [],
      page_orgs: [],
    };
  },
  async onLoad(options) {
    
    
    if (!options.id || parseInt(options.id) <= 0) {
      this.toast('无效的课题ID，无法进入编辑页面');
      setTimeout(() => this.goBack(), 1500);
      return;
    }
    this.form.id = parseInt(options.id);


    await this.loadPageData();
    await this.getProjectDetail();
  },
  methods: {
    goBack() {
      uni.navigateBack({ delta: 1 });
    },
async loadPageData() {
    const retjson = await this.callfunc({
      func: 'project.addinit',
      //data: { act: 'edit' }
      data: {}
    });
    if (retjson.code === 1) {

      //const rawUsers = retjson.users || [];
      //this.page_users = rawUsers.map(user => ({
        //id: user.id,     
        //name: user.name
      //}));
      //const rawOrgs = retjson.orgs || [];
      //this.page_orgs = rawOrgs.map(org => ({
        //id: org.id, 
        //name: org.name 
      //})); 

  }
},
    async getProjectDetail() {
      try {
        console.log('开始获取项目详情...');
        const retjson = await this.callfunc({
          func: 'project.info',
          data: { id: this.form.id,act: 'edit' }
        });
console.log('项目详情接口返回的完整数据:', retjson); // <-- 打印完整对象
    console.log('项目详情接口返回的 members 字段:', retjson.members); // <-- 打印特定字段
    console.log('项目详情接口返回的 data 字段:', retjson.data); // <-- 打印 data 字段

        if (retjson.code !== 1) {
          throw new Error(retjson.msg || '获取项目信息失败');
        }
        if (!retjson.data || typeof retjson.data !== 'object') {
          throw new Error('项目信息格式异常，请重试');
        }

        const detail = retjson.data;
        this.form = {
          ...this.form,
          ...detail,
     
          lasttimes: detail.lasttimes ? detail.lasttimes * 1000 : 0,
          recordtime: detail.recordtime ? detail.recordtime * 1000 : 0,

          members: (retjson.members || []).map(m => ({
            mid: m.id,
            uid: m.uid,
            prole: m.prole,
            isNew: false, 
            isDeleted: false 
          }))
        };
this.page_users = (retjson.users || []).map(u => ({ id: u.id, name: u.name }));
    this.page_orgs = (retjson.orgs || []).map(o => ({ id: o.id, name: o.name }));


      } catch (err) {
        this.toast(err.message);
        console.error('拉取详情异常：', err);
      }
    },
    addMember() {
      const allRoleIds = Object.keys(this.g.prole);
      const firstRoleId = allRoleIds.length > 0 ? allRoleIds[0] : "10";
      this.form.members.push({
        mid: 0, 
        uid: 0,
        prole: firstRoleId, 
        isNew: true,
        isDeleted: false
      });
    },
    removeMember(index) {
      const member = this.form.members[index];
      if (member.mid) { 
        member.isDeleted = true; 
      } else { 
        this.form.members.splice(index, 1); 
      }
    },
    async submitForm() {
      if (!this.form.name) {
        return this.toast('请输入课题标题');
      }
      if (!this.form.recordno) {
        return this.toast('请输入备案编号');
      }

      this.isSubmitting = true;
      try {
        const submitData = { ...this.form };
        // 转换时间戳
        if (submitData.lasttimes) submitData.lasttimes = Math.floor(submitData.lasttimes / 1000);
        if (submitData.recordtime) submitData.recordtime = Math.floor(submitData.recordtime / 1000);
        console.log("准备提交的完整数据:", JSON.stringify(submitData, null, 2));
        const retjson = await this.callfunc({
          func: 'project.update', 
          data: submitData
        });
      if (retjson.err && retjson.err.length) {
        const errorMsg = retjson.err[0]?.msg || retjson.err[0]?.type || '网络请求异常';
        throw new Error(`请求失败: ${errorMsg}`);
      }
        if (retjson.code !== 1) {
          throw new Error(retjson.msg || '更新课题失败');
        }

        this.toast('更新课题信息成功');
        uni.$emit('project-updated', { id: this.form.id });
        setTimeout(() => this.goBack(), 1500);
      } catch (err) {
        this.toast(err.message);
        console.error('提交编辑异常：', err);
      } finally {
        this.isSubmitting = false;
      }
    }
  },
};
</script>

<style scoped>
.ciy-edit {
  width: 100%;
  height: 2rem;
  border: 1px solid var(--bg6);
  border-radius: 0.3rem;
}
.text-xs {
  font-size: 0.75rem;
}
.text-gray-500 {
  color: #9ca3af;
}
.mt1 {
  margin-top: 0.25rem;
}
.member-row {
  display: flex;
  gap: 0.5em;
  align-items: center;
  margin-bottom: 0.5em;
}
</style>