<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="dialogVisible"
    width="25%"
    :before-close="handleClose"
    center
  >
    <data-form ref="dataform" :form="form" :form-model="formModel" :rules="rules" />
    <span slot="footer" class="dialog-footer">
      <el-button v-loading="loading" type="primary" @click="doSave">{{ $t('global.confirm') }}</el-button>
      <el-button @click="handleClose">{{ $t('global.cancel') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { postOperateSaveAs } from '@/api/management/operation';
import { getSkinCodeList } from '@/api/management/mapskin';

export default {
    name: 'AddBatch',
    data() {
        return {
            loading: false,
            dialogVisible: false,
            formModel: {
                skinCodeFrom: '',
                skinCodeTo: ''
            },
            skinCodeList: []
        };
    },
    computed: {
        form() {
            this.type === 'ADD';
            const form = {
                labelWidth: '120px',
                items: [
                    {
                        prop: 'skinCodeFrom',
                        label: this.$t('lesson.skinTypeFrom'),
                        type: 'select',
                        required: true,
                        options: this.skinCodeList
                    },
                    {
                        prop: 'skinCodeTo',
                        label: this.$t('lesson.skinTypeTo'),
                        type: 'select',
                        required: true,
                        options: this.skinCodeList
                    }
                ]
            };
            return form;
        },
        rules() {
            const crules = {
                skinCodeFrom: [
                    {
                        required: true,
                        message: this.$t('rules.inputSkinType'),
                        trigger: 'change'
                    }
                ],
                skinCodeTo: [
                    {
                        required: true,
                        message: this.$t('rules.inputSkinType'),
                        trigger: 'change'
                    }
                ]
            };
            return crules;
        },
        title() {
            return this.$t('lesson.copyLesson');
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            // 获取皮肤列表
            this.skinCodeList = [];
            getSkinCodeList().then(response => {
                this.skinCodeList = response.data.map(item => {
                    const params = {};
                    params.label = item.name;
                    params.value = item.code;
                    return params;
                });
            });
        },
        show() {
            this.dialogVisible = true;
        },
        doSave() {
            const self = this;
            this.$refs.dataform.validateForm(() => {
                if (this.formModel.skinCodeFrom != this.formModel.skinCodeTo) {
                    self.create();
                } else {
                    this.$alert(this.$t('lesson.countSkinCode'), this.$t('global.tips'), {
                        confirmButtonText: this.$t('global.confirm'),
                        callback: () => {}
                    });
                }
            });
        },
        create() {
            const self = this;
            this.loading = true;
            postOperateSaveAs(this.formModel.skinCodeFrom, this.formModel.skinCodeTo)
                .then(response => {
                    self.loading = false;
                    self.$message.success(this.$t('lesson.batchCreateSuccess'));
                    self.handleClose();
                    self.$emit('reloadTable'); // 刷新列表
                })
                .catch(error => {
                    self.loading = false;
                    self.$message.error(
                        `${this.$('error.batchCreateFailed')}:${error.message}`
                    );
                });
        },
        handleClose() {
            this.$refs.dataform.resetForm();
            this.dialogVisible = false;
        }
    }
};
</script>
