import webapi from './webapi'
import * as validator from '@/utils/validator'

//////////////////查询列表//////////////////////////////////////////////

// 初始化查询表单
export function initSearchForm(reduce, gf, fields) {
  return async (dispatch, getState) => {
    if (!fields)
      fields = gf(['searchForm'])
    // 更新表单数据
    reduce('searchForm', { type: 'init', data: fields })
  }
}
// 更新查询表单
export function updateSearchForm(reduce, gf, fields) {
  return async (dispatch, getState) => {
    if (!fields)
      fields = gf(['searchForm'])
    // 更新表单数据
    reduce('searchForm', { type: 'update', data: fields })
  }
}

// 列表数据
export function tableData(reduce, gf, param) {
  return async (dispatch, getState) => {
    let data = []
    let pager = {}
    param = gf(['searchForm'])
    if (param) {
      let res = await webapi.tableData(param)
      data = res.data
      pager = res.pager
    }
    reduce('tableData', { type: 'update', data })
    reduce('pagination', { type: 'update', data: pager })
  }
}

// 获取业务类型
export function getGusType(reduce, gf, data) {
  return async (dispatch, getState) => {
    const res = await webapi.getBusTypeArr({ "dicCode": "DICT_BUSINESS_TYPE_BACK" })
    if (res === undefined) return
    reduce('busType', { type: 'init', data: res.dic_DICT_BUSINESS_TYPE_BACK })
  }
}

export function deleteItem(reduce, gf, fields) {
  return async (dispatch, getState) => {
    const formObj = gf(['formObj'])
    const result = await webapi.delDic(formObj)
    if (result.errorCode === '0') {
      dispatch(getTreeChildrenData(reduce, gf, { functioinId: formObj.parentId }))
      reduce('selectKey', { type: 'init', data: {} })
      reduce('formObj', { type: 'reset', data: {} })
      return result
    }
  }
}

//////////////////three 组件//////////////////////////////////////////////

export function getTreeData(reduce, gf, data) {
  return async (dispatch, getState) => {
    const treeData = gf(['treeData'])
    if (treeData.length > 0) return
    const root = await webapi.getTreeData({})
    if (root === undefined) return
    reduce('treeData', { type: 'update', data: root })
  }
}

export function getTreeChildrenData(reduce, gf, { id, ...orther }) {
  return async (dispatch, getState) => {
    const res = await webapi.getTreeData({
      pid: id,
      level: 0
    })
    if (res === undefined) return null
    reduce('treeData', { type: 'updateChildren', data: { children: res, id, ...orther } })
    return res
  }
}

export function getTreeNodeDetail(reduce, gf, functioinId) {
  return async (dispatch, getState) => {
    let res = await webapi.findById({ functioinId })
    if (res === undefined) return
    reduce('formObj', {
      type: 'update', data: {
        parentName: res.parentName,
        ...res.secFunctioinDTOReturn
      }
    })
    reduce('tempState', { type: 'setEdit', data: true })
  }
}

//////////////////用户表单验证//////////////////////////////////////////////
// 定义需要校验的字段和校验规则, 支持同一个字段多条规则和异步校验，详情可参考
// https://github.com/yiminghe/async-validator
const rules = {
  loginName: [{ type: 'string', required: true, message: '必填项'}],
  userName: [{ type: 'string', required: true, message: '必填项'}],
  mobilePhone: { type: 'string', required: true, message: '必填项' },
}

// 定义form表单字段，用于清除重置时回显和首次校验
const formData = {
  loginName: null,
  userName: null,
  mobilePhone: null
}

// 初始化表单
export function initForm(reduce, gf, param) {
  return async (dispatch, getState) => {
    let data = formData
    if (param && param.userId) {
      data = await webapi.getUser(param)
      reduce('formObj', { type: 'update', data })
      return
    }
    reduce('formObj', { type: 'init', data })
  }
}

// 更新表单
export function updateFormObj(reduce, gf, fields) {
  return async (dispatch, getState) => {
    if (!fields)
      fields = gf(['formObj'])
    // 字段校验。返回一个长度为2的数组，第一个是boolean值，代表是否校验成功，第二个是错误消息状态
    const [result, resultObj] = await validator.fieldValidator(fields, rules)
    // 更新文本域验证状态
    reduce('validateState', { type: 'update', data: resultObj })
    // 更新表单数据
    reduce('formObj', { type: 'update', data: fields })
    return result
  }
}

// 提交表单
export function dialogSave(reduce, gf, data) {
  return async (dispatch, getState) => {
    // 获取表单数据
    const formObj = gf(['formObj'])
    // const isEdit = gf(['dialogTempState', 'isEdit'])
    // 提交表单
    const res = await webapi.save(formObj)
    if (res === undefined) {
      return false
    } else {
      return true
    }
  }
}

const resetRules = {
  password: {type: 'string', required: true, message: '请输入密码', dependValidator: (fields, value, cb) => {
    if (!fields.password2) {
      cb()
    } else if (fields.password2 != value) {
      cb('两个密码不一致', 'password2') // confirm字段显示错误信息，若本字段显示错误信息，则第二个参数'confirm'可不传
    } else {
      cb('', 'password2') // 消除confirm可能的校验错误状态
    }
  }},
  password2: {type: 'string', required: true, message: '请确认密码', dependValidator: (fields, value, cb) => {
    console.log(234)
    if (fields.password !== value) {
      return cb('两个密码不一致')
    } else {
      return cb()
    }
  }},
}

// 定义form表单字段，用于清除重置时回显和首次校验
const resetFormData = {
  password: null,
  password2: null,
}

// 初始化表单
export function initResetForm(reduce, gf, param) {
  return async (dispatch, getState) => {
    let data = resetFormData
    if (param && param.userId) {
      data = await webapi.getUser(param)
      reduce('resetFormObj', { type: 'update', data })
      return
    }
    reduce('resetFormObj', { type: 'init', data })
  }
}

// 更新表单
export function updateResetFormObj(reduce, gf, fields) {
  return async (dispatch, getState) => {
    if (!fields)
      fields = gf(['resetFormObj'])
    // 字段校验。返回一个长度为2的数组，第一个是boolean值，代表是否校验成功，第二个是错误消息状态
    const [result, resultObj] = await validator.fieldValidator(fields, resetRules, gf(['attributeForm']))
    // 更新文本域验证状态
    reduce('resetValidateState', { type: 'update', data: resultObj })
    // 更新表单数据
    reduce('resetFormObj', { type: 'update', data: fields })
    return result
  }
}

// 提交表单
export function resetFormSave(reduce, gf, data) {
  return async (dispatch, getState) => {
    // 获取表单数据
    const formObj = gf(['resetFormObj'])
    // const isEdit = gf(['dialogTempState', 'isEdit'])
    // 提交表单
    const res = await webapi.savePwd(formObj)
    if (res === undefined) {
      return false
    } else {
      return true
    }
  }
}