<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="专题图ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topicid">
              <a-input v-model="model.topicid" placeholder="请输入专题图ID"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专题图名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topicname">
              <a-input v-model="model.topicname" placeholder="请输入专题图名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="自定义默认加载的图层" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="displayers">
              <j-multi-select-tag type="checkbox" v-model="model.displayers" dictCode="VIEW_BUSS_LAYERS,alias,id" placeholder="请选择自定义默认加载的图层" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="自定义加载的工具" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="disptools" >
              <j-multi-select-tag type="checkbox" v-model="model.disptools" dictCode="VIEW_MAP_TOOLS,name,code"
                                 @change='handleToolConfig' placeholder="请选择自定义加载的工具" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="自定义查询类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="querytypes" v-if='this.showQueryConfig'>
              <j-multi-select-tag type="checkbox" v-model="model.querytypes" dictCode="VIEW_QUERY_CONFIG,tablealias,id" placeholder="请选择自定义查询类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="自定义分析类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="analysistypes" v-if='this.showAnalysisConfig'>
              <j-multi-select-tag type="checkbox" v-model="model.analysistypes" dictCode="VIEW_ANALYSIS_CONFIG,sourcename,tablename" placeholder="请选择自定义分析类型" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'TopicsConfigForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        showQueryConfig:false,
        showAnalysisConfig:false,
        validatorRules: {
        },
        url: {
          add: "/topicManage/topicsConfig/add",
          edit: "/topicManage/topicsConfig/edit",
          queryById: "/topicManage/topicsConfig/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        let disptools = record.disptools;
        this.handleToolConfig(disptools);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleToolConfig(value){
        if(value.indexOf("isQuery") !=-1){
          this.showQueryConfig = true;
        }else{
          this.showQueryConfig = false;
        }
        if(value.indexOf("isAnalysis") !=-1){
          this.showAnalysisConfig = true;
        }else{
          this.showAnalysisConfig = false;
        }
      }
    }
  }
</script>
