<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" :before-close="doClose" center>
    <data-form ref="dataform" :form="form" :form-model="formModel" :rules="rules" />
    <span slot="footer" class="dialog-footer">
      <el-button v-if="!isPut" type="primary" @click="doCreate">{{ $t('global.confirm') }}</el-button>
      <el-button v-if="isPut" type="primary" @click="putInfo">更新</el-button>
      <el-button @click="doClose">{{ $t('global.cancel') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getStationList } from '@/api/runplan';
import { createIbp, putIbpBasicInfo } from '@/api/ibp';

export default {
    name: 'ScriptDraft',
    props: {
        title: {
            type: String,
            default() {
                return '';
            }
        }
    },
    data() {
        return {
            dialogVisible: false,
            isPut: false,
            stationList: [],
            ibpId: '',
            formModel: {
                stationCode: ''
            }
        };
    },
    computed: {
        form() {
            const form = {
                labelWidth: '150px',
                items: [
                    { prop: 'stationCode', label: '关联站台:', type: 'select', options: this.stationList }
                ]
            };
            return form;
        },
        rules() {
            const crules = {
                stationCode: [
                    { required: true, message: '请选择', trigger: 'change' }
                ]
            };
            return crules;
        }
    },
    watch: {
        '$route.params.mapId': function() {
            this.getStationLIst();
        }
    },
    async created () {
        this.getStationLIst();
    },
    methods: {
        async getStationLIst() {
            try {
                const res = await getStationList(this.$route.params.mapId);
                this.stationList = [];
                if (res.code == 200) {
                    res.data.forEach(station => {
                        const param = {
                            label: station.name,
                            value: station.code
                        };
                        this.stationList.push(param);
                    });
                }
            } catch (error) {
                console.log(error);
            }
        },
        doShow(data) {
            if (data) {
                this.formModel.stationCode = data.stationCode;
                this.ibpId = data.id;
                this.isPut = true;
            } else {
                this.formModel.stationCode = '';
            }
            this.dialogVisible = true;
        },
        doCreate() {
            this.$refs.dataform.validateForm(() => {
                const param = {
                    mapId: this.$route.params.mapId,
                    stationCode: this.formModel.stationCode
                };
                createIbp(param).then(resp => {
                    this.$message.success('创建成功');
                    this.$emit('reloadTable');
                }).catch(error => {
                    this.$messageBox(`创建失败: ${error.message}`);
                });
                this.doClose();
            });
        },
        putInfo() {
            this.$refs.dataform.validateForm(() => {
                const param = {
                    mapId: this.$route.params.mapId,
                    stationCode: this.formModel.stationCode
                };
                putIbpBasicInfo(this.ibpId, param).then(resp => {
                    this.$message.success('更新成功');
                    this.$emit('reloadTable');
                    this.isPut = false;
                }).catch(error => {
                    this.$messageBox(`更新失败: ${error.message}`);
                });
                this.doClose();
            });
        },
        doClose() {
            this.$refs.dataform.resetForm();
            this.dialogVisible = false;
        }
    }
};
</script>
<style lang="scss" scoped>
/deep/ .el-dialog--center .el-dialog__body{
    padding: 25px 65px 30px 10px;
}
</style>
