<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="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <j-date placeholder="请选择开始日期" v-model="model.startDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date placeholder="请选择结束日期" v-model="model.endDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="原价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priceOrigin">
              <a-input-number v-model="model.priceOrigin" placeholder="请输入原价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="优惠价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priceDiscount">
              <a-input-number v-model="model.priceDiscount" placeholder="请输入优惠价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="是否下架" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDel">
              <j-dict-select-tag type="list" v-model="model.isDel" dictCode="is_true" placeholder="请选择是否下架" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="关联活动id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activityId">
              <j-dict-select-tag type="list" v-model="model.activityId" dictCode="studytour_activity,title,id" placeholder="请选择关联活动id" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="年龄段价格表" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="studytourDatePriceTable.loading"
          :columns="studytourDatePriceTable.columns"
          :dataSource="studytourDatePriceTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'StudytourActivityDateForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['studytourDatePrice', ],
        tableKeys:['studytourDatePrice', ],
        activeKey: 'studytourDatePrice',
        // 年龄段价格表
        studytourDatePriceTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '年龄段id',
              key: 'periodId',
              type: JVXETypes.select,
              options:[],
              dictCode:"studytour_period,title,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '单人价格',
              key: 'price',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '关联团期id',
              key: 'dateId',
              type: JVXETypes.selectSearch,
              dictCode:"studytour_activity_date,id,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/studytourActivityDate/studytourActivityDate/add",
          edit: "/studytourActivityDate/studytourActivityDate/edit",
          queryById: "/studytourActivityDate/studytourActivityDate/queryById",
          studytourDatePrice: {
            list: '/studytourActivityDate/studytourActivityDate/queryStudytourDatePriceByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.studytourDatePriceTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.studytourDatePrice.list, params, this.studytourDatePriceTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          studytourDatePriceList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>