import App from './App'
import Vue from 'vue'
import './uni.promisify.adaptor'
import uView from './uni_modules/vk-uview-ui';
// import uView from "uview-ui";
import G from './utils/common.js';
import Ajax from './utils/ajax.js';
import apiInfo from './api/index.js';

import gEmpty from './components/empty.vue';
import gLoading from './components/loading.vue';
import gButton from './components/button.vue';
import gListJob from './components/list/job.vue';
import gListApply from './components/list/apply.vue';
import gPanelServer from './components/panel/server.vue';
import gPanelImage from './components/panel/defaultImage.vue';
import gPanelFormItem from './components/panel/formItem.vue';
import gPanelFormSlot from './components/panel/formSlot.vue';
import gPanelCardNum from './components/panel/numCard.vue';
import gPanelCardInfo from './components/panel/infoCard.vue';
import gPanelHr from './components/panel/hr.vue';
import gPanelFond from './components/panel/fond.vue';
import gPanelAbout from './components/panel/about.vue';
import gPanelDetailItem from './components/panel/detailItem.vue';
import gPanelRecord from './components/panel/record.vue';
import gPanelTime from './components/panel/time.vue';
import gPanelTabbar from './components/panel/tabbar.vue';
import gPanelFixed from './components/panel/fixed.vue';
import gFilter from './pages/home/child/filter.vue';
import { createSSRApp } from 'vue'
export function createApp () {
  const app = createSSRApp(App)
  app.config.productionTip = false;

  app.component('g-empty', gEmpty);
  app.component('g-loading', gLoading);
  app.component('g-button', gButton);
  app.component('g-list-job', gListJob);
  app.component('g-list-apply', gListApply);
  app.component('g-panel-server', gPanelServer);
  app.component('g-panel-image', gPanelImage);
  app.component('g-panel-form-item', gPanelFormItem);
  app.component('g-panel-form-slot', gPanelFormSlot);
  app.component('g-panel-card-num', gPanelCardNum);
  app.component('g-panel-card-info', gPanelCardInfo);
  app.component('g-panel-hr', gPanelHr);
  app.component('g-panel-fond', gPanelFond);
  app.component('g-panel-about', gPanelAbout);
  app.component('g-panel-detail-item', gPanelDetailItem);
  app.component('g-panel-record', gPanelRecord);
  app.component('g-panel-time', gPanelTime);
  app.component('g-panel-tabbar', gPanelTabbar);
  app.component('g-panel-fixed', gPanelFixed);
  app.component('g-filter', gFilter);

  app.config.globalProperties.api = apiInfo;
  app.config.globalProperties.G = Object.assign(G, Ajax, {
    /*
     * 检查token,获取最新登录者信息
     */
    checkToken () {
      return new Promise((resolve, reject) => {
        G.Get('/checkToken', '', (res) => {
          uni.setStorageSync("apply-userinfo", res)
          uni.setStorageSync('apply-userinfo-copy', JSON.stringify(res));
          uni.setStorageSync("apply-agencyId", res.user.agencyId); // 单独存储 -- 代理id，方便获取
          uni.setStorageSync("apply-supplierAccount", res.supplierAccount); // 单独存储 -- 是否是发单号，方便获取 0.不是发单号 1.是发单号
          console.log('resresresresresresresres', res);
          G.Get(apiInfo.login_agencyInfo, {}, (aRes) => {
            console.log("获取团队信息:", aRes);
            uni.setStorageSync("agencyInfo", aRes.agency); // 1是创建者 2是普通成员
            uni.setStorageSync("IS_CREATOR", aRes.agency.userId == res.user.id || res.admin == true ? 1 : 2); // 1是创建者 2是普通成员

            // 登录跳转到首页的判断
            uni.setStorageSync("SHOW_INVITE", 1);
            resolve(res)
          });
        },
          function fail (err) {
            console.log(err);
            resolve(err)

          })
      })
    },
    /*
   * 新的检查token,获取最新登录者信息
   */
    checkTokenNew () {
      return new Promise((resolve, reject) => {
        G.Get('/appLoginByToken', '', (res) => {
          console.log('res', res);
          uni.setStorageSync("apply-userinfo", res)
          uni.setStorageSync('apply-userinfo-copy', JSON.stringify(res));
          uni.setStorageSync("apply-agencyId", res.user.agencyId); // 单独存储 -- 代理id，方便获取
          uni.setStorageSync("apply-supplierAccount", res.supplierAccount); // 单独存储 -- 是否是发单号，方便获取 0.不是发单号 1.是发单号

          G.Get(apiInfo.login_agencyInfo, {}, (aRes) => {
            console.log("获取团队信息:", aRes);
            uni.setStorageSync("agencyInfo", aRes.agency); // 1是创建者 2是普通成员
            uni.setStorageSync("IS_CREATOR", aRes.agency.userId == res.user.id || res.admin == true ? 1 : 2); // 1是创建者 2是普通成员

            // 登录跳转到首页的判断
            uni.setStorageSync("SHOW_INVITE", 1);
            resolve(res)
          });
        })
      })
    },

    /* 上传图片
     */
    uploadImg (callback = () => { }, type = 'default', $num = 1, $sourceType = ['album', 'camera']) {
      G.uploadImage($num, (imgRes) => {
        uni.showLoading({
          title: '上传中...',
        })
        uni.uploadFile({
          url: G.ajaxUrl + apiInfo.uploadImage,
          method: "post",
          filePath: JSON.parse(imgRes)[0],
          name: "uploadFile",
          header: Object.assign({
            Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
          }, Ajax.setPublicParams()),
          formData: Ajax.setPublicParams(),
          success: function (res) {
            let imageStr = ''
            imageStr = JSON.parse(res.data).data;

            if (type == 'idcard') {
              // 调用身份证识别
              uni.uploadFile({
                url: G.ajaxUrl + apiInfo.getIDcard,
                filePath: JSON.parse(imgRes)[0],
                name: 'uploadFile',
                header: Object.assign({
                  Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
                }, Ajax.setPublicParams()),
                formData: Ajax.setPublicParams(),
                success: (uploadFileRes) => {
                  let cardData = {
                    status: '上传成功',
                    image: imageStr,
                    info: JSON.parse(uploadFileRes.data).data
                  }
                  uni.hideLoading()
                  callback(cardData);
                }
              });
            } else {
              uni.hideLoading()
              callback({
                status: '上传成功',
                image: imageStr
              });
            }
          },
          fail: function (res) { },
        });
      }, $sourceType);
    },

    /* 上传图片
   */
    uploadImgByCamera (callback = () => { }, type = 'default', $num = 1, $sourceType = ['camera']) {
      G.uploadImage($num, (imgRes) => {
        console.log('imgRes', imgRes);
        uni.showLoading({
          title: '上传中...',
        })
        if (type == 'idcard') {
          that.recognizeTmpIDcard(imgRes, (cardData) => {
            callback(cardData);
          })
        } else {
          uni.uploadFile({
            url: G.ajaxUrl + apiInfo.uploadImage,
            method: "post",
            filePath: JSON.parse(imgRes)[0],
            name: "uploadFile",
            header: Object.assign({
              Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
            }, Ajax.setPublicParams()),
            formData: Ajax.setPublicParams(),
            success: function (res) {
              let imageStr = ''
              imageStr = JSON.parse(res.data).data;
              console.log('imageStr', imageStr);
              uni.hideLoading()
              callback({
                status: '上传成功',
                image: imageStr
              });
            },
            fail: function (res) { },
          });
        } 


      }, $sourceType);
    },
    /* 上传图片
   */
    uploadImgByAlbum (callback = () => { }, type = 'default', $num = 1, $sourceType = ['album']) {
      var that = this;
      G.uploadImage($num, (imgRes) => {
        console.log('imgRes', imgRes);
        uni.showLoading({
          title: '上传中...',
        })
        if (type == 'idcard') {
          that.recognizeTmpIDcard(imgRes, (cardData) => {
            callback(cardData);
          })
        } else {
          uni.uploadFile({
            url: G.ajaxUrl + apiInfo.uploadImage,
            method: "post",
            filePath: JSON.parse(imgRes)[0],
            name: "uploadFile",
            header: Object.assign({
              Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
            }, Ajax.setPublicParams()),
            formData: Ajax.setPublicParams(),
            success: function (res) {
              let imageStr = ''
              imageStr = JSON.parse(res.data).data;
              console.log('imageStr', imageStr);
              uni.hideLoading()
              callback({
                status: '上传成功',
                image: imageStr
              });
            },
            fail: function (res) { },
          });
        }


      }, $sourceType);
    },
    /**
    *  上传临时文件(不需要选择图库) 
    */
    uploadTmpImg ($fileName, callback = () => { }) {
      uni.uploadFile({
        url: G.ajaxUrl + apiInfo.uploadImage,
        method: "post",
        filePath: $fileName,
        name: "uploadFile",
        header: Object.assign({
          Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
        }, Ajax.setPublicParams()),
        formData: Ajax.setPublicParams(),
        success: function (res) {
          let imageStr = ''
          imageStr = JSON.parse(res.data).data;
          callback({
            status: '上传成功',
            image: imageStr
          });
        },
        fail: function (res) { },
      });
    },
    /**
    * 识别临时身份证文件
    */
    recognizeTmpIDcard ($fileName, callback = () => { }) {
      uni.uploadFile({
        url: G.ajaxUrl + apiInfo.uploadImage,
        method: "post",
        filePath: Array.isArray($fileName) ? $fileName[0] : JSON.parse($fileName)[0],
        name: "uploadFile",
        header: Object.assign({
          Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
        }, Ajax.setPublicParams()),
        formData: Ajax.setPublicParams(),
        success: function (res) {
          let imageStr = ''
          imageStr = JSON.parse(res.data).data;
          console.log('imageStr', imageStr);
          uni.uploadFile({
            url: G.ajaxUrl + apiInfo.getIDcard,
            filePath: Array.isArray($fileName) ? $fileName[0] : JSON.parse($fileName)[0],
            name: 'uploadFile',
            header: Object.assign({
              Authorization: 'Bearer ' + uni
                .getStorageSync("apply-token")
            }, Ajax.setPublicParams()),
            formData: Ajax.setPublicParams(),
            success: (uploadFileRes) => {
              let cardData = {
                status: '上传成功',
                image: imageStr,
                info: JSON.parse(uploadFileRes.data)
                  .data
              }
              uni.hideLoading()
              callback(cardData);
            }
          });
        },
        fail: function (res) { },
      });

    },
    /* 上传媒体文件
     */
    uploadMedia (callback = () => { }, type = 'default', $num = 9) {
      G.uploadM($num, (imgRes) => {
        uni.showLoading({
          title: '上传中...',
        })

        console.log('imgRes', imgRes)

        let args = Array.from(JSON.parse(imgRes));
        console.log('args', args)
        // return
        let imgList = []
        args.forEach((item, index) => {
          console.log('item,', item)
          uni.uploadFile({
            url: G.ajaxUrl + apiInfo.uploadImage,
            method: "post",
            filePath: item.tempFilePath,
            name: "uploadFile",
            header: Object.assign({
              Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
            }, Ajax.setPublicParams()),
            formData: Ajax.setPublicParams(),
            success: function (res) {
              console.log('uploadFileRes', res)
              imgList.push(JSON.parse(res.data).data)
              if (imgList.length == args.length) {
                callback({
                  status: '上传成功',
                  imgList: imgList
                });
              }

            },
            fail: function (res) {
              console.log('res', res)
            },
          });
        })
      });
    }
  });
  app.use(uView)
  return {
    app
  }

}



/**
* Vue2
*/
// Vue.component('g-empty', gEmpty);
// Vue.component('g-loading', gLoading);
// Vue.component('g-button', gButton);
// Vue.component('g-list-job', gListJob);
// Vue.component('g-list-apply', gListApply);
// Vue.component('g-panel-server', gPanelServer);
// Vue.component('g-panel-image', gPanelImage);
// Vue.component('g-panel-form-item', gPanelFormItem);
// Vue.component('g-panel-form-slot', gPanelFormSlot);
// Vue.component('g-panel-card-num', gPanelCardNum);
// Vue.component('g-panel-card-info', gPanelCardInfo);
// Vue.component('g-panel-hr', gPanelHr);
// Vue.component('g-panel-fond', gPanelFond);
// Vue.component('g-panel-about', gPanelAbout);
// Vue.component('g-panel-detail-item', gPanelDetailItem);
// Vue.component('g-panel-record', gPanelRecord);
// Vue.component('g-panel-time', gPanelTime);
// Vue.component('g-panel-tabbar', gPanelTabbar);
// Vue.component('g-panel-fixed', gPanelFixed);
// Vue.prototype.api = apiInfo;
// Vue.prototype.G = Object.assign(G, Ajax, {
//   /*
//    * 检查token,获取最新登录者信息
//    */
//   checkToken () {
//     return new Promise((resolve, reject) => {
//       G.Get('/checkToken', '', (res) => {
//         uni.setStorageSync("apply-userinfo", res)
//         uni.setStorageSync('apply-userinfo-copy', JSON.stringify(res));
//         uni.setStorageSync("apply-agencyId", res.user.agencyId); // 单独存储 -- 代理id，方便获取
//         uni.setStorageSync("apply-supplierAccount", res.supplierAccount); // 单独存储 -- 是否是发单号，方便获取 0.不是发单号 1.是发单号

//         resolve(res)
//       })
//     })
//   },
//   /*
//  * 新的检查token,获取最新登录者信息
//  */
//   checkTokenNew () {
//     return new Promise((resolve, reject) => {
//       G.Get('/appLoginByToken', '', (res) => {
//         console.log('res', res);
//         uni.setStorageSync("apply-userinfo", res)
//         uni.setStorageSync('apply-userinfo-copy', JSON.stringify(res));
//         uni.setStorageSync("apply-agencyId", res.user.agencyId); // 单独存储 -- 代理id，方便获取
//         uni.setStorageSync("apply-supplierAccount", res.supplierAccount); // 单独存储 -- 是否是发单号，方便获取 0.不是发单号 1.是发单号

//         resolve(res)
//       })
//     })
//   },

//   /* 上传图片
//    */
//   uploadImg (callback = () => { }, type = 'default', $num = 1, $sourceType = ['album', 'camera']) {
//     G.uploadImage($num, (imgRes) => {
//       uni.showLoading({
//         title: '上传中...',
//       })
//       uni.uploadFile({
//         url: G.ajaxUrl + apiInfo.uploadImage,
//         method: "post",
//         filePath: JSON.parse(imgRes)[0],
//         name: "uploadFile",
//         header: Object.assign({
//           Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
//         }, Ajax.setPublicParams()),
//         formData: Ajax.setPublicParams(),
//         success: function (res) {
//           let imageStr = ''
//           imageStr = JSON.parse(res.data).data;

//           if (type == 'idcard') {
//             // 调用身份证识别
//             uni.uploadFile({
//               url: G.ajaxUrl + apiInfo.getIDcard,
//               filePath: JSON.parse(imgRes)[0],
//               name: 'uploadFile',
//               header: Object.assign({
//                 Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
//               }, Ajax.setPublicParams()),
//               formData: Ajax.setPublicParams(),
//               success: (uploadFileRes) => {
//                 let cardData = {
//                   status: '上传成功',
//                   image: imageStr,
//                   info: JSON.parse(uploadFileRes.data).data
//                 }
//                 uni.hideLoading()
//                 callback(cardData);
//               }
//             });
//           } else {
//             uni.hideLoading()
//             callback({
//               status: '上传成功',
//               image: imageStr
//             });
//           }
//         },
//         fail: function (res) { },
//       });
//     }, $sourceType);
//   },
//   /**
//   *  上传临时文件(不需要选择图库) 
//   */
//   uploadTmpImg ($fileName, callback = () => { }) {
//     uni.uploadFile({
//       url: G.ajaxUrl + apiInfo.uploadImage,
//       method: "post",
//       filePath: $fileName,
//       name: "uploadFile",
//       header: Object.assign({
//         Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
//       }, Ajax.setPublicParams()),
//       formData: Ajax.setPublicParams(),
//       success: function (res) {
//         let imageStr = ''
//         imageStr = JSON.parse(res.data).data;
//         callback({
//           status: '上传成功',
//           image: imageStr
//         });
//       },
//       fail: function (res) { },
//     });
//   },
//   /* 上传媒体文件
//    */
//   uploadMedia (callback = () => { }, type = 'default', $num = 9) {
//     G.uploadM($num, (imgRes) => {
//       uni.showLoading({
//         title: '上传中...',
//       })

//       console.log('imgRes', imgRes)

//       let args = Array.from(JSON.parse(imgRes));
//       console.log('args', args)
//       // return
//       let imgList = []
//       args.forEach((item, index) => {
//         console.log('item,', item)
//         uni.uploadFile({
//           url: G.ajaxUrl + apiInfo.uploadImage,
//           method: "post",
//           filePath: item.tempFilePath,
//           name: "uploadFile",
//           header: Object.assign({
//             Authorization: 'Bearer ' + uni.getStorageSync("apply-token")
//           }, Ajax.setPublicParams()),
//           formData: Ajax.setPublicParams(),
//           success: function (res) {
//             console.log('uploadFileRes', res)
//             imgList.push(JSON.parse(res.data).data)
//             if (imgList.length == args.length) {
//               callback({
//                 status: '上传成功',
//                 imgList: imgList
//               });
//             }

//           },
//           fail: function (res) {
//             console.log('res', res)
//           },
//         });
//       })
//     });
//   }
// });
// Vue.use(uView)
// const app = new Vue({
//   ...App
// })
// app.$mount()





