import React, { Component } from 'react'
import { Button, Card, Form, Input, Popconfirm, message as Message } from 'antd'
import { isEqual } from 'lodash'
import { AntIcon, Auth, Container, ModalForm, QueryTable, QueryTableActions } from 'components'
import { api } from 'common/api'
import auth from 'components/authorized/handler'
import FormBody from './form'

// 配置页面所需接口函数
const apiAction = {
    page: api.sysPosPage,
    add: api.sysPosAdd,
    edit: api.sysPosEdit,
    delete: api.sysPosDelete,
}

// 用于弹窗标题
const name = '职位'

export default class index extends Component {
    // 表格实例
    table = React.createRef()

    // 新增窗口实例
    addForm = React.createRef()
    // 编辑窗口实例
    editForm = React.createRef()

    columns = [
        {
            title: '职位名称',
            dataIndex: 'name',
            width: 400,
            sorter: true,
        },
        {
            title: '唯一编码',
            dataIndex: 'code',
            width: 400,
            sorter: true,
        },
        {
            title: '排序',
            dataIndex: 'sort',
            width: 80,
            sorter: true,
            defaultSortOrder: 'ascend',
        },
        {
            title: '备注',
            dataIndex: 'remark',
            width: 400,
            sorter: true,
        },
    ]

    /**
     * 构造函数,在渲染前动态添加操作字段等
     * @param {*} props
     */
    constructor(props) {
        super(props)

        const flag = auth({ sysPos: [['edit'], ['delete']] })

        if (flag) {
            this.columns.push({
                title: '操作',
                width: 150,
                dataIndex: 'actions',
                render: (text, record) => (
                    <QueryTableActions>
                        <Auth auth="sysApp:edit">
                            <a onClick={() => this.onOpen(this.editForm, record)}>编辑</a>
                        </Auth>
                        <Auth auth="sysApp:delete">
                            <Popconfirm
                                placement="topRight"
                                title="是否确认删除"
                                onConfirm={() => this.onDelete(record)}
                            >
                                <a>删除</a>
                            </Popconfirm>
                        </Auth>
                    </QueryTableActions>
                ),
            })
        }
    }

    /**
     * 阻止外部组件引发的渲染,提升性能
     * 可自行添加渲染条件
     * [必要]
     * @param {*} props
     * @param {*} state
     * @returns
     */
    shouldComponentUpdate(props, state) {
        return !isEqual(this.state, state)
    }

    /**
     * 调用加载数据接口,可在调用前对query进行处理
     * [异步,必要]
     * @param {*} params
     * @param {*} query
     * @returns
     */
    loadData = async (params, query) => {
        const { data } = await apiAction.page({
            ...params,
            ...query,
        })
        return data
    }

    /**
     * 打开新增/编辑弹窗
     * @param {*} modal
     * @param {*} record
     */
    onOpen(modal, record) {
        modal.current.open({
            record,
        })
    }

    /**
     * 对表格上的操作进行统一处理
     * [异步]
     * @param {*} action
     * @param {*} successMessage
     */
    async onAction(action, successMessage) {
        this.table.current.onLoading()
        try {
            await action
            Message.success(successMessage)
            this.table.current.onReloadData()
        } catch {
            this.table.current.onLoaded()
        }
    }

    /**
     * 删除
     * @param {*} record
     */
    onDelete(record) {
        this.onAction(apiAction.delete(record), '删除成功')
    }

    render() {
        return (
            <Container mode="fluid">
                <br />
                <Card bordered={false}>
                    <QueryTable
                        ref={this.table}
                        loadData={this.loadData}
                        columns={this.columns}
                        query={
                            <Auth auth="sysPos:page">
                                <Form.Item label="职位名称" name="name">
                                    <Input autoComplete="off" placeholder="请输入职位名称" />
                                </Form.Item>
                                <Form.Item label="唯一编码" name="code">
                                    <Input autoComplete="off" placeholder="请输入唯一编码" />
                                </Form.Item>
                            </Auth>
                        }
                        operator={
                            <Button
                                icon={<AntIcon type="plus" />}
                                onClick={() => this.onOpen(this.addForm)}
                            >
                                新增{name}
                            </Button>
                        }
                    ></QueryTable>
                </Card>
                <ModalForm
                    title={`新增${name}`}
                    action={apiAction.add}
                    ref={this.addForm}
                    onSuccess={() => this.table.current.onReloadData()}
                >
                    <FormBody />
                </ModalForm>

                <ModalForm
                    title={`编辑${name}`}
                    action={apiAction.edit}
                    ref={this.editForm}
                    onSuccess={() => this.table.current.onReloadData()}
                >
                    <FormBody />
                </ModalForm>
            </Container>
        )
    }
}
