<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!-- 主表单区域 -->
        <a-row class="form-row" :gutter="16">
          <a-col :lg="9">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="表名称">
              <a-input disabled v-model="dataSourceFieldModel.TableName" />
            </a-form-item>
          </a-col>
        </a-row>

        <!-- 子表单区域 -->
        <a-tabs defaultActiveKey="1" >
          <a-tab-pane tab="字段信息" key="1">
            <div>
              <a-row type="flex" style="margin-bottom:10px" :gutter="16">
                <a-col :span="5">源表字段</a-col>
                <a-col :span="5">源表字段类型</a-col>
                <a-col :span="3"></a-col>
                <a-col :span="5">目标表字段</a-col>
                <a-col :span="5">目标表字段类型</a-col>
              </a-row>
              <a-row type="flex" style="margin-bottom:10px" :gutter="16" v-for="(it,index) in this.dataSourceFieldModel.longList" :key="index">
                <a-col :span="5">
                  <a-form-item>
                    <a-input placeholder="源表字段" v-if="index < dataSourceFieldModel.SourceField.length" disabled v-model="dataSourceFieldModel.SourceField[index].fieldName" />
                  </a-form-item>
                </a-col>
                <a-col :span="5">
                  <a-form-item>
                    <a-input placeholder="源表字段" v-if="index < dataSourceFieldModel.SourceField.length" disabled v-model="dataSourceFieldModel.SourceField[index].fieldType" />
                  </a-form-item>
                </a-col>
                <a-col :span="3">

                </a-col>
                <a-col :span="5">
                  <a-form-item>
                    <a-input placeholder="源表字段" v-if="index < dataSourceFieldModel.TargetField.length" disabled v-model="dataSourceFieldModel.TargetField[index].fieldName" />
                  </a-form-item>
                </a-col>
                <a-col :span="5">
                  <a-form-item>
                    <a-input placeholder="源表字段" v-if="index < dataSourceFieldModel.TargetField.length" disabled v-model="dataSourceFieldModel.TargetField[index].fieldType" />
                  </a-form-item>
                </a-col>
              </a-row>
            </div>
          </a-tab-pane>
        </a-tabs>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction,getAction } from '@/api/manage'
import JDate from '@/components/jeecg/JDate'
import pick from 'lodash.pick'
import moment from "moment"

export default {
  name: "JeecgOrderMainModal",
  components: {
    JDate
  },
  data () {
    return {
      title:"操作",
      visible: false,
      dataSourceFieldModel: {
        TableName:'',
        longList:[{}],
        SourceField:[{}],
        TargetField:[{}]
      },
      dataSourceFields:[],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules:{
      },
      url: {
        add: "/idcdatasourcefield/idcDataSourceField/add",
        edit: "/IdcDataSourceTable/idcDataSourceTable/editField",
        dataSourceFieldList: "/SynchronizationStrategy/synchronizationStrategy/getTableDetailByStrategy",
      },
    }
  },
  created () {
  },
  methods: {
    show(roleId){
      this.roleId=roleId
      this.visible = true;
    },
    add () {
      this.edit({});
    },
    edit (record) {
      this.form.resetFields();
      this.dataSourceFields = Object.assign({}, record);
      this.dataSourceFieldModel.idcDataSourceFields = [{}];
      //--------------------------------------------------------
      //初始化明细表数据
      if(this.dataSourceFields.id){
        let params = {id:this.dataSourceFields.id}
        //初始化字段列表
        getAction(this.url.dataSourceFieldList,{strategyId:params.id}).then((res)=>{
          if(res.success){
            this.dataSourceFieldModel = res.result;
            if(this.dataSourceFieldModel.SourceField.length > this.dataSourceFieldModel.TargetField){
              this.dataSourceFieldModel.longList = this.dataSourceFieldModel.SourceField;
            }else{
              this.dataSourceFieldModel.longList = this.dataSourceFieldModel.TargetField;
            }
            this.$forceUpdate()
          }
        })
      }
      //--------------------------------------------------------
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.dataSourceFieldModel,'instanceName','tableName'))
      });
    },
    close () {
      this.$emit('close');
      this.visible = false;
    },
    handleOk () {
      this.close();
    },
    handleCancel () {
      this.close()
    },
    addRowCustom () {
      this.dataSourceFieldModel.idcDataSourceFields.push({});
      this.$forceUpdate();
    },
    delRowCustom (index) {
      console.log(index)
      let all = this.form.getFieldsValue()
      all['idcDataSourceFields'].splice(index,1);
      this.form.setFieldsValue(all)
      this.dataSourceFieldModel.idcDataSourceFields.splice(index,1);
      this.$forceUpdate();
    }
  }
}
</script>

<style scoped>
.ant-btn {
  padding: 0 10px;
  margin-left: 3px;
}
.ant-form-item-control {
  line-height: 0px;
}
/** 主表单行间距 */
.ant-form .ant-form-item {
  margin-bottom: 10px;
}
/** Tab页面行间距 */
.ant-tabs-content .ant-form-item {
  margin-bottom: 0px;
}
</style>
