<!--  -->
<template>
  <div class="m-core" style="margin-bottom: 18px;overflow: hidden;">
    <el-alert
        title="云教室规则"
        type="info"
        :closable="false"
        style="margin-bottom: 20px"
      ></el-alert>
      <el-form ref="form" label-position="top" :model="form" label-width="200px"  :inline="true">
        <el-form-item label="云教室人数上限（含老师）" prop="cloud_room_up_limit"
          :rules="[{ required: true, message: '请输入云教室人数上限', trigger: 'blur' },
            { type: 'number', message: '云教室人数上限必须为数字值', trigger: 'blur' }]">
          <el-input
            placeholder="请输入云教室人数上限"
            v-model.number="form.cloud_room_up_limit"
            :disabled="isDisabled"
            @blur="onUpLimitChange"
            type="number"
          >
            <div slot="append">人</div>
          </el-input>
        </el-form-item>
      </el-form>
      <el-col :lg="12" :md="18" :sm="18" :xs="24">
        <el-table :data="form.roomConfig"
                  style="width:100% !important;"
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column prop="num" label="人数">
          </el-table-column>
          <el-table-column prop="price" label="每分钟扣费标准">
            <template slot-scope="scope">
              <el-input-number v-model.number="scope.row.price" :disabled="isDisabled" :min="0" :controls="false" placeholder="请输入合同价" ></el-input-number>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
  </div>
</template>

<script>
export default {
  props: ['type', 'data'],
  data () {
    return {
      form: {
        cloud_room_up_limit: null,
        roomConfig: []
        // cloud_room_config: {}
      },
    };
  },
  mounted () {
    this.setValues()
  },
  computed: {
    isDisabled() {
      return this.type == 'setting' ? true : false
    }
  },
  methods: {
    onUpLimitChange() {
      const form = this.form
      if(form.cloud_room_up_limit > 0) {
        const count = form.cloud_room_up_limit || 0
        const roomLength = form.roomConfig.length || 0
        if(count - roomLength > 0) {
          const diff = count - roomLength
          for(let i = 1; i < diff; i++) {
            form.roomConfig.push({
              num: roomLength + 1 + i,
              price: undefined
            })
          }
        } else {
          const diff = roomLength - count + 1
          console.log(roomLength, diff, 'diff')
          form.roomConfig.splice(roomLength - diff, diff)
          this.$forceUpdate()
        }
      } else {
        form.roomConfig = []
      }

      form.roomConfig.forEach(room => {
        console.log(typeof room.price)
      });
    },
    onSubmit() {
      let status = false
      this.$refs.form.validate(_ => {
        status = _
      })
      return status
    },
    setValues() {
      if(this.data) {
        const { config } = this.data
        const tempC = config ? JSON.parse(config) : {}
        const { cloud_room_rule } = tempC
        console.log(cloud_room_rule)
        this.form.cloud_room_up_limit = cloud_room_rule.cloud_room_up_limit
        const roomCloud = cloud_room_rule.cloud_room_config
        if(!roomCloud) return
        let tempConfig = []
        for(let item in roomCloud) {
          tempConfig.push({
            num: item,
            price: roomCloud[item]
          })
        }
        this.form.roomConfig = tempConfig
      }
    },
    getValues() {
      const { roomConfig, cloud_room_up_limit } = this.form
      let cloud_room_config = {}
      roomConfig.forEach(room => {
        cloud_room_config[room.num] = room.price
      });
      return {
        cloud_room_rule: {
          cloud_room_up_limit,
          cloud_room_config
        }
      }
    },
  },
};
</script>
<style lang='scss' scoped>
/deep/.el-input-number .el-input__inner {
  text-align: left;
}
.el-input, .el-select, .el-input-number {
  width: 300px !important;
}
</style>
