管灌系统农户端微信小程序(嘉峪关应用)
pages/createIrrigation/createIrrigation.js
@@ -1,538 +1,450 @@
const app = getApp();
const { get, post } = require('../../api/request');
const dayjs = require('dayjs');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 表单数据
    planCode: '', // 计划编号
    startTime: '', // 灌溉开始时间
    pickerValue: '', // 时间选择器的值
    timePickerVisible: false, // 时间选择器是否可见
    // 项目和轮灌组数据
    projectList: [
      {
        id: '1',
        name: '项目一',
        expanded: false, // 是否展开
        totalDuration: 0, // 总时长
        groups: [
          { id: '1-1', name: '轮灌组1', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 }
        ]
      },
      {
        id: '2',
        name: '项目二',
        expanded: false,
        totalDuration: 0,
        groups: [
          { id: '2-1', name: '轮灌组A', selected: false, duration: 0 },
          { id: '2-2', name: '轮灌组B', selected: false, duration: 0 }
        ]
      },
      {
        id: '4',
        name: '项目一',
        expanded: false, // 是否展开
        totalDuration: 0, // 总时长
        groups: [
          { id: '1-1', name: '轮灌组1', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 }
        ]
      },
      {
        id: '5',
        name: '项目一',
        expanded: false, // 是否展开
        totalDuration: 0, // 总时长
        groups: [
          { id: '1-1', name: '轮灌组1', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 }
        ]
      },
      {
        id: '6',
        name: '项目一',
        expanded: false, // 是否展开
        totalDuration: 0, // 总时长
        groups: [
          { id: '1-1', name: '轮灌组1', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 }
        ]
      },
      {
        id: '7',
        name: '项目一',
        expanded: false, // 是否展开
        totalDuration: 0, // 总时长
        groups: [
          { id: '1-1', name: '轮灌组1', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 }
        ]
      },
      {
        id: '8',
        name: '项目8',
        expanded: false, // 是否展开
        totalDuration: 0, // 总时长
        groups: [
          { id: '1-1', name: '轮灌组1', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 },
          { id: '1-2', name: '轮灌组2', selected: false, duration: 0 },
          { id: '1-3', name: '轮灌组3', selected: false, duration: 0 }
        ]
      },
      {
        id: '3',
        name: '项目3',
        expanded: false,
        totalDuration: 0,
        groups: [
          { id: '3-1', name: '轮灌组X', selected: false, duration: 0 },
          { id: '3-2', name: '轮灌组Y', selected: false, duration: 0 },
          { id: '3-3', name: '轮灌组Z', selected: false, duration: 0 }
        ]
      },
      {
        id: '9',
        name: '项目9',
        expanded: false,
        totalDuration: 0,
        groups: [
          { id: '3-1', name: '轮灌组X', selected: false, duration: 0 },
          { id: '3-2', name: '轮灌组Y', selected: false, duration: 0 },
          { id: '3-3', name: '轮灌组Z', selected: false, duration: 0 }
        ]
      }
      ,
      {
        id: '10',
        name: '项目10',
        expanded: false,
        totalDuration: 0,
        groups: [
          { id: '3-1', name: '轮灌组X', selected: false, duration: 0 },
          { id: '3-2', name: '轮灌组Y', selected: false, duration: 0 },
          { id: '3-3', name: '轮灌组Z', selected: false, duration: 0 }
        ]
      }
      ,
      {
        id: '11',
        name: '项目11',
        expanded: false,
        totalDuration: 0,
        groups: [
          { id: '3-1', name: '轮灌组X', selected: false, duration: 0 },
          { id: '3-2', name: '轮灌组Y', selected: false, duration: 0 },
          { id: '3-3', name: '轮灌组Z', selected: false, duration: 0 }
        ]
      }
    ]
  },
    /**
     * 页面的初始数据
     */
    data: {
        // 表单数据
        planCode: '', // 计划编号
        startTime: '', // 灌溉开始时间
        pickerValue: '', // 时间选择器的值
        timePickerVisible: false, // 时间选择器是否可见
        timeInfoVisible: false, // 时间提示弹窗是否可见
        // 项目选择器相关
        projectPickerVisible: false,
        projectPickerValue: [],
        selectedProject: null,
        projectOptions: [],
        totalDuration: 0, // 添加总灌溉时间
        // 项目列表
        projectList: [],
        // 时间选择器选项
        timeOptions: [],
        // 轮灌组列表刷新状态
        isRefreshing: false
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面加载时可以从API获取项目和轮灌组数据
    // this.fetchProjectsAndGroups();
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 生成计划编号
        this.generatePlanCode();
        // 设置时间选择器的初始值
        const now = dayjs();
        this.setData({
            pickerValue: now.add(8.5, 'hour').format('YYYY-MM-DD HH:mm')
        });
        // 获取项目列表
        this.fetchProjects();
    },
  /**
   * 获取项目和轮灌组数据
   */
  fetchProjectsAndGroups: function () {
    // 这里可以添加API请求逻辑,获取真实数据
    // wx.request({
    //   url: 'your-api-url',
    //   success: (res) => {
    //     this.setData({
    //       projectList: res.data
    //     });
    //   }
    // });
  },
    /**
     * 生成计划编号
     */
    generatePlanCode: function () {
        const now = dayjs();
        const dateStr = now.format('YYYYMMDD');
        const randomNum = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
        const planCode = `${dateStr}${randomNum}`;
        this.setData({ planCode });
    },
  /**
   * 处理计划编号输入
   */
  onPlanCodeInput: function (e) {
    this.setData({
      planCode: e.detail.value
    });
  },
    /**
     * 获取项目列表
     */
    fetchProjects: function () {
        return get({
            url: '/wx/irrigation/getSimpleProjects',
            isShowLoding: true
        }).then(res => {
            if (res.success) {
                const projectList = res.content.obj.map(project => ({
                    id: project.projectId,
                    name: project.projectName,
                    groupCount: project.groupCount,
                    groups: []
                }));
  /**
   * 显示时间选择器
   */
  showTimePicker: function () {
    this.setData({
      timePickerVisible: true
    });
  },
                const projectOptions = projectList.map(project => ({
                    label: project.name,
                    value: project.id
                }));
  /**
   * 时间选择器确认回调
   */
  onTimePickerConfirm: function (e) {
    this.setData({
      startTime: e.detail.formatValue,
      pickerValue: e.detail.value,
      timePickerVisible: false
    });
  },
                this.setData({
                    projectList,
                    projectOptions
                });
            } else {
                wx.showToast({
                    title: res.msg || '获取项目列表失败',
                    icon: 'none'
                });
                return Promise.reject(new Error(res.msg || '获取项目列表失败'));
            }
        }).catch(err => {
            console.error('获取项目列表失败:', err);
            wx.showToast({
                title: '获取项目列表失败',
                icon: 'none'
            });
            return Promise.reject(err);
        });
    },
  /**
   * 时间选择器取消回调
   */
  onTimePickerCancel: function () {
    this.setData({
      timePickerVisible: false
    });
  },
    /**
     * 获取轮灌组列表
     */
    fetchGroups: function (projectId) {
        return get({
            url: '/wx/irrigation/getSimpleGroups',
            data: {
                projectId: projectId,
            },
            isShowLoding: true
        }).then(res => {
            if (res.success) {
                console.log('轮灌组数据:', res.content.obj);
  /**
   * 切换项目展开/折叠状态
   */
  toggleProject: function (e) {
    const index = e.currentTarget.dataset.index;
    const currentValue = this.data.projectList[index].expanded;
    // 创建新的项目列表,先将所有项目设为折叠状态
    const newProjectList = this.data.projectList.map((item, idx) => {
      return {
        ...item,
        expanded: false
      };
    });
    // 如果当前点击的项目已经是展开状态,则保持所有项目折叠
    // 否则,将当前点击的项目设为展开状态
    if (!currentValue) {
      newProjectList[index].expanded = true;
    }
    this.setData({
      projectList: newProjectList
    });
  },
                // 更新选中项目的轮灌组信息
                const projectList = this.data.projectList.map(project => {
                    if (project.id === projectId) {
                        return {
                            ...project,
                            groups: res.content.obj.map(group => ({
                                id: group.groupId,
                                name: group.groupCode,
                                duration: group.defaultDuration || 0,
                                selected: false,
                                intakeCount: group.intakeCount
                            }))
                        };
                    }
                    return project;
                });
  /**
   * 切换轮灌组选中状态
   */
  toggleGroupSelection: function (e) {
    const projectIndex = e.currentTarget.dataset.projectIndex;
    const groupIndex = e.currentTarget.dataset.groupIndex;
    const key = `projectList[${projectIndex}].groups[${groupIndex}].selected`;
    const currentValue = this.data.projectList[projectIndex].groups[groupIndex].selected;
    this.setData({
      [key]: !currentValue
    });
    // 更新项目总时长
    this.updateProjectTotalDuration(projectIndex);
  },
                // 更新选中的项目
                const selectedProject = projectList.find(project => project.id === projectId);
                console.log('更新后的选中项目:', selectedProject);
  /**
   * 处理时长输入
   */
  onDurationInput: function (e) {
    const projectIndex = e.currentTarget.dataset.projectIndex;
    const groupIndex = e.currentTarget.dataset.groupIndex;
    const value = parseInt(e.detail.value) || 0;
    // 更新轮灌组时长
    const durationKey = `projectList[${projectIndex}].groups[${groupIndex}].duration`;
    this.setData({
      [durationKey]: value
    });
    // 自动选中该轮灌组
    const selectedKey = `projectList[${projectIndex}].groups[${groupIndex}].selected`;
    if (value > 0 && !this.data.projectList[projectIndex].groups[groupIndex].selected) {
      this.setData({
        [selectedKey]: true
      });
    }
    // 计算并更新项目总时长
    this.updateProjectTotalDuration(projectIndex);
  },
                this.setData({
                    projectList,
                    selectedProject
                }, () => {
                    // 计算总时间
                    this.calculateTotalDuration();
                });
            } else {
                wx.showToast({
                    title: res.msg || '获取轮灌组列表失败',
                    icon: 'none'
                });
                return Promise.reject(new Error(res.msg || '获取轮灌组列表失败'));
            }
        }).catch(err => {
            console.error('获取轮灌组列表失败:', err);
            wx.showToast({
                title: '获取轮灌组列表失败',
                icon: 'none'
            });
            return Promise.reject(err);
        });
    },
  /**
   * 计算并更新项目总时长
   */
  updateProjectTotalDuration: function (projectIndex) {
    const project = this.data.projectList[projectIndex];
    let totalDuration = 0;
    // 计算所有选中的轮灌组的时长总和
    project.groups.forEach(group => {
      if (group.selected) {
        totalDuration += parseInt(group.duration) || 0;
      }
    });
    // 更新项目总时长
    const totalDurationKey = `projectList[${projectIndex}].totalDuration`;
    this.setData({
      [totalDurationKey]: totalDuration
    });
  },
    /**
     * 显示时间选择器
     */
    showTimePicker: function () {
        // 如果没有选择时间,使用当前时间
        if (!this.data.pickerValue) {
            const now = dayjs();
            this.setData({
                pickerValue: now.format('YYYY-MM-DD HH:mm')
            });
        }
  /**
   * 阻止事件冒泡
   */
  stopPropagation: function (e) {
    // 阻止事件冒泡,防止点击输入框时触发父元素的点击事件
  },
        this.setData({
            timePickerVisible: true
        });
    },
  /**
   * 跳转到轮灌组详情页
   */
  navigateToGroupDetail: function (e) {
    const projectIndex = e.currentTarget.dataset.projectIndex;
    const groupIndex = e.currentTarget.dataset.groupIndex;
    const project = this.data.projectList[projectIndex];
    const group = project.groups[groupIndex];
    wx.navigateTo({
      url: `/pages/groupDetail/groupDetail?projectId=${project.id}&projectName=${project.name}&groupId=${group.id}&groupName=${group.name}`
    });
  },
    /**
     * 时间选择器确认回调
     */
    onTimePickerConfirm: function (e) {
        const { value } = e.detail;
        this.setData({
            timePickerVisible: false,
            startTime: value
        });
    },
  /**
   * 确认按钮点击事件
   */
  onConfirm: function () {
    // 验证表单
    if (!this.validateForm()) {
      return;
    }
    // 收集选中的轮灌组
    const selectedGroups = this.getSelectedGroups();
    // 构建提交数据
    const formData = {
      planCode: this.data.planCode,
      startTime: this.data.startTime,
      groups: selectedGroups
    };
    // 提交数据
    this.submitForm(formData);
  },
    /**
     * 时间选择器取消回调
     */
    onTimePickerCancel: function () {
        this.setData({
            timePickerVisible: false
        });
    },
  /**
   * 验证表单
   */
  validateForm: function () {
    if (!this.data.planCode) {
      wx.showToast({
        title: '请输入计划编号',
        icon: 'none'
      });
      return false;
    }
    if (!this.data.startTime) {
      wx.showToast({
        title: '请选择灌溉开始时间',
        icon: 'none'
      });
      return false;
    }
    const selectedGroups = this.getSelectedGroups();
    if (selectedGroups.length === 0) {
      wx.showToast({
        title: '请至少选择一个轮灌组',
        icon: 'none'
      });
      return false;
    }
    // 检查选中的轮灌组是否都设置了时长
    const invalidGroups = selectedGroups.filter(group => !group.duration);
    if (invalidGroups.length > 0) {
      wx.showToast({
        title: '请为所有选中的轮灌组设置时长',
        icon: 'none'
      });
      return false;
    }
    return true;
  },
    /**
     * 切换项目展开/折叠状态
     */
    toggleProject: function (e) {
        const index = e.currentTarget.dataset.index;
        const currentValue = this.data.projectList[index].expanded;
  /**
   * 获取选中的轮灌组
   */
  getSelectedGroups: function () {
    const selectedGroups = [];
    this.data.projectList.forEach(project => {
      project.groups.forEach(group => {
        if (group.selected) {
          selectedGroups.push({
            projectId: project.id,
            projectName: project.name,
        // 创建新的项目列表,先将所有项目设为折叠状态
        const newProjectList = this.data.projectList.map((item, idx) => {
            return {
                ...item,
                expanded: false
            };
        });
        // 如果当前点击的项目已经是展开状态,则保持所有项目折叠
        // 否则,将当前点击的项目设为展开状态
        if (!currentValue) {
            newProjectList[index].expanded = true;
        }
        this.setData({
            projectList: newProjectList
        });
    },
    /**
     * 切换轮灌组选中状态
     */
    toggleGroupSelection: function (e) {
        const projectIndex = e.currentTarget.dataset.projectIndex;
        const groupIndex = e.currentTarget.dataset.groupIndex;
        const key = `projectList[${projectIndex}].groups[${groupIndex}].selected`;
        const currentValue = this.data.projectList[projectIndex].groups[groupIndex].selected;
        this.setData({
            [key]: !currentValue
        });
        // 更新项目总时长
        this.updateProjectTotalDuration(projectIndex);
    },
    /**
     * 处理时长输入
     */
    onDurationInput: function (e) {
        const { groupIndex } = e.currentTarget.dataset;
        const duration = parseInt(e.detail.value) || 0;
        const selectedProject = { ...this.data.selectedProject };
        selectedProject.groups[groupIndex].duration = duration;
        this.setData({
            selectedProject
        }, () => {
            // 输入时长后重新计算总时间
            this.calculateTotalDuration();
        });
    },
    /**
     * 计算并更新项目总时长
     */
    updateProjectTotalDuration: function (projectIndex) {
        const project = this.data.projectList[projectIndex];
        let totalDuration = 0;
        // 计算所有选中的轮灌组的时长总和
        project.groups.forEach(group => {
            if (group.selected) {
                totalDuration += parseInt(group.duration) || 0;
            }
        });
        // 更新项目总时长
        const totalDurationKey = `projectList[${projectIndex}].totalDuration`;
        this.setData({
            [totalDurationKey]: totalDuration
        });
    },
    /**
     * 阻止事件冒泡
     */
    stopPropagation: function (e) {
        if (e && e.stopPropagation) {
            e.stopPropagation();
        }
        return false;
    },
    /**
     * 跳转到轮灌组详情页
     */
    navigateToGroupDetail: function (e) {
        const { groupIndex } = e.currentTarget.dataset;
        // TODO: 实现跳转逻辑
    },
    /**
     * 确认按钮点击事件
     */
    onConfirm: function () {
        const { planCode, startTime, selectedProject } = this.data;
        if (!planCode) {
            wx.showToast({
                title: '请输入计划编号',
                icon: 'none'
            });
            return;
        }
        if (!selectedProject) {
            wx.showToast({
                title: '请选择项目',
                icon: 'none'
            });
            return;
        }
        // 构建上报数据
        const schedules = selectedProject.groups.map(group => ({
            groupId: group.id,
            groupName: group.name,
            duration: group.duration
          });
        }
      });
    });
    return selectedGroups;
  },
            duration: parseInt(group.duration) || 0
        }));
  /**
   * 提交表单数据
   */
  submitForm: function (formData) {
    // 这里可以添加API请求逻辑,提交表单数据
    console.log('提交的表单数据:', formData);
    // 模拟API请求
    wx.showLoading({
      title: '提交中...',
    });
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '创建成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          // 延迟返回上一页
          setTimeout(() => {
            wx.navigateBack();
          }, 2000);
        const requestData = {
            projectId: selectedProject.id,
            planName: planCode,
            startupMode: startTime ? 2 : 1,
            operatorId: app.globalData.clientId,
            schedules: schedules
        };
        // 如果有开始时间,添加到请求数据中
        if (startTime) {
            requestData.planStartTime = startTime;
        }
      });
    }, 1500);
    // 实际API请求示例
    // wx.request({
    //   url: 'your-api-url',
    //   method: 'POST',
    //   data: formData,
    //   success: (res) => {
    //     wx.showToast({
    //       title: '创建成功',
    //       icon: 'success',
    //       duration: 2000,
    //       success: () => {
    //         setTimeout(() => {
    //           wx.navigateBack();
    //         }, 2000);
    //       }
    //     });
    //   },
    //   fail: (err) => {
    //     wx.showToast({
    //       title: '创建失败,请重试',
    //       icon: 'none'
    //     });
    //   }
    // });
  }
        // 发送请求
        post({
            url: '/wx/plan/createPlan',
            data: requestData,
            isShowLoding: true
        }).then(res => {
            if (res.success) {
                wx.showToast({
                    title: '创建成功',
                    icon: 'success'
                });
                // 返回上一页
                setTimeout(() => {
                    wx.navigateBack();
                }, 1500);
            } else {
                wx.showToast({
                    title: res.msg || '创建失败',
                    icon: 'none'
                });
            }
        }).catch(err => {
            console.error('创建计划失败:', err);
            wx.showToast({
                title: '创建失败',
                icon: 'none'
            });
        });
    },
    // 显示项目选择器
    showProjectPicker() {
        this.setData({
            projectPickerVisible: true
        });
    },
    // 项目选择器确认
    onProjectPickerConfirm(e) {
        const { value } = e.detail;
        console.log('选择的项目ID:', value[0]); // 添加日志查看数据
        const selectedProject = this.data.projectList.find(project => project.id === value[0]);
        console.log('找到的项目:', selectedProject); // 添加日志查看数据
        this.setData({
            projectPickerVisible: false,
            selectedProject: selectedProject,
            projectPickerValue: value
        }, () => {
            // 选择项目后获取轮灌组列表
            if (selectedProject) {
                this.fetchGroups(selectedProject.id);
            }
        });
    },
    // 项目选择器取消
    onProjectPickerCancel() {
        this.setData({
            projectPickerVisible: false
        });
    },
    /**
     * 计算总灌溉时间
     */
    calculateTotalDuration: function () {
        if (!this.data.selectedProject) return;
        const totalDuration = this.data.selectedProject.groups.reduce((sum, group) => {
            return sum + (parseInt(group.duration) || 0);
        }, 0);
        this.setData({
            totalDuration
        });
    },
    /**
     * 轮灌组列表下拉刷新
     */
    onGroupListRefresh: function () {
        if (!this.data.selectedProject) {
            this.setData({ isRefreshing: false });
            return;
        }
        this.setData({ isRefreshing: true });
        this.fetchGroups(this.data.selectedProject.id)
            .then(() => {
                this.setData({ isRefreshing: false });
            })
            .catch(() => {
                this.setData({ isRefreshing: false });
            });
    },
    /**
     * 显示时间提示弹窗
     */
    showTimeInfo: function () {
        this.setData({
            timeInfoVisible: true
        });
    },
    /**
     * 关闭时间提示弹窗
     */
    onTimeInfoConfirm: function () {
        this.setData({
            timeInfoVisible: false
        });
    },
});