<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择父级节点"
                v-model="model.pid"
                dict="rescatalog,alias,id"
                pidField="pid"
                pidValue="0"
                hasChildField="has_child"
              >
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="图层名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入图层名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="图层别名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alias">
              <a-input v-model="model.alias" placeholder="请输入图层别名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="图层服务地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-input v-model="model.url" placeholder="请输入图层服务地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="子图层ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sublayerid">
              <a-input-number v-model="model.sublayerid" placeholder="请输入子图层ID" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否显示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visible">
              <j-dict-select-tag type="list" v-model="model.visible" dictCode="visible" placeholder="请选择是否显示"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否显示标注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labelsvisible">
              <j-dict-select-tag type="list" v-model="model.labelsvisible" dictCode="visible" placeholder="请选择是否显示标注"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="服务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="servicetype">
              <j-dict-select-tag type="list" v-model="model.servicetype" dictCode="servicetype" placeholder="请选择服务类型"
                                 @change='srvtypeChange'/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="显示层级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zindex">
              <a-input-number v-model="model.zindex" placeholder="请输入显示层级" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="列表显示顺序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="displayorder">
              <a-input-number v-model="model.displayorder" placeholder="请输入列表显示顺序" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="数据服务接口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataport">-->
          <!--              <a-input v-model="model.dataport" placeholder="请输入数据服务接口" ></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="数据关联字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relfield">-->
          <!--              <a-input v-model="model.relfield" placeholder="请输入数据关联字段" ></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="是否参与分析" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="analyzable">
              <j-dict-select-tag type="list" v-model="model.analyzable" dictCode="whether" placeholder="请选择图层是否参与分析"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分析类型名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourcename">
              <a-input v-model="model.sourcename" placeholder="请输入分析类型名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否参与查询" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="queryable">
              <j-dict-select-tag type="list" v-model="model.queryable" dictCode="whether" placeholder="请选择业务是否参与查询"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否返回树形查询结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returntree">
              <j-dict-select-tag type="list" v-model="model.returntree" dictCode="whether" placeholder="请选择是否返回树形查询结果"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="查询字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="queryfield">
              <a-input v-model="model.queryfield" placeholder="请输入查询字段"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="显示字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="showfield">
              <a-input v-model="model.showfield" placeholder="请输入显示字段"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="提示内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lyrhint">
              <a-input v-model="model.lyrhint" placeholder="请输入提示内容"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否是案件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="iscase">
              <j-dict-select-tag type="list" v-model="model.iscase" dictCode="whether" placeholder="请选择是否是案件图层"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否过滤" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="filter">
              <j-dict-select-tag type="list" v-model="model.filter" dictCode="whether" placeholder="请选择是否过滤"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数据表名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tablename">
              <a-input v-model="model.tablename" placeholder="请输入数据表名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="面积单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaunit">
              <a-input v-model="model.areaunit" placeholder="请输入面积单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="查询条件字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returnarea">
              <a-input v-model="model.queryname" placeholder="请输入查询条件字段"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="查询条件中文名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returnarea">
              <a-input v-model="model.chinesename" placeholder="请输入查询条件中文名称"/>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="图例" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="legend">-->
          <!--              <j-image-upload v-model="model.legend"></j-image-upload>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="图层样式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="legend">
              <a-input v-model="model.style" placeholder="请输入查询条件中文名称"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

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

export default {
  name: "RescatalogModal",
  components: {},
  data() {
    return {
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },

      confirmLoading: false,
      validatorRules: {},
      url: {
        add: "/resManager.catalog/rescatalog/add",
        edit: "/resManager.catalog/rescatalog/edit",
      },
      expandedRowKeys: [],
      pidField: "pid"

    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add(obj) {
      this.modelDefault.pid = ''
      this.edit(Object.assign(this.modelDefault, obj));
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    close() {
      this.$emit('close');
      this.visible = false;
      this.$refs.form.clearValidate()
    },
    handleOk() {
      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';
          }
          if (this.model.id && this.model.id === this.model[this.pidField]) {
            that.$message.warning("父级节点不能选择自己");
            that.confirmLoading = false;
            return;
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              this.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    submitSuccess(formData, flag) {
      if (!formData.id) {
        let treeData = this.$refs.treeSelect.getCurrTreeData()
        this.expandedRowKeys = []
        this.getExpandKeysByPid(formData[this.pidField], treeData, treeData)
        this.$emit('ok', formData, this.expandedRowKeys.reverse());
      } else {
        this.$emit('ok', formData, flag);
      }
    },
    getExpandKeysByPid(pid, arr, all) {
      if (pid && arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].key == pid) {
            this.expandedRowKeys.push(arr[i].key)
            this.getExpandKeysByPid(arr[i]['parentId'], all, all)
          } else {
            this.getExpandKeysByPid(pid, arr[i].children, all)
          }
        }
      }
    },
    srvtypeChange(val) {
      if (val == 'TileLayer') {
        this.model.sublayerid = '';
      }
    }
  }
}
</script>
