import { deepClone } from '@/utils/index'

export const rules = {
  avmatCategoriesName: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
  number: [
    { required: true, message: '请输入序号', trigger: 'blur' },
    { validator: (rule, value, callback) => validate(rule, value, callback), trigger: 'blur' }
  ],
  avmatCategoriesCode: [{ required: true, message: '请输入分类编号', trigger: 'blur' }]
}

const validate = (rule, value, callback) => {
  if (!Number.isInteger(Number(value)) || Number(value) <= 0) {
    callback(new Error('必须为正整数'))
  } else {
    callback()
  }
}

export const columns = (_this) => [
  {
    prop: 'avmatCategoriesName',
    label: '分类名称',
    align: 'left'
  },
  {
    prop: 'avmatCategoriesCode',
    label: '分类编号'
  },
  {
    prop: 'number',
    label: '显示序号'
  },
  {
    prop: 'status',
    label: '状态',
    render: (h, params) => {
      if (params.row.status == 1) {
        return h('span', { class: 'success-state' }, '启用')
      } else {
        return h('span', { class: 'warning-state' }, '禁用')
      }
    }
  },

  {
    button: true,
    label: '操作',
    width: '240px',
    group: [
      {
        name: '编辑',
        type: 'text',
        round: false,
        plain: false,
        onClick: (row, index, scope) => {
          _this.handUpdate(row)
        }
      },
      {
        name: '启用',
        type: 'text',
        statusKey: 'status',
        unDisableKey: 0,
        round: false,
        plain: false,
        onClick: (row, index, scope) => {
          _this.operation(row, 1)
        }
      },
      {
        name: '禁用',
        type: 'text',
        statusKey: 'status',
        unDisableKey: 1,
        round: false,
        plain: false,
        onClick: (row, index, scope) => {
          _this.operation(row, 0)
        }
      },
      {
        name: '删除',
        type: 'text',
        round: false,
        plain: false,
        onClick: (row, index, scope) => {
          _this.handRemove(row)
        }
      }
    ]
  }
]

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