<template>
    <div>
      <div class="run-modal-style">
        <Row :gutter="20" class="code-row-bg">
          <Col span="24" style="border:none;">
          <Form :model="formInfo" ref="formDimension" class="ivu-form-no-margin-bottom" :rules="rule" :label-width="90">
            <Form-item prop="wdCode" :label="$t('module.dimension.dimensionCode')">
              <Input placeholder="" size="small" v-model="formInfo.wdCode" maxlength="30" style="width:90%" :disabled="noEdit"></Input>
            </Form-item>
            <Form-item prop="wdName" :label="$t('module.dimension.dimensionName')">
              <Input placeholder="" size="small" v-model="formInfo.wdName" maxlength="50" style="width:90%" :disabled="noEdit"></Input>
            </Form-item>
            <Form-item prop="wdDataUrl" :label="$t('module.dimension.dataConfig')">
              <Input placeholder="" size="small" v-model="formInfo.wdDataUrl" maxlength="256" style="width:90%"></Input>
            </Form-item>
            <Form-item :label="$t('module.dimension.desc')">
              <Input placeholder="" type="textarea" :rows="4" size="small" v-model="formInfo.wdDesc" maxlength="300" style="width:90%"></Input>
            </Form-item>
          </Form>
          </Col>
        </Row>
      </div>
    </div>
</template>
<script>
    export default{
        components:{

        },
        props:{
            dimensionId: {
                type: String,
                default() {
                    return "";
                }
            },
        },
        data(){
            return{
                noEdit: false,
                formInfo: {
                    dimensionId: "",
                    wdCode: "",
                    wdName: "",
                    wdDataUrl: "",
                    wdDesc: ""
                },
                rule: {
                    wdCode: [{ required: true, message: this.$t('module.dimension.warning.enterCode'), trigger: "blur" }],
                    wdName: [{ required: true, message:  this.$t('module.dimension.warning.enterName'), trigger: "blur" }],
                    wdDataUrl: [{ required: true, message:  this.$t('module.dimension.warning.enterConfig'), trigger: "blur" }],
                },
            }
        },
        computed:{

        },
        created:function(){
            console.log(this.dimensionId)
            if(this.dimensionId){
                this.noEdit = true;
                 $store.dispatch("QueryDimensionDetail",{dimensionId: this.dimensionId}).then(response => {
                 this.formInfo = {
                    dimensionId: response.dimensionId,
                    wdCode: response.wdCode,
                    wdName: response.wdName,
                    wdDataUrl: response.wdDataUrl,
                    wdDesc: response.wdDesc
                };
                });
                this.$emit("SetTitle", this.$t('module.dimension.editDimension'));
            }else{
                this.$emit("SetTitle", this.$t('module.dimension.createDimension'));
            }
            this.$emit("SetPageSize", 700);
            this.$emit('SetPageActions', [
                {
                    text: this.$t("common.save"),
                    theme: "primary",
                    icon: "save",
                    handle: this.save,
                }
            ]);
        },
        beforeMount:function(){

        },
        mounted:function(){

        },
        beforeDestroy:function(){

        },
        destroyed:function(){

        },
        methods:{
            save(){
                this.$refs["formDimension"].validate(valid => {
                    console.log(valid)
                    if(!valid){
                        return;
                    }else{
                        if(this.dimensionId){
                          let param = {
                            dimensionId: this.formInfo.dimensionId,
                            wdCode: this.formInfo.wdCode,
                            wdName: this.formInfo.wdName,
                            wdDataUrl: this.formInfo.wdDataUrl,
                            wdDesc: this.formInfo.wdDesc
                            };
                            $store.dispatch("UpdateDimension", param).then(response => {
                            if (response.statusCode == "0") {
                                this.editModal = false;
                                this.$Message.success({
                                content: this.$t('module.dimension.warning.updateSuccessfully'),
                                duration: 3
                                });
                                this.$emit("Close")
                            } else {
                                this.$Message.warning({
                                content: response.msg,
                                duration: 3
                                });
                            }
                            });
                        }else{
                            $store.dispatch("AddDimension", {
                                wdCode: this.formInfo.wdCode,
                                wdName: this.formInfo.wdName,
                                wdDataUrl: this.formInfo.wdDataUrl,
                                wdDesc: this.formInfo.wdDesc
                            }).then((response) => {
                                if(response.statusCode == "0"){
                                    this.$Message.success({
                                        content: this.$t('module.dimension.warning.createSuccessfully'),
                                        duration: 3
                                    });
                                    this.$emit("Close")
                                }else{
                                    this.$Message.warning({
                                        content: response.msg,
                                        duration: 3
                                    })
                                }
                            })
                        }
                    }
                });
            },
            getInfo(){
                $store.dispatch("QueryDimensionDetail", this.dimensionId).then(response => {
                this.formInfo = {
                    dimensionId: response.dimensionId,
                    wdCode: response.wdCode,
                    wdName: response.wdName,
                    wdDataUrl: response.wdDataUrl,
                    wdDesc: response.wdDesc
                };
                });
            }
        },
        watch:{

        },
        directives:{

        }
    }
</script>
