import React from 'react';
import {
Drawer,
Space,
Button,
Dropdown,
Menu,
Table,
Spin,
Checkbox,
Modal,
Input,
} from 'antd';
import { OrgChartComponent } from '@/components/orgChart';
import * as d3 from 'd3';
import qs from 'qs';
import { message } from 'antd';
import jsPDF from 'jspdf';
import ExportJsonExcel from 'js-export-excel';
import './index.less';
import { i18n } from '../../util/i18n.js';
let addNodeChildFunc = null;
let orgChart = null;
let active = 'top';
export default class DrawerComponents extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
data: [],
detailType: 'chart',
params: {},
dataSource: [],
columns: [],
spinning: true,
showJob: true,
isModalOpen: false,
jobResponsibility: '',
personId: '',
jobTitle: '',
};
}
componentDidMount() {}
// 点击节点
onNodeClick = (node) => {
if (node.ftype == '4') {
this.setState({
jobResponsibility: node.jobResponsibility,
jobTitle: node.jobTitle,
isModalOpen: true,
personId: node.id,
});
}
};
onButtonClick(event, d) {
if (d.children) {
let idsList = [];
d.children.forEach((item) => {
if (item.data.hasChildren && !item._children) {
idsList.push(item.data.id);
}
});
if (idsList.length == 0) {
return;
}
}
}
// 获取部门图片
getDepartmentImage(fisvitual) {
return fisvitual == '0'
? `./img/user-card/user-card.png`
: `./img/user-card/user-card-blue.png`;
}
//获取数据
getDeatilDatas(params, type = 'chart', showJob = '1') {
const { language } = this.props;
this.setState({ spinning: true, data: [] });
d3.json(
'/api/bs/hrmorganization/orgchart/getDepartmentDetail?' +
qs.stringify({ detauleType: type, ...params, showJob }),
).then((data) => {
//
if (type == 'chart') {
this.setState({ data: data.data, spinning: false });
} else {
let columns = [
{
dataIndex: 'id',
key: 'id',
title: `${i18n.table.no[language]}`,
},
{
dataIndex: 'workCode',
key: 'workCode',
title: `${i18n.table.workCode[language]}`,
},
{
dataIndex: 'lastName',
key: 'lastName',
title: `${i18n.table.lastName[language]}`,
},
{
dataIndex: 'sex',
key: 'sex',
title: `${i18n.table.gender[language]}`,
},
{
dataIndex: 'departmentName',
key: 'departmentName',
title: `${i18n.table.department[language]}`,
},
{
dataIndex: 'subcompanyName',
key: 'subcompanyName',
title: `${i18n.table.company[language]}`,
},
{
dataIndex: 'jobTitle',
key: 'jobTitle',
title: `${i18n.table.possition[language]}`,
},
{
dataIndex: 'mobile',
key: 'mobile',
title: `${i18n.table.mobile[language]}`,
},
];
this.setState({ dataSource: data.dataSource, columns: columns });
}
});
}
// ButtonContent渲染
buttonContentRender = ({ node, state }) => {
return `
`;
};
// 节点宽度渲染
nodeWidthRender = (d) => {
return 280;
};
nodeHeightRender = (d) => {
return 160;
};
// tool bar start
handleTopLayoutClick = (progressBtn) => {
progressBtn.current.style.top = 50 + 'px';
orgChart &&
orgChart
.setCentered(orgChart.getChartState().root.id)
.layout('top')
.render();
active = 'top';
};
handleLeftLayoutClick = (progressBtn) => {
progressBtn.current.style.top = 50 + 'px';
orgChart &&
orgChart
.layout('left')
.setCentered(orgChart.getChartState().root.id)
.render();
active = 'left';
};
handleZoomIn = (progressBtn) => {
if (progressBtn) {
let top = parseInt(progressBtn.current.style.top) - 10;
if (top >= 0) {
progressBtn.current.style.top = top + 'px';
} else {
return;
}
}
orgChart && orgChart.zoomIn();
};
handleZoomOut = (progressBtn) => {
if (progressBtn) {
let top = parseInt(progressBtn.current.style.top) + 10;
if (top <= 100) {
progressBtn.current.style.top = top + 'px';
} else {
return;
}
}
orgChart && orgChart.zoomOut();
};
downloadPdf = (chart) => {
chart.exportImg({
save: false,
full: true,
onLoad: (base64) => {
var pdf = new jsPDF();
var img = new Image();
img.src = base64;
img.onload = function () {
pdf.addImage(
img,
'JPEG',
5,
5,
595 / 3,
((img.height / img.width) * 595) / 3,
);
pdf.save('chart.pdf');
};
},
});
};
handleExport = (e) => {
const { language } = this.props;
let type = e.key == '1' ? 'png' : e.key == '1' ? 'pdf' : 'excel';
if (type == 'png') {
orgChart && orgChart.exportImg({ full: true });
} else if (type == 'pdf') {
orgChart && this.downloadPdf(orgChart);
} else {
let { dataSource } = this.state;
var option = {};
let dataTable = [];
if (dataSource) {
for (let i in dataSource) {
if (dataSource) {
let obj = {
序号: dataSource[i].id,
工号: dataSource[i].workCode,
姓名: dataSource[i].lastName,
性别: dataSource[i].sex,
部门: dataSource[i].departmentName,
分部: dataSource[i].subcompanyName,
岗位: dataSource[i].jobTitle,
手机号: dataSource[i].mobile,
};
dataTable.push(obj);
}
}
}
option.fileName = `${i18n.table.orgInfo[language]}`;
option.datas = [
{
sheetData: dataTable,
sheetName: 'sheet',
sheetFilter: [
'序号',
'工号',
'姓名',
'性别',
'部门',
'分部',
'岗位',
'手机号',
],
sheetHeader: [
i18n.table.no[language],
i18n.table.workCode[language],
i18n.table.lastName[language],
i18n.table.gender[language],
i18n.table.department[language],
i18n.table.company[language],
i18n.table.possition[language],
i18n.table.mobile[language],
],
},
];
var toExcel = new ExportJsonExcel(option);
toExcel.saveExcel();
}
};
/**
* 节点渲染
*/
nodeContentRender = (d, i, arr, state) => {
const { language } = this.props;
if (d.data.ftype == 2) {
return `
${d.data.fname}
${i18n.head[language]}:${d.data.fleader}
${i18n.onJob[language]}: ${d.data.fonjob} ${
i18n.onJob[language] == 'cn' ? '人' : ''
}
`;
} else if (d.data.ftype == 3) {
return `
${
d.data.fname
}
${i18n.onJob[language]}: ${d.data.fonjob} ${
i18n.onJob[language] == 'cn' ? '人' : ''
}
`;
} else if (d.data.ftype == 4) {
return `
${i18n.serveAge[language]}: ${
d.data.companyWorkYear
} ${i18n.year[language]}
${i18n.postion[language]}: ${d.data.jobTitle}
${i18n.level[language]}: ${d.data.level}
`;
}
};
showDrawer = (params) => {
const showJob = params.fclass == '0' ? '1' : '0';
this.getDeatilDatas(params, 'chart', showJob);
this.setState({ open: true, params: params });
};
onClose = () => {
const { params } = this.state;
this.setState({
open: false,
detailType: 'chart',
showJob: params.fclass == '0' ? 'true' : 'false',
});
};
changeDetail = () => {
const { detailType, params } = this.state;
let type = detailType == 'chart' ? 'table' : 'chart';
var showJob = '1';
if (params.fclass == '0') {
showJob = this.state.showJob ? '1' : '0';
} else {
showJob = '0';
}
this.setState({
detailType: type,
});
this.getDeatilDatas(params, type, showJob);
};
render() {
const { language } = this.props;
const {
params,
open,
data,
detailType,
dataSource,
columns,
spinning,
showJob,
jobResponsibility,
jobTitle,
isModalOpen,
personId,
} = this.state;
let arr = [];
if (detailType == 'chart') {
arr.push({ label: `${i18n.exportImage[language]}`, key: '1' });
//arr.push({ label: '导出PDF', key: '2' });
} else {
arr.push({ label: `${i18n.exportTable[language]}`, key: '3' });
}
const menu = ;
return (
{detailType == 'chart' && params && params.fclass == '0' && (
{
this.setState({
showJob: e.target.checked,
});
this.getDeatilDatas(
params,
detailType,
e.target.checked ? '1' : '0',
);
}}
>
{i18n.showJob[language]}
)}
}
>
{detailType == 'chart' ? (
{data.length > 0 && (
(orgChart = chart)}
setClick={(click) => (addNodeChildFunc = click)}
onNodeClick={this.onNodeClick}
onButtonClick={this.onButtonClick}
data={data}
buttonContent={this.buttonContentRender}
nodeWidth={this.nodeWidthRender}
nodeHeight={this.nodeHeightRender}
nodeContent={this.nodeContentRender}
/>
)}
) : (
language == 'cn'
? `共 ${dataSource.length} 条`
: `${dataSource.length} ${i18n.table.records[language]} ${i18n.table.total[language]}`,
}}
/>
)}
this.setState({ isModalOpen: false })}
footer={[
,
,
]}
>
{jobResponsibility}
);
}
}