const mixins = require('@/mixins/index.js')
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util');
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    table: Constants.tables.macTransfer,
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: mixins.$t('collection')
    }],
    contentList: [{
      code: 'need',
      title: mixins.$t('totalAmount'),
      type: 'str'
    }, {
      code: 'amount',
      content: 0,
      type: 'number'
    }],
    cardList: [{
      name: 'main'
    }, {
      name: 'items',
      title: '资金转账'
    }, {
      name: 'file'
    }],
    contentObj: {
      main: [{
          code: 'staffId',
          name: 'staffName',
          title: '经手人', //'经手人',mixins.$t("procureStaff")
          type: 'choose',
          urlKey: 'chooseStaff',
          required: true,
        },
        {
          code: 'orgId',
          name: 'orgName',
          title: '经手部门',
          type: 'choose',
          urlKey: 'chooseOrg',
          required: true,
        },

      ],
      items: [{
          code: 'trfOutMac',
          name: 'trfOutMacName',
          title: '转出账户',
          type: 'choose',
          urlKey: 'chooseMoneyAccount'
        },
        {
          code: 'trfInMac',
          name: 'trfInMacName',
          title: '转入账户',
          type: 'choose',
          urlKey: 'chooseMoneyAccount'
        }, {
          code: 'amtTrf',
          type: 'number',
          title: '转出金额',
          required: true,
          control: 'trfOutMac'
        }
      ],
      file: [{
        code: 'remarks',
        type: 'textarea',
        title: '备注'
      }, {
        code: 'file',
        type: 'uploader',
        title: '附件'
      }]
    },
    // 路由
    routeObjName: 'transfer',
  },



  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setValuesByEdit(data) {
    console.log("setValuesByEdit", data);
    let formData = JSON.parse(this.data.formData)
    formData = data.data
    formData.accountDate = data.data.accDate
    formData.itemList = data.dataItem
    this.setData({
      formData: JSON.stringify(formData),
    })

  },

  /**
   * @desc   : 表单选择回调
   * @author : 王英杰
   * @date   : 2024/1/26 11:46
   */
  chooseData(e) {
    console.log("t", e, this.data.formData);
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data
    if (code == "staffId") { //员工
      formData.staffId = data.id
      formData.staffName = data.name
    }
    if (code == "orgId") { //部门
      formData.orgId = data.id
      formData.orgName = data.name
    }

    this.setData({
      formData: JSON.stringify(formData)
    })
  },
  /**
   * @desc : 保存数据服务--校验
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  validData() {
    let formData = JSON.parse(this.data.formData)
    for (let i = 0; i < formData.itemList.length; i++) {
      for (let m = 0; m < formData.itemList.length; m++) {
        if (formData.itemList[i].trfInMac == formData.itemList[m].trfInMac) {
          //如果相等进入 说明 转出账户 和转入账户 选择了一个 是无效的
           util.showToast("转出账户和转入账户不能选择一个")
          return false
        }
      }
    }
    formData.itemList.forEach(element => {

    });


    return true
  },
  /**
   * @desc : 保存的參數設置
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setParams(params) {
    let formData = JSON.parse(this.data.formData)
    //收付款类型-收款的查询参数 
    params.orgId = formData.orgId //组织部门
    params.staffId = formData.staffId // 员工
    params.accDate = formData.accDate
    params.annexPaths = formData.annexPaths || [] //附件 
    params.remarks = formData.remarks // 备注
    params.makeStaff = app.globalData.user.staffId // 制单人  
    params.itemList = formData.itemList //商品明细  
    params.accDate = formData.accountDate
    return params
  },
  /**
   * @desc : 页面加载数据方法
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  loadInit() {
    if (this.data.formMode == Constants.formMode.edit) {
      wx.setNavigationBarTitle({
        title: mixins.$t('otherExpenditureOrdersEdit'),
      })
    } else {
      wx.setNavigationBarTitle({
        title: mixins.$t('otherExpenditureOrdersAdd'),
      })
    }
  },
  /**
   * @desc : 保存数据服务
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  saveData() {
    if (this.data.formMode == Constants.formMode.edit) {
      return this.excute(this.data.service, this.data.service.update, this.data.params)
    } else {
      return this.excute(this.data.service, this.data.service.insert, this.data.params)
    }
  },
  /**
   * @desc : 重写detail 
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  detail(id) {
    return this.excute(this.data.service, this.data.service.getMacTransferForUpdate + '/' + id, null);
  },
})