2020-01-06 15:02:50 +08:00

111 lines
2.3 KiB
JavaScript

import * as API from 'api/virtual/agents';
export default {
props: {
show: {
type: Boolean,
default: false
},
data: {
type: Object,
default() {
return null;
}
}
},
data() {
return {
my_show: false,
isUpdate: false,
loading: false,
params: {
company_id: '',
name: '',
contacts: '',
mobile: '',
address: '',
remark: '',
status: 0
}
};
},
watch: {
show(bool) {
this.my_show = bool;
if (bool) {
if (this.data) {
for (let k in this.data) {
if (k in this.params) {
this.params[k] = this.data[k];
}
}
}
}
}
},
methods: {
ok() {
if (!this.params.company_id) {
this.$Message.info('请选择代理商');
return;
}
if (!this.params.name) {
this.$Message.info('请填写代理商名称');
return;
}
if (this.params.contacts && !(/[\s\S]{2,32}/.test(this.params.contacts))) {
this.$Message.info('联系人长度在2-32之间');
return;
}
if (this.data) {
// 编辑
API.update(this.params, this.data.id).then(res => {
this.loading = false;
if (res.code == 0) {
this.$emit('update-success');
this.$Message.success('更新成功');
this.clear();
this.completeCompanyInitialized = false;
}
}).catch(err => {
this.loading = false;
});
} else {
// 添加
API.create(this.params).then(res => {
this.loading = false;
if (res.code == 0) {
this.$emit('add-success');
this.$Message.success('添加成功');
this.clear();
this.completeCompanyInitialized = false;
}
}).catch(err => {
this.loading = false;
});
}
},
visibleChange(bool) {
if (!bool) {
this.$emit('update:show', false);
}
},
clear() {
for (let k in this.params) {
if (k == 'status') {
this.params[k] = 0;
} else {
this.params[k] = '';
}
}
this.my_show = false;
}
}
};