<template>
  <div>
  <!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated @change="handleChangeTabs">
     <!--主表区域 -->
     <a-tab-pane tab="showlist" :key="refKeys[0]" :forceRender="true" :style="tabsStyle">
       <BasicForm @register="registerForm" ref="formRef"/>
     </a-tab-pane>
  <!--子表单区域 -->
      <a-tab-pane tab="概览" key="okkiOverview" :forceRender="true" :style="tabsStyle">
        <OkkiOverviewForm ref="okkiOverviewForm" :disabled="formDisabled"></OkkiOverviewForm>
      </a-tab-pane>

      <a-tab-pane tab="产品" key="okkiProductList" :forceRender="true" :style="tabsStyle">
        <OkkiProductListForm ref="okkiProductListForm" :disabled="formDisabled"></OkkiProductListForm>
      </a-tab-pane>

      <a-tab-pane tab="优势" key="okkiAdvantage" :forceRender="true" :style="tabsStyle">
        <JVxeTable
          keep-source
          resizable
          ref="okkiAdvantage"
          v-if="okkiAdvantageTable.show"
          :loading="okkiAdvantageTable.loading"
          :columns="okkiAdvantageTable.columns"
          :dataSource="okkiAdvantageTable.dataSource"
          :height="340"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="关键词" key="okkiKeywords" :forceRender="true" :style="tabsStyle">
        <JVxeTable
          keep-source
          resizable
          ref="okkiKeywords"
          v-if="okkiKeywordsTable.show"
          :loading="okkiKeywordsTable.loading"
          :columns="okkiKeywordsTable.columns"
          :dataSource="okkiKeywordsTable.dataSource"
          :height="340"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="常见问题" key="okkiFaq" :forceRender="true" :style="tabsStyle">
        <JVxeTable
          keep-source
          resizable
          ref="okkiFaq"
          v-if="okkiFaqTable.show"
          :loading="okkiFaqTable.loading"
          :columns="okkiFaqTable.columns"
          :dataSource="okkiFaqTable.dataSource"
          :height="340"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>

    <div style="width: 100%;text-align: center;margin-top: 10px;" v-if="showFlowSubmitButton">
      <a-button preIcon="ant-design:check-outlined" style="width: 126px" type="primary" @click="handleSubmit">提 交</a-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
    import { defHttp } from '/@/utils/http/axios';
    import {ref, computed, unref,reactive, onMounted, defineProps } from 'vue';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import { JVxeTable } from '/@/components/jeecg/JVxeTable'
    import { useJvxeMethod } from '/@/hooks/system/useJvxeMethods.ts'
    import OkkiOverviewForm from './OkkiOverviewForm.vue'
    import OkkiProductListForm from './OkkiProductListForm.vue'
    import {formSchema,okkiAdvantageColumns,okkiKeywordsColumns,okkiFaqColumns} from '../OkkiShowlist.data';
    import {saveOrUpdate,okkiOverviewList,okkiProductListList,okkiAdvantageList,okkiKeywordsList,okkiFaqList} from '../OkkiShowlist.api';
    import { VALIDATE_FAILED } from '/@/utils/common/vxeUtils'
    const refKeys = ref(['okkiShowlist','okkiOverview', 'okkiProductList', 'okkiAdvantage', 'okkiKeywords', 'okkiFaq', ]);
    const activeKey = ref('okkiShowlist');
    const okkiOverviewForm = ref();
    const okkiProductListForm = ref();
    const okkiAdvantage = ref();
    const okkiKeywords = ref();
    const okkiFaq = ref();
    const tableRefs = {okkiAdvantage, okkiKeywords, okkiFaq, };
    const okkiAdvantageTable = reactive({
          loading: false,
          dataSource: [],
          columns:okkiAdvantageColumns,
          show: false
    })
    const okkiKeywordsTable = reactive({
          loading: false,
          dataSource: [],
          columns:okkiKeywordsColumns,
          show: false
    })
    const okkiFaqTable = reactive({
          loading: false,
          dataSource: [],
          columns:okkiFaqColumns,
          show: false
    })

    const props = defineProps({
      formData: { type: Object, default: ()=>{} },
      formBpm: { type: Boolean, default: true }
    });
    const formDisabled = computed(()=>{
      if(props.formBpm === true){
        if(props.formData.disabled === false){
          return false;
        }
      }
      return true;
    });
    // 是否显示提交按钮
    const showFlowSubmitButton = computed(()=>{
      if(props.formBpm === true){
        if(props.formData.disabled === false){
          return true
        }
      }
      return false
    });
    
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });

    onMounted(()=>{
      initFormData();
    });
    //渲染流程表单数据
    const queryByIdUrl = '/showlist/okkiShowlist/queryById';
    async function initFormData(){
      if(props.formBpm === true){
        await reset();
        let params = {id: props.formData.dataId};
        const data = await defHttp.get({url: queryByIdUrl, params});
        //表单赋值
        await setFieldsValue({
          ...data
        });
        okkiOverviewForm.value.initFormData(okkiOverviewList, data.id)
        okkiProductListForm.value.initFormData(okkiProductListList, data.id)
        requestSubTableData(okkiAdvantageList, {id: data.id}, okkiAdvantageTable, ()=>{
          okkiAdvantageTable.show = true;
        })
        requestSubTableData(okkiKeywordsList, {id: data.id}, okkiKeywordsTable, ()=>{
          okkiKeywordsTable.show = true;
        })
        requestSubTableData(okkiFaqList, {id: data.id}, okkiFaqTable, ()=>{
          okkiFaqTable.show = true;
        })
        // 隐藏底部时禁用整个表单
        setProps({ disabled: formDisabled.value })
      }
    }

    //方法配置
    const [handleChangeTabs,handleSubmit,requestSubTableData,formRef] = useJvxeMethod(requestAddOrEdit,classifyIntoFormData,tableRefs,activeKey,refKeys,validateSubForm);
    // 弹窗tabs滚动区域的高度
    const tabsStyle = computed(() => {
      let height: Nullable<string> = null
      let minHeight = '100px'
      // 弹窗wrapper
      let overflow = 'auto';
      return {height, minHeight, overflow};
    })

    async function reset(){
      await resetFields();
      activeKey.value = 'okkiShowlist';
      okkiOverviewForm.value.resetFields();
      okkiProductListForm.value.resetFields();
      okkiAdvantageTable.dataSource = [];
      okkiKeywordsTable.dataSource = [];
      okkiFaqTable.dataSource = [];
    }
    function classifyIntoFormData(allValues) {
         let main = Object.assign({}, allValues.formValue)
         return {
           ...main, // 展开
           okkiOverviewList: okkiOverviewForm.value.getFormData(),
           okkiProductListList: okkiProductListForm.value.getFormData(),
           okkiAdvantageList: allValues.tablesValue[0].tableData,
           okkiKeywordsList: allValues.tablesValue[1].tableData,
           okkiFaqList: allValues.tablesValue[2].tableData,
         }
       }
     //校验所有一对一子表表单
     function validateSubForm(allValues){
         return new Promise((resolve,reject)=>{
             Promise.all([
                  okkiOverviewForm.value.validateForm(1),
                  okkiProductListForm.value.validateForm(2),
             ]).then(() => {
                 resolve(allValues)
             }).catch(e => {
                 if (e.error === VALIDATE_FAILED) {
                     // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                     activeKey.value = e.index == null ? unref(activeKey) : refKeys.value[e.index]
                 } else {
                     console.error(e)
                 }
             })
         })
     }
    //表单提交事件
    async function requestAddOrEdit(values) {
      //提交表单
      await saveOrUpdate(values, true);
    }
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
    :deep(.ant-input-number){
        width: 100%
    }

    :deep(.ant-calendar-picker){
        width: 100%
    }
</style>

<style lang="less">
// Online表单Tab风格专属样式
.j-cgform-tab-modal {
  .ant-modal-header {
    padding-top: 8px;
    padding-bottom: 8px;
    border-bottom: none !important;
  }

  .ant-modal .ant-modal-body > .scrollbar,
  .ant-tabs-nav .ant-tabs-tab {
    padding-top: 0;
  }

  .ant-tabs-top-bar {
    width: calc(100% - 55px);
    position: relative;
    left: -14px;
  }

  .ant-tabs .ant-tabs-top-content > .ant-tabs-tabpane {
    overflow: hidden auto;
  }
}
</style>