<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="state">
              <j-dict-select-tag type="list" v-model="model.state" dictCode="orderState" placeholder="请选择状" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="订单备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" type='textarea' placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="总金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalAmount">
              <a-input-number v-model="model.totalAmount" placeholder="请输入总金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="用户id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <a-input v-model="model.userId" placeholder="请输入用户id" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addr">
              <a-input v-model="model.addr" placeholder="请输入地址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="物料确认人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dispacher">
              <a-input v-model="model.materialClerk" placeholder="请输入确认人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="物料确认时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dispacherTime">
              <j-date placeholder="请选择物料确认时间" v-model="model.materialTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="物料确认意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financeRemark">
              <a-input v-model="model.materialRemark" type='textarea' placeholder="请输入审核意见" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="财务审核人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financeReview">
              <a-input v-model="model.financeReview" placeholder="请输入审核人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="财务审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financeTime">
              <j-date placeholder="请选择审核时间" v-model="model.financeTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="财务审核意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financeRemark">
              <a-input v-model="model.financeRemark" type='textarea' placeholder="请输入审核意见" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="付款回执单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <j-image-upload isMultiple  v-model="model.financeUrl" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="出货确认人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dispacher">
              <a-input v-model="model.dispacher" placeholder="请输入确认人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="出货确认时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dispacherTime">
              <j-date placeholder="请选择物料确认时间" v-model="model.dispacherTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="出货确认意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financeRemark">
              <a-input v-model="model.dispacherRemark" type='textarea' placeholder="请输入审核意见" ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="完结确认文件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <j-image-upload isMultiple  v-model="model.dispacherUrl" ></j-image-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" >
            <a-form-model-item label="收件人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recipient">
              <a-input v-model="model.recipient" placeholder="请输入收件人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="手机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile">
              <a-input v-model="model.mobile" placeholder="请输入手机" ></a-input>
            </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="orderDetailTable.loading"
          :columns="orderDetailTable.columns"
          :dataSource="orderDetailTable.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: 'OrderForm',
        mixins: [JVxeTableModelMixin],
        components: {
            JFormContainer,
        },
        data() {
            return {
                labelCol: {
                    xs: { span: 24 },
                    sm: { span: 5 },
                },
                wrapperCol: {
                    xs: { span: 24 },
                    sm: { span: 16 },
                },
                model:{
                },
                // 新增时子表默认添加几行空数据
                addDefaultRowNum: 1,
                validatorRules: {
                    addr: [
                        { required: true, message: '请输入地址!'},
                    ],
                },
                refKeys: ['orderDetail', ],
                tableKeys:['orderDetail', ],
                activeKey: 'orderDetail',
                // 订单详情
                orderDetailTable: {
                    loading: false,
                    dataSource: [],
                    columns: [
                        {
                            title: '主单id',
                            key: 'orderId',
                            type: JVXETypes.input,
                            width:"200px",
                            placeholder: '请输入${title}',
                            defaultValue:'',
                            validateRules: [{ required: true, message: '${title}不能为空' }],
                        },
                        {
                            title: '产品id',
                            key: 'goodsId',
                            type: JVXETypes.input,
                            width:"200px",
                            placeholder: '请输入${title}',
                            defaultValue:'',
                            validateRules: [{ required: true, message: '${title}不能为空' }],
                        },
                        {
                            title: '产品名称',
                            key: 'goodsName',
                            type: JVXETypes.input,
                            width:"200px",
                            placeholder: '请输入${title}',
                            defaultValue:'',
                            validateRules: [{ required: true, message: '${title}不能为空' }],
                        },
                        {
                            title: '产品数量',
                            key: 'goodsNo',
                            type: JVXETypes.input,
                            width:"200px",
                            placeholder: '请输入${title}',
                            defaultValue:'',
                        },
                        {
                            title: '备注',
                            key: 'remark',
                            type: JVXETypes.input,
                            width:"200px",
                            placeholder: '请输入${title}',
                            defaultValue:'',
                        },
                    ]
                },
                url: {
                    add: "/erp/order/add",
                    edit: "/erp/order/edit",
                    queryById: "/erp/order/queryById",
                    orderDetail: {
                        list: '/erp/order/queryOrderDetailByMainId'
                    },
                }
            }
        },
        props: {
            //表单禁用
            disabled: {
                type: Boolean,
                default: false,
                required: false
            }
        },
        computed: {
            formDisabled(){
                return this.disabled
            },
        },
        created () {
        },
        methods: {
            addBefore(){
                this.orderDetailTable.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.orderDetail.list, params, this.orderDetailTable)
                }
            },
            //校验所有一对一子表表单
            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, // 展开
                    orderDetailList: allValues.tablesValue[0].tableData,
                }
            },
            validateError(msg){
                this.$message.error(msg)
            },

        }
    }
</script>

<style scoped>
</style>