import cityList from './city';
import {url} from '../request'
Page({
  data: {
    cityList,
    errortext:"",
        cascaderValue: ['34', '330'],
        cascaderVisible: false,
        list: ['可上门可到店', '到店服务'],
  },
  handleOk(value, column, e) {
    console.log('onOk value', value, 'onOk  column', column, e);
    if(value=='可上门可到店'){
      this.setData({
        serviceMode:0
      })
    }else{
      this.setData({
        serviceMode:1
      })
    }
    console.log(this.data.serviceMode);
},handleTriggerPicker(visible, e) {
  console.log('onVisibleChange', visible, e);
}, handleDismiss(e) {
  my.showToast({
      content: '取消操作，关闭 picker',
  });
  console.log(e);
},
  onLoad() {},
  ruzhu(){
    if (!this.validateForm()) {
      return;  // 验证未通过，阻止提交
    }
    my.request({
      url: url+'/api/business/add',
      method: 'POST',
      data: {
        address: this.data.selectedOption,
        backIdCard: this.data.backIdCard,
        businessName: this.data.businessName,
        businessPhone: this.data.phone,
        frontIdCard:this.data.frontIdCard,
        license: this.data.yingyeimage,
        shopkeeper: this.data.person,
        userAccount: this.data.userAccount,
        userPassword: this.data.userPassword,
        serviceMode: this.data.serviceMode,
      },
      headers: {
        'content-type': 'application/json', //默认值
      },
      dataType: 'json',
      success: (res)=> {
        this.setData({
          errortext:res.data.description
        })
        if(res.data.code==0){
          my.alert({ content: '正在审核中，请耐心等待' });
          my.navigateBack()
          console.log(res);
        }else{
          my.alert({content:this.data.errortext})
        }
        
      },
      fail: function (error) {
        console.error('fail: ', JSON.stringify(error));
      },
    });
  },
  // 获取数据
  businessName(e) {
    this.setData({
      businessName: e.detail.value,
    });
    console.log(e.detail.value);
  },
  person(e) {
    this.setData({
      person: e.detail.value,
    });
    console.log(e.detail.value);
  },
  phone(e) {
    this.setData({
      phone: e.detail.value,
    });
    console.log(e.detail.value);
  },
  // idcard(e) {
  //   this.setData({
  //     idcard: e.detail.value,
  //   });
  //   console.log(e.detail.value);
  // },
  userAccount(e) {
    this.setData({
      userAccount: e.detail.value,
    });
    console.log(e.detail.value);
  },
  userPassword(e) {
    this.setData({
      userPassword: e.detail.value,
    });
    console.log(e.detail.value);
  },
  // 地点选择
  handleCascaderPickerChange(cascaderValue, selectedOption, e) {
    console.log('cityChange', cascaderValue, selectedOption, e);
},
handleCascaderOnOk(cascaderValue, selectedOption, e) {
  console.log('cityOk', cascaderValue, selectedOption, e);
  const selectedCityLabels = selectedOption.map(option => option.label).join(' ');  

  this.setData({
    selectedOption: selectedCityLabels  
  });

  console.log('Selected cities:', this.data.selectedOption);
},
// 前端校验
validateForm() {
  // 验证店铺名（非空）
  if (!this.data.businessName) {
    my.showToast({
      content: '店铺名不能为空',
      type: 'none',
      duration: 2000,
    });
    return false;
  }
  if (!this.data.userPassword&&this.data.userPassword.length>=8) {
    my.showToast({
      content: '密码不能为空，并且不少于8位',
      type: 'none',
      duration: 2000,
    });
    return false;
  }
  if (!this.data.userAccount) {
    my.showToast({
      content: '账号不能为空',
      type: 'none',
      duration: 2000,
    });
    return false;
  }
  if (!this.data.person) {
    my.showToast({
      content: '姓名不能为空',
      type: 'none',
      duration: 2000,
    });
    return false;
  }
  // 验证电话（简单的正则校验）
  const phoneRegex = /^1[3-9]\d{9}$/;
  if (!this.data.phone || !phoneRegex.test(this.data.phone)) {
    my.showToast({
      content: '请输入有效的手机号码',
      type: 'none',
      duration: 2000,
    });
    return false;
  }

  // 验证身份证号（简单的正则校验）
  // const idcardRegex = /^\d{17}(\d|X)$/;
  // if (!this.data.idcard || !idcardRegex.test(this.data.idcard)) {
  //   my.showToast({
  //     content: '请输入有效的身份证号',
  //     type: 'none',
  //     duration: 2000,
  //   });
  //   return false;
  // }

  // 验证是否选择了城市
  if (!this.data.selectedOption) {
    my.showToast({
      content: '请选择省市',
      type: 'none',
      duration: 2000,
    });
    return false;
  }
  // 验证图片是否上传
  if (!this.data.yingyeimage) {
    my.showToast({
      content: '请上传营业执照',
      type: 'none',
      duration: 2000,
    });
    return false;
  }

  if (!this.data.frontIdCard) {
    my.showToast({
      content: '请上传身份证正面照',
      type: 'none',
      duration: 2000,
    });
    return false;
  }

  if (!this.data.backIdCard) {
    my.showToast({
      content: '请上传身份证反面照',
      type: 'none',
      duration: 2000,
    });
    return false;
  }

  return true; // 所有验证通过
},
onChange(fileList) {

  console.log('图片列表：', fileList);
},
// 营业执照
onUpload(file) {
  return new Promise((resolve, reject) => {
    console.log('上传文件路径：', file); // 确保文件路径正确
    my.uploadFile({
      url: url + '/api/file/upload/server/not_login',
      fileType: 'image',
      name: 'file',
      filePath: file.path,
      formData: { 
        biz: 'card',
       },
      success: res => {
        resolve(file.path);
        console.log('上传成功:', res);
        const cunchu = JSON.parse(res.data)
        this.setData({
          yingyeimage:cunchu.data,
        })
        console.log(this.data.yingyeimage,'hhhhhhhhhhhhhhh'); 
      },
      fail: (err) => {
        console.log('上传失败:', err);
        reject();
      },
    });
  });
},
// 身份证正面
onUpload1(file) {
  return new Promise((resolve, reject) => {
    console.log('上传文件路径：', file); // 确保文件路径正确
    my.uploadFile({
      url: url + '/api/file/upload/server/not_login',
      fileType: 'image',
      name: 'file',
      filePath: file.path,
      formData: { 
        biz: 'card',
       },
      success: res => {
        resolve(file.path);
        console.log('上传成功:', res);
        const cunchu = JSON.parse(res.data)
        this.setData({
          frontIdCard:cunchu.data,
        })
        console.log(this.data.yingyeimage,'hhhhhhhhhhhhhhh'); 
      },
      fail: (err) => {
        console.log('上传失败:', err);
        reject();
      },
    });
  });
},
// 身份证反面
onUpload2(file) {
  return new Promise((resolve, reject) => {
    console.log('上传文件路径：', file); // 确保文件路径正确
    my.uploadFile({
      url: url + '/api/file/upload/server/not_login',
      fileType: 'image',
      name: 'file',
      filePath: file.path,
      formData: { 
        biz: 'card',
       },
      success: res => {
        resolve(file.path);
        console.log('上传成功:', res);
        const cunchu = JSON.parse(res.data)
        this.setData({
          backIdCard:cunchu.data,
        })
        console.log(this.data.yingyeimage,'hhhhhhhhhhhhhhh'); 
      },
      fail: (err) => {
        console.log('上传失败:', err);
        reject();
      },
    });
  });
},
});
