<template>
    <div>
        <Form :model="formSearch" :rules="rules"  :label-width="85">
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item :label="$t('module.valueSet.vsCode')">
                <Input  v-model="formSearch.vsCode" style="width: 236.5px" readonly/>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item :label="$t('module.valueSet.vsName')">
                <Input v-model="formSearch.vsName"></Input>
                </Form-item>
                </Col>           
            </Row>
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item :label="$t('module.valueSet.vsType')">
                <Input v-model="formSearch.vsType"></Input>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item :label="$t('module.valueSet.parentVs')">
                    <Input v-model="formSearch.vsParentName" readonly></Input>
                </Form-item>
                </Col>
            </Row>
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item :label="$t('module.userManage.affiliation')">
                    <Input v-model="formSearch.orgName" readonly></Input>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item :label="$t('module.valueSet.vsValueSource')">
                    <Input v-model="formSearch.vsValueSource" readonly></Input>
                </Form-item>
                </Col>
            </Row>
        </Form>
        <hr>
        <Alert closable show-icon>
            <span slot="desc">
                <div>
                {{$t("module.valueSet.sqlReferenceFormat")}}select id as vsi_id,key as vsi_key,value as vsi_value from sys_value_set
                </div>
            </span>
        </Alert>
        <Form :model="formSql" slot="search-bar" label-position="top"  inline>
            <Form-item :label="$t('module.valueSet.sqlSentence')">
                <Input type="textarea" rows="4" v-model="formSql.vsiSql" style="width: 680px"></Input>
            </Form-item>
        </Form>
    </div>
</template>
<script>
    import SQLPreview from './SQLPreview.vue';
    export default{
        components:{

        },
        props: {
            vsId: {
                type: String,
                default() {
                    return "";
                }
            },
            vsParam: {
                type: String,
                default() {
                    return "";
                }
            }
        },
        data(){
            return{
                formSearch: {
                    vsCode: "",
                    vsName: "",
                    orgName: "",
                    vsParentName: "",
                    vsType: "",
                    vsValueSource: "",
                    enabled: ""
                },
                formSql: {
                    vsiSql: "",
                    sqlEdit: false,
                    sqlVsiId: "",
                    sqlVersion: "",
                },
            }
        },
        computed:{

        },
        created:function(){
            $store.dispatch("GetValueSetInfo", this.vsId).then((response) => {
                this.formSearch.vsCode = response.dataResult.vsCode;
                this.formSearch.vsName = response.dataResult.vsName;
                this.formSearch.vsType = response.dataResult.vsType == "1" ? this.$t("module.valueSet.independent") : this.$t("module.valueSet.subordinate");
                this.formSearch.vsParentName = response.dataResult.vsParentName;
                this.formSearch.orgName = response.dataResult.orgName;
                this.formSearch.enabled = response.dataResult.enabled == "1" ? this.$t("common.valid") : this.$t("common.invalid");
                this.formSearch.vsValueSource = response.dataResult.vsValueSource == "1" ? this.$t("module.valueSet.userDefine") : this.$t("module.valueSet.dynamicSql");
            })
            $store.dispatch("GetSQLInfo", this.vsParam).then((response) => {
                if(!response.dataResult){
                    this.$emit("SetTitle", this.$t("common.create"));
                    this.sqlEdit = false;
                }else{
                    this.$emit("SetTitle", this.$t("common.edit"));
                    this.formSql.vsiSql= response.dataResult.vsiSql;
                    this.sqlVsiId = response.dataResult.vsiSqlId;
                    this.sqlVersion = response.dataResult.version;
                    this.sqlEdit = true;
                }
                 this.$emit("SetPageSize", 700);
                 this.$emit('SetPageActions', [
                    {
                        text: this.$t("module.valueSet.preview"),
                        theme: "primary",
                        icon: "add",
                        handle: this.preview,
                    },
                    {
                        text: this.$t("common.save"),
                        theme: "success",
                        icon: "checkmark-round",
                        handle: this.AddOrUpdate,
                    }
                ]);
            })
        },
        beforeMount:function(){

        },
        mounted:function(){

        },
        beforeDestroy:function(){

        },
        destroyed:function(){

        },
        methods:{
            preview() {
                new this.$pageModal(
                    SQLPreview,
                    {
                        props:{
                            sqlSentence: this.formSql.vsiSql
                        }
                    },(data) => {

                    }
                )
            },
            AddOrUpdate(){
                if(this.sqlEdit){
                    $store.dispatch("UpdateVsSQL", {
                        vsiSqlId: this.sqlVsiId,
                        version: this.sqlVersion,
                        vsCode: this.formSearch.vsCode,
                        vsiSql: this.formSql.vsiSql,
                    }).then((response) => {
                        if(response.statusCode == "0"){
                            this.$Message.success({
                                content: this.$t("module.valueSet.warning.editSqlSuccessfully"),
                                duration: 3
                            });
                            this.$emit("Close")
                        }else{
                            this.$Message.warning({
                                content: response.msg,
                                duration: 3
                            })
                        }
                    })
                }else{
                    $store.dispatch("AddVsSQL", {
                        vsCode: this.formSearch.vsCode,
                        vsiSql: this.formSql.vsiSql,
                    }).then((response) => {
                        if(response.statusCose == "0"){
                            this.$Message.success({
                                content: this.$t("module.valueSet.warning.createSqlSuccessfully"),
                                duration: 3
                            })
                        }else{
                            this.$Message.warning({
                                content: response.msg,
                                duration: 3
                            })
                        }
                    })

                }
            },
            cancel() {
                this.$emit("Close")
            }
        },
        watch:{

        },
        directives:{

        }
    }
</script>
