import { askAid } from "../../../service/system";
import dayjs from "../../../utils/dayjs";
import { alertInfo, alertSuccess, isValidPhoneNumber, loadingFunc } from "../../../utils/index";
import request from '../../../utils/request'
const { OK } = request

// pages/aid/consult/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { id, type } = options;
    this.setData({ type: Number(type), id });
  },
  onChange(e) {
    const data = e.detail;
    const { name } = e.currentTarget.dataset
    const { form } = this.data;
    form[name] = data;
    this.setData({
      form,
    })
  },
  back() {
    wx.navigateBack()
  },
  async submit() {
    const { form } = this.data;
    const { name, phone, detail } = form;
    if (!name) {
      alertInfo("请输入名字")
      return;
    }
    if (!phone) {
      alertInfo("请输入手机号")
      return;
    }
    if (phone && !isValidPhoneNumber(phone)) {
      alertInfo("手机号格式不正确")
      return
    }
    if (!detail) {
      alertInfo("请输入详情")
      return
    }
    const that = this;
    loadingFunc(async () => {
      const { type, id } = that.data;
      const { code, message } = await askAid({ ...form, type, createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'), connectId: id })
      if (code !== OK) {
        alertInfo(message)
        return
      }
      alertSuccess("创建成功")
      setTimeout(() => {
        wx.navigateBack();
      }, 500)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})