import { taskStatus } from '@/utils/status'

export const columns = (_this) => [
  {
    prop: 'taskName',
    label: '任务名称',
    align: 'left',
    width: '200px'
  },
  {
    prop: 'taskCode',
    label: '任务代号'
  },
  {
    prop: 'taskTime',
    label: '任务时间',
    width: '200px'
  },
  {
    prop: 'taskDuration',
    label: '任务时长/小时'
  },
  {
    prop: 'flyingSession',
    label: '飞行场次'
  },
  {
    prop: 'env',
    label: '飞行环境'
  },
  {
    prop: 'transferLabel',
    label: '转场形式'
  },

  {
    prop: 'teamLeader',
    label: '带队领导'
  },

  {
    prop: 'taskAircraftNum',
    label: '任务要求飞机数量'
  },
  {
    prop: 'carryingAvmatNumber',
    label: '携带航材数量'
  },
  {
    prop: 'taskType',
    label: '任务类型'
  },

  {
    prop: 'taskSite',
    label: '任务地点'
  },
  // {
  //   prop: 'riskFactor',
  //   label: '风险系数'
  // },

  // {
  //   prop: 'taskStrengthLabel',
  //   label: '任务强度'
  // },

  {
    prop: 'status',
    label: '状态',
    width: '150px',
    render: (h, params) => taskStatus(h, params.row.status)
  },
  {
    prop: 'updateTime',
    label: '更新时间',
    width: '120px'
  },
  {
    button: true,
    label: '操作',
    width: '250px',
    align: 'left',
    group: [
      {
        name: '编辑',
        type: 'text',
        round: false,
        plain: false,
        onClick: (row, index, scope) => {
          _this.edit(row)
        }
      },
      {
        name: '任务飞机',
        type: 'text',
        round: false,
        plain: false,
        statusKey: 'status',
        showType: '1', //0:父级；1：子级
        onClick: (row, index, scope) => {
          _this.toAircraft(row)
        }
      },
      {
        name: '删除',
        type: 'text',
        round: false,
        plain: false,
        statusKey: 'status',
        unDisableKey: '1',
        onClick: (row, index, scope) => {
          _this.remove(row)
        }
      },
      {
        name: '提交',
        type: 'text',
        round: false,
        plain: false,
        statusKey: 'status',
        unDisableKey: '1',
        showType: '0', //0:父级；1：子级
        onClick: (row, index, scope) => {
          _this.audit(row)
        }
      }
    ]
  }
]

export const options = {
  treeProps: {
    children: 'children',
    hasChildren: 'hasChildren'
  },
  rowKey: 'taskId',
  stripe: true, // 斑马纹
  mutiSelect: true, // 多选框
  index: false, // 显示序号， 多选则 mutiSelect
  loading: false, // 表格动画
  initTable: true, // 是否一挂载就加载数据
  border: true,
  rowKey: 'taskId',
  height: 'calc(100vh - 240px)'
}

// export const findParents = (tree, targetId, path = []) => {
//   for (const node of tree) {
//     path.push(node.aircaftTypeCode)
//     if (node.aircaftTypeCode === targetId) {
//       return path
//     }
//     if (node.children && Array.isArray(node.children) && node.children.length > 0) {
//       const result = findParents(node.children, targetId, path)
//       if (result) {
//         return result
//       }
//     }
//     path.pop()
//   }
//   return null
// }

export const modalConfig = (_this) => ({
  formItems: [
    {
      key: 'parentId',
      type: 'select',
      label: '主任务',
      placeHolder: '请选择主任务',
      valueOptions: [],
      colLayout: { span: 24 }
    },
    { key: 'taskName', type: 'input', label: '任务名称', placeHolder: '请输入任务名称' },
    { key: 'taskCode', type: 'input', label: '任务代号', placeHolder: '请输入任务代号' },
    { key: 'teamLeader', type: 'input', label: '带队领导', placeHolder: '请输入带队领导' },

    {
      key: 'taskTime',
      type: 'daterange',
      label: '任务时间',
      options: {
        format: 'yyyy-M-d',
        placeHolder: '请选择任务时间',
        'range-separator': '至',
        'start-placeholder': '开始日期',
        'end-placeholder': '结束日期',
        'value-format': 'yyyy-MM-dd'
      }
    },
    {
      key: 'taskDuration',
      type: 'inputNumber',
      label: '任务时长',
      placeHolder: '请输入任务时长',
      options: {
        style: 'width: 100%'
      }
    },

    {
      key: 'envFactorId',
      type: 'select',
      label: '飞行环境',
      placeHolder: '请选择飞行环境',
      valueOptions: []
    },
    {
      key: 'flyingSession',
      type: 'inputNumber',
      label: '飞行场次',
      placeHolder: '请输入飞行场次',
      options: {
        style: 'width: 100%'
      }
    },
    { key: 'taskType', type: 'input', label: '任务类型', placeHolder: '请输入任务类型' },
    {
      key: 'taskAircraftNum',
      type: 'inputNumber',
      label: '任务飞机数量',
      placeHolder: '请输入任务飞机数量',
      options: {
        style: 'width: 100%'
      }
    },
    // {
    //   key: 'carryingAvmatNumber',
    //   type: 'inputNumber',
    //   label: '携带航材数量',
    //   placeHolder: '请输入携带航材数量',
    //   options: {
    //     style: 'width: 100%'
    //   }
    // },
    { key: 'taskSite', type: 'input', label: '任务地点', placeHolder: '请输入任务地点' },
    {
      key: 'transferModality',
      type: 'select',
      label: '转场形式',
      placeHolder: '请选择转场形式',
      valueOptions: []
    },

    { key: 'riskFactor', type: 'input', label: '风险系数', placeHolder: '请输入危险系数' },
    {
      key: 'taskStrength',
      type: 'select',
      label: '任务强度',
      placeHolder: '请选择任务强度',
      valueOptions: []
    },

    {
      key: 'taskDescribe',
      type: 'input',
      label: '任务描述',
      // 文本域，不写则是普通文本框
      inputOptions: {
        type: 'textarea',
        rows: '2',
        style: 'width: 100%'
      },
      colLayout: { span: 24 }
    }
  ],
  labelWidth: '120px',
  width: '800px',
  itemStyle: { padding: '10px' },
  rules: {
    taskName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
    taskCode: [{ required: true, message: '请输入任务代号', trigger: 'blur' }],
    teamLeader: [{ required: true, message: '请输入带队领导', trigger: 'blur' }],
    taskTime: [{ required: true, message: '请选择任务时间', trigger: 'blur' }],
    taskDuration: [
      { required: true, message: '请输入任务时长', trigger: 'blur' },
      { validator: taskDurationvalidate, trigger: 'blur' }
    ],
    envFactorId: [{ required: true, message: '请选择飞行环境', trigger: 'blur' }],
    flyingSession: [
      { required: true, message: '请输入飞行场次', trigger: 'blur' },
      { validator: flyingSessionvalidate, trigger: 'blur' }
    ],
    taskType: [{ required: true, message: '请输入任务类型', trigger: 'blur' }],
    taskAircraftNum: [
      { required: true, message: '请输入任务飞机数量', trigger: 'blur' },
      { validator: taskAircraftNumvalidate, trigger: 'blur' }
    ],
    carryingAvmatNumber: [
      { required: true, message: '请输入携带航材数量', trigger: 'blur' },
      { validator: taskAircraftNumvalidate, trigger: 'blur' }
    ],
    taskSite: [{ required: true, message: '请输入任务地点', trigger: 'blur' }],
    transferModality: [{ required: true, message: '请选择转场形式', trigger: 'blur' }],
    // taskDescribe: [{ required: true, message: '请输入任务描述', trigger: 'blur' }]
    riskFactor: [
      { required: true, message: '请输入危险系数', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入数字'))
          } else {
            const reg = /^(\d+|\d+\.\d+)$/
            if (reg.test(value)) {
              callback()
            } else {
              callback(new Error('请输入整数或小数'))
            }
          }
        }
      }
    ],
    taskStrength: [{ required: true, message: '请选择任务强度', trigger: 'blur' }]
  }
})

const taskDurationvalidate = (rule, value, callback) => {
  if (!Number.isInteger(Number(value)) || Number(value) <= 0) {
    callback(new Error('任务时长必须大于0'))
  } else {
    callback()
  }
}

const flyingSessionvalidate = (rule, value, callback) => {
  if (!Number.isInteger(Number(value)) || Number(value) <= 0) {
    callback(new Error('飞行场次必须大于0且为正整数'))
  } else {
    callback()
  }
}
const taskAircraftNumvalidate = (rule, value, callback) => {
  if (!Number.isInteger(Number(value)) || Number(value) <= 0) {
    callback(new Error('飞机数量必须大于0且为正整数'))
  } else {
    callback()
  }
}
