<template>
  <a-spin :spinning="loading">
    <a-form v-bind="formItemLayout">
      <a-row>
        <a-col :span="24">
          <a-form-item label="题目编号" v-bind="validateInfos.wjIndex">
	          <a-input-number v-model:value="formData.wjIndex" placeholder="请输入题目编号" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题标题" v-bind="validateInfos.wjTitle">
            <a-input v-model:value="formData.wjTitle" placeholder="请输入问题标题" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否必填" v-bind="validateInfos.isRequir">
            <a-input v-model:value="formData.isRequir" placeholder="请输入是否必填" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题分值" v-bind="validateInfos.wjScore">
	          <a-input-number v-model:value="formData.wjScore" placeholder="请输入问题分值" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

		<!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated>
      <a-tab-pane tab="选项信息" key="wjxWjxxTmxx" :forceRender="true">
        <j-vxe-table
          :keep-source="true"
          resizable
          ref="wjxWjxxTmxxTableRef"
          :loading="wjxWjxxTmxxTable.loading"
          :columns="wjxWjxxTmxxTable.columns"
          :dataSource="wjxWjxxTmxxTable.dataSource"
          :height="340"
          :disabled="disabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive, computed, toRaw, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useValidateAntFormAndTable } from '/@/hooks/system/useJvxeMethods';
  import { queryWjxWjxxTmxxListByMainId, queryDataById, saveOrUpdate } from '../WjxWjxxTmlb.api';
  import { JVxeTable } from '/@/components/jeecg/JVxeTable';
  import {wjxWjxxTmxxColumns} from '../WjxWjxxTmlb.data';
  import { Form } from 'ant-design-vue';
  const useForm = Form.useForm;

  export default defineComponent({
    name: "WjxWjxxTmlbForm",
    components:{
      JVxeTable,
    },
    props:{
      formDisabled:{
        type: Boolean,
        default: false
      },
      formData: { type: Object, default: ()=>{} },
      formBpm: { type: Boolean, default: true }
    },
    emits:['success'],
    setup(props, {emit}) {
      const loading = ref(false);
      const wjxWjxxTmxxTableRef = ref();
      const wjxWjxxTmxxTable = reactive<Record<string, any>>({
        loading: false,
        columns: wjxWjxxTmxxColumns,
        dataSource: []
      });
      const activeKey = ref('wjxWjxxTmxx');
      const formData = reactive<Record<string, any>>({
        id: '',
        wjIndex: '',
        wjTitle: '',
        isRequir: '',
        wjScore: '',
      });

      //表单验证
      const validatorRules = reactive({
      });
      const {resetFields, validate, validateInfos} = useForm(formData, validatorRules, {immediate: true});
      const dbData = {};
      const formItemLayout = {
        labelCol: {xs: {span: 24}, sm: {span: 5}},
        wrapperCol: {xs: {span: 24}, sm: {span: 16}},
      };

      // 表单禁用
      const disabled = computed(()=>{
        if(props.formBpm === true){
          if(props.formData.disabled === false){
            return false;
          }else{
            return true;
          }
        }
        return props.formDisabled;
      });

      

      function add() {
        resetFields();
        wjxWjxxTmxxTable.dataSource = [];
      }

      async function edit(row) {
        //主表数据
        await queryMainData(row.id);
        //子表数据
        const wjxWjxxTmxxDataList = await queryWjxWjxxTmxxListByMainId(row['id']);
        wjxWjxxTmxxTable.dataSource = [...wjxWjxxTmxxDataList];
      }

      async function queryMainData(id) {
        const row = await queryDataById(id);
        Object.keys(row).map(k => {
          formData[k] = row[k];
        });
      }

      const {getSubFormAndTableData, transformData} = useValidateAntFormAndTable(activeKey, {
        'wjxWjxxTmxx': wjxWjxxTmxxTableRef,
      });

      async function getFormData() {
        await validate();
        return transformData(toRaw(formData))
      }

      async function submitForm() {
        const mainData = await getFormData();
        const subData = await getSubFormAndTableData();
        const values = Object.assign({}, dbData, mainData, subData);
        console.log('表单提交数据', values)
        const isUpdate = values.id ? true : false
        await saveOrUpdate(values, isUpdate);
        //关闭弹窗
        emit('success');
      }
      
      function setFieldsValue(values) {
        if(values){
          Object.keys(values).map(k=>{
            formData[k] = values[k];
          });
        }
      }

      /**
       * 值改变事件触发-树控件回调
       * @param key
       * @param value
       */
      function handleFormChange(key, value) {
        formData[key] = value;
      }


      return {
        wjxWjxxTmxxTableRef,
        wjxWjxxTmxxTable,
        validatorRules,
        validateInfos,
        activeKey,
        loading,
        formData,
        setFieldsValue,
        handleFormChange,
        formItemLayout,
        disabled,
        getFormData,
        submitForm,
        add,
        edit
      }
    }
  });
</script>