import React from 'react';
import { Tag, Modal, Input, Spin, Switch, Select, message, Popconfirm, Button, Cascader, Icon } from 'antd';
import { provinceSelect, provinceList, getProvince } from '../../NewDicProvinceList.js';
import { companySearch } from '../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
const ProvinceAdd = React.createClass({
getInitialState() {
return {
cityOption: [],
theProvince: [],
citys: []
};
},
componentWillReceiveProps(nextProps) {
let theArr = [];
this.state.theProvince = [nextProps.locations[nextProps.index].province];
this.state.citys = nextProps.locations[nextProps.index].city || [];
provinceList.map((item) => {
if (item.id == nextProps.locations[nextProps.index].province) {
item.cityList.map((city) => {
theArr.push({
value: city.id,
label: city.name
})
});
};
});
this.state.cityOption = theArr;
},
render() {
return (
{
let bool = true, theArr = [], _me = this;;
this.props.locations.map((item, i) => {
if (item.province == e[0] && this.props.index != i) {
bool = false;
};
});
if (!bool) {
message.warning('请选择一个其他省份');
this.setState({
theProvince: undefined,
citys: undefined,
cityOption: []
});
return;
};
provinceList.map((item) => {
if (item.id == e) {
item.cityList.map((city) => {
theArr.push({
value: city.id,
label: city.name
})
});
};
});
this.props.getLocations(this.props.index, e[0])
this.setState({
theProvince: e,
citys: undefined,
cityOption: theArr,
})
}} />
);
},
})
const TheModal = React.createClass({
postData() {
this.setState({
loading: true
});
let theArr = [];
let theLocations = this.state.locations.concat();
theLocations.map((item, i) => {
if (item.province) {
theArr.push(item);
};
});
theArr.map((item) => {
if (item.city && item.city.length) {
item.city = item.city.join(',');
} else {
item.city = null;
};
});
$.ajax({
type: "POST",
url: this.state.id ? globalConfig.context + "/api/admin/supervise/updateAdmin" : globalConfig.context + "/api/admin/supervise/insertAdmin",
data: {
data: JSON.stringify({
'id': this.state.id,
'name': this.state.name,
'email': this.state.email,
'mobile': this.state.mobile,
'position': this.state.position,
'locations': theArr,
'superiorId': this.state.superiorId
}),
'roles': this.state.bindroles
}
}).done((res) => {
if (res.error.length) {
message.error(res.error[0].message);
} else {
message.success("保存成功");
this.setState({
visible: false,
});
this.props.handleReturn(false, true);
//第二个参数表示保存
}
}).always(() => {
this.setState({
loading: false
})
})
this.props.postData;
},
getInitialState() {
return {
name: '',
email: '',
mobile: '',
roles: [],
visible: false,
loading: false,
bindroles: [],
locations: [],
cityOption: [],
provinceList: [],
locationsObj: {}
}
},
handleCancel() {
this.setState({
visible: false,
});
this.props.handleReturn(false, false);
},
loadInitialData() {
this.setState({
loading: true
});
$.ajax({
url: globalConfig.context + '/api/roles',
cache: false
}).done((rolesres) => {
this.setState({
roles: rolesres.data,
loading: false
});
})
},
loadAdminSelectList() {
this.setState({
loading: true
});
$.ajax({
url: globalConfig.context + '/api/admin/supervise/adminSelectList',
cache: false
}).done((data) => {
if (!data.data) {
if (data.error && data.error.length) {
message.warning(data.error[0].message);
return;
};
};
let theArr = [], i;
for (i in data.data) {
theArr.push(
{data.data[i]}
)
};
this.setState({
adminSelectOption: theArr,
loading: false
});
})
},
loadBindRoles(uid) {
this.setState({
loading: true
});
$.ajax({
url: globalConfig.context + '/api/admin/role',
cache: false,
data: {
"uid": uid
}
}).done((data) => {
this.setState({
bindroles: data.data || [],
loading: false
});
})
},
loadLocations(id) {
this.setState({
loading: true
});
$.ajax({
url: globalConfig.context + '/api/admin/supervise/adminDetailLocation',
cache: false,
data: {
"id": id
}
}).done((data) => {
if (!data.data) {
if (data.error && data.error.length) {
message.warning(data.error[0].message);
return;
};
};
data.data.map((item) => {
if (item.city) {
item.city = item.city.split(',');
}
});
this.setState({
locations: data.data,
loading: false
});
})
},
componentWillMount() {
this.loadAdminSelectList();
},
componentWillReceiveProps(nextProps) {
if (nextProps.show && !this.state.roles.length) {
this.loadInitialData();
};
if (!this.state.visible && nextProps.show) {
this.loadBindRoles(nextProps.data.id);
this.loadLocations(nextProps.data.id);
};
let nextState = {
visible: nextProps.show,
id: nextProps.data ? nextProps.data.id : '',
name: nextProps.data ? nextProps.data.name : '',
email: nextProps.data ? nextProps.data.email : '',
mobile: nextProps.data ? nextProps.data.mobile : '',
position: nextProps.data ? nextProps.data.position : '',
superiorId: nextProps.data ? nextProps.data.superiorId : '',
};
this.setState(nextState)
},
resetPwd(e) {
this.setState({
loading: true
})
$.ajax({
type: 'post',
url: globalConfig.context + "/api/admin/supervise/resetPwd",
dataType: "json",
data: {
id: this.state.id
}
}).done((res) => {
if (res.error && res.error.length) {
message.error(res.error[0].message);
} else {
message.success("密码重置成功");
}
}).always(() => {
this.setState({
loading: false
})
});
},
getRolesSelection() {
if (!this.state.id) {
return
} else if (this.state.id != '1') {
return
角色
} else if (this.state.id == "1") {
return 角色系统管理员
}
},
getProvinceSelection() {
if (window.showPermissionList && window.showRoleList) {
return
省份
{ this.setState({ province: e }) }} />
} else {
return
省份
{ this.setState({ province: e }) }} />
}
},
bindRoles(val) {
this.setState({
bindroles: val
});
},
getRolesOptions() {
let options = [];
for (let i = 0; i < this.state.roles.length; i++) {
options.push({this.state.roles[i].roleName});
}
return options;
},
getLocations(index, p, c) {
this.state.locations.map((item, i) => {
if (index == i) {
item.province = p;
item.city = c || [];
};
});
},
delLocations(index) {
this.state.locations.splice(index, 1);
this.setState({ locations: this.state.locations });
},
provinceAdd() {
this.state.locations.push({
province: null,
city: null
});
this.setState({ locations: this.state.locations });
},
render() {
return (
地区
{!window.showPermissionList && window.showRoleList ?
{this.state.locations.map((item, i) => {
return
})}
:
{this.state.locations.map((item, i) => {
return {getProvince(item.province) + ' ' + (item.city ? item.city.map((item) => {
return getProvince(item) + ' '
}) : '')}
})}
}
);
}
})
export default TheModal;