<template>
  <el-form :model="form" label-position="right">
    <div class="content">
      <el-form-item
        class="content-item"
        :style="{ width: cell_width }"
        v-for="(field, index) in form.field_list"
        :key="index"
        :required="!field.is_null_space"
        :label="field.field_description"
        :label-width="formLabelWidth"
      >
        <FormFieldValue
          :key_id="field.key_id"
          :field_value="field.field_value"
          :field_type="field.web_ui_control_type"
          :fk_table_name="field.fk_table_name"
          :fk_table_name_field="field.fk_table_name_field"
          :fk_table_display_field_name="field.fk_table_display_field_name"
          :disabled="field.is_detail_read_only"
          @changeKey="changFieldValue"
        ></FormFieldValue>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { mapState, mapActions, mapGetters } from 'vuex';
import system from '../../api/system.js';
export default {
  name: 'form-table-new',
  props: {
    table_name: {
      type: String,
      required: true
    },
    col_num: {
      type: Number,
      default: 2
    }
  },
  components: {},
  data() {
    return {
      cell_width: '50%',
      formLabelWidth: '120px',
      form: {
        field_list: []
      },
      // 变化的字段列表
      change_field_list: []
    };
  },
  computed: {
    // 表对于的后台的模型名称
    ...mapGetters('global', ['getOneTableInfoByTableName', 'flameTableInfoItems', 'getOneTableInfoItemByTableName']),
    commbox_where_values: function() {
      let that = this;
      let array = [];
      return array;
    }
  },
  mounted() {
    this.getField();
    this.calcWidth();
  },

  // 监控ID 是否变化，如果变化之后重新取新的数据
  watch: {
    
    flameTableInfoItems(val) {
      this.getField();
    }
  },
  methods: {
    closeDialog(value) {
      let that = this;
      that.is_show = false;
      that.$emit('dialogClose', false);
    },
    // 确定对话框
    confirmDialog() {
      let that = this;
      if (that.change_field_list.length > 0) {
        that.change_field_list = that.form.field_list.filter(t => t.is_change === true);
        let commit_data = {};
        that.change_field_list.forEach(e => {
          let value = e.field_value;
          commit_data[e.field_name] = e.field_value;
        });

        system.singleAdd(that.table_name, commit_data, res => {
          if (res !== null) {
            if (res.result === true) {
              that.clear();
              that.$emit('dialogClose', true);
            }
          }
        });
      } else {
        that.$emit('dialogClose', false);
      }
    },
    // 清出数据
    clear() {
      let that = this;
      that.form.field_list.forEach(e => {
        e.field_value = null;
        e.is_change = false;
      });
      that.change_field_list=[];
    },
    calcWidth() {
      let that = this;
      let cell_width = 100 / that.col_num;
      that.cell_width = cell_width + '%';
    },
    getField() {
      let that = this;
      let temp = that.getOneTableInfoItemByTableName(that.table_name);
      // let table_info = that.getOneTableInfoByTableName(that.table_name);
      that.form.field_list = [];
      temp.forEach(e => {
        if (e.is_display_detail === true && e.is_detail_read_only === false) {
          that.form.field_list.push({
            key_id: e.flame_id,
            field_name: e.field_name,
            field_description: e.field_description,
            web_ui_control_type: e.web_ui_control_type,
            field_color_der: e.field_color_der,
            is_fk: e.is_fk,
            is_detail_read_only: e.is_detail_read_only,
            is_null_space: e.is_null_space,
            fk_table_name: e.fk_table_name,
            fk_table_name_field:e.fk_table_name_field,
            fk_table_display_field_name: e.fk_table_display_field_name,
            fk_table_ref_display_fields: e.fk_table_ref_display_fields,
            fk_sql: e.fk_sql,
            field_value: '',
            is_change: false
          });
        }
      });
    },
    //  数据发生变化时触发
    changFieldValue(key_id, value) {
      let that = this;
      // 如果是bool 类型要处理一下
      if (value === 'true') {
        value = true;
      } else if (value === 'false') {
        value = false;
      }
      
      that.form.field_list.forEach(e => {
        if (e.key_id === key_id) {
          e.field_value = value;
          e.is_change = true;
        }
      });
      that.change_field_list = that.form.field_list.filter(t => t.is_change === true);
      let change_field = that.form.field_list.find(t => t.key_id === key_id);
      console.log('change_field_list:', that.change_field_list, change_field);
      that.$emit('changeKey', that.change_field);
    }
  }
};
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 2px;
}
.content {
  overflow-y: auto;
  max-height: 400px;
  display: flex;
  flex-wrap: wrap;
  .content-item {
    width: 33%;
  }
}
</style>
