<template>
  <div class="SapFormContent">
    <el-col :span="colSpan" v-if="[1,2,3,4,5,6,7,8].some(i => i == item.dataType)">
      <sap-form-item :item="item" :label-width="labelWidth">
        <el-input-all v-model="value" :item="item"/>
      </sap-form-item>
    </el-col>

    <template v-else>
      <el-col class="obj-title">{{ item.name }}</el-col>
      <!-- 对象 -->
      <template v-if="item.dataType === 9">
        <template v-for="child in item.children">
          <sap-form-item v-show="child.show" :item="child" :label-width="labelWidth">
            <el-input-all v-model="value[child.sapField]" :item="child"/>
          </sap-form-item>
        </template>
      </template>

      <!-- 数组 -->
      <template v-if="item.dataType === 10">
        <div style="width: 100%; overflow-x: auto">
          <div>
            <div class="item-head clearfix" style="width: max-content;">
              <template v-for="child in item.children">
                <div v-show="child.show" :key="child.id + 'head'" class="item-label">{{ child.name }}</div>
              </template>
            </div>
            <template v-for="(sub, i) in value"><!-- ITEM -->
              <div class="item-content" style="width: max-content;" :key="'sub' + '' + i">
                <template v-for="(child, j) in item.children">
                  <div v-show="child.show" v-if="[1,2,3,4,5,6,7,8].some(dataType => dataType == child.dataType)" class="item-value" :key="child.id + j">
                    <sap-form-item :item="child" label-width="0px">
                      <el-input-all v-model="sub[child.sapField]" :item="child"/>
                    </sap-form-item>
                  </div>
                  <template v-else>
                    <div class="item-value">
                      <el-button type="text" size="mini" @click="onShowSize(sub)">{{$t('查看')}}</el-button>
                    </div>
                    <div class="item-sub clearfix" v-if="sub.showSize">
                      <div class="item-sub-head" style="width: max-content;">
                        <div v-for="son in child.children" :key="son.id + 'head'" class="item-sub-label">{{ son.name }}</div>
                      </div>
                      <!-- SIZE -->
                      <div v-for="leaf in sub[child.sapField]" class="item-child-content clearfix" style="width: max-content;">
                        <div v-for="(son, j) in child.children" class="item-value" :key="son.id + '' + j">
                          <sap-form-item :item="son" label-width="0px">
                            <el-input-all v-model="leaf[son.sapField]" :item="son"/>
                          </sap-form-item>
                        </div>
                      </div>
                    </div>
                  </template>
                </template>
              </div>
            </template>
          </div>
        </div>
      </template>
    </template>
  </div>
</template>

<script>


import SapFormItem from '@/views/mes/md/sapField/components/SapFormItem'

export default {
  name: 'SapFormContext',
  components: {
    SapFormItem
  },
  model: {prop: 'val', event: 'update'},
  props: {
    val: { type: [Number, String, Date, Array, Object] },
    item: { type: Object, required: true },
    showLabel: { type: Boolean, default: true },
    colSpan: { type: Number, default: 6 },
    labelWidth: { type: String, default: '100px'},
  },
  computed: {
    value: {
      get() {
        return this.val
      },
      set(val) {
        this.$emit('update', val)
      }
    }
  },
  data() {
    return {}
  },
  methods: {
    onValidate(rule, value, callback, item) {
      if (value === '' || value == null) {
        callback(new Error(item.errMsg))
      } else {
        if (item.dataType === 1) {
          if (value.length > item.length || !new RegExp(item.regEx).test(value)) callback(new Error(item.errMsg))
        } else if ([2, 3].includes(item.dataType)) {
          if (value < item.min || value > max) callback(new Error(item.errMsg))
        }
        callback()
      }
    },
    onShowSize(sub) {
      sub.showSize = !sub.showSize
      this.$forceUpdate()
    }
  },
  created() {

  }
}
</script>

<style scoped>
.obj-title {
  line-height: 40px;
  font-weight: bold;
  margin: 20px;
}
.item-label, .item-value, .item-sub-label {
  width: 150px;
  height: 50px;
  line-height: 28px;
  float: left;
  overflow: hidden;
  text-wrap: nowrap;
  margin-left: 10px;
}

.item-sub {
  width: 100%;
}
</style>
