import BaseCurd from './BaseCurd' const operatorTypes = [{ id: 1, name: '新增', method: 'POST' }, { id: 2, name: '修改', method: 'PUT' }, { id: 3, name: '查看' }] export default (Target) => ({ name: 'BaseCurdEditor', extends: BaseCurd(Target), data: () => ({ visible: false, data: null, oData: null, operatorTypeId: 1, refFormName: 'form', activeCollapseName: '1' }), computed: { operatorType () { return operatorTypes[this.operatorTypeId - 1] } }, methods: { onOpen (data) { return Promise.resolve() }, onGetDetail () { return Promise.resolve() }, afterGetDetail (model) {}, beforeSave () { return Promise.resolve() }, onSave () {}, open (data, isView) { this.onOpen(data).then(() => { if (data) { this.operatorTypeId = isView ? operatorTypes[2].id : operatorTypes[1].id this.oData = data this.getDetail(data) } else { this.operatorTypeId = operatorTypes[0].id this.model = this.initModel() this.clearFormValid() } this.visible = true }) }, initModel () { this.data = new Target(this) }, getDetail (data) { this.onGetDetail().then(() => { this.$$request(this.$$api.getDetail, data, Target).then(data => { data = data || {} this.data = data this.clearFormValid() this.afterGetDetail(data) }).catch(console.error).finally(() => {}) }) }, clearFormValid () { this.$nextTick(() => { let formEl = this.getForm() formEl && formEl.resetFields() }) }, save () { const formEl = this.getForm() formEl && formEl.validate(flag => { if (!flag) { return } this.beforeSave().then(() => { this.$$request(Target.$$api.save, this.data, this.operatorType.method, Target).then((data) => { if (data) { this.$notify({ title: '成功', message: '保存成功', type: 'success', position: 'bottom-right' }) this.onSave() this.$emit('saved', this.data) this.close() } }).catch(console.error).finally(() => {}) }) }) }, onSaveBtnClick () { this.save() }, onCloseBtnClick () { this.close() }, close () { this.visible = false this.$emit('close') }, getForm () { return this.$refs[this.refFormName] } } })