<template>
  <div>
    <Modal  v-if="isDrawer" v-model="drawerShow" width="50%" :mask-closable="false" title="查询" class="modal-style">
        <search-filter v-if="templateName" class="gms-query" :templateName="templateName" :extrCondition="extrCondition"></search-filter>
        <div slot="footer"></div>
    </Modal>
  </div>
</template>

<script>
import SearchFilter from './searchQueryFilter/query-filter.vue'
export default {
  name: 'CustomerSelect',
  props: {
    context: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      bill: {},
      drawerShow: false,
      templateName: '',
      queryData: [],
      tableName: '',
      isSub: true,
      field: {},
      baseDataFields: [],
      extrCondition: [],
      isDrawer: false
    }
  },
  components: {
    SearchFilter
  },
  mounted() {
    GMS.vbus.$on('onSelectionChange', (selection) => {
      this.queryData = [...selection]
    })
    GMS.vbus.$on('onCurrentChange', (currentRow) => {
      this.queryData = [currentRow]
    })
  },

  methods: {
    handleConfirm() {
      if (!this.tableName) {
        this.$message.error('操作表名称为空')
        return
      }
      if (this.isSub) {
        // this.queryData = [
        //   { x: 1, y: 2 },
        //   { x: 3, y: 4 },
        // ]
        this.queryData.forEach((item, index) => {
          this.setSubTableData(item, index)
        })
        // 刷新
        this.bill.refreshSubDataRow(this.tableName)
      } else {
        this.setMasterData()
      }
      
      this.drawerShow = false
    },
    setSubTableData(sourceRow, index) {
      // 增行
      this.bill.addSubData(this.tableName)
      //赋值
      const subDataObj = this.bill.getSubData(this.tableName)
      const tableLength = subDataObj.length || 0
      const subDataIndex = tableLength  - 1
      const subRowData = this.getRowData(sourceRow)
      if (Array.isArray(subDataObj)) {
        for (const key in subRowData) {
          if (this.baseDataFields&&this.baseDataFields.includes(key)) {
            const idKey = this.field[key] + 'id'
            const baseDataValue = { id: sourceRow[idKey], name: subRowData[key].value }
            console.log('baseDataValue', baseDataValue)
            subDataObj[subDataIndex].setValue(key, baseDataValue)
          } else {
            subDataObj[subDataIndex].setValue(key, subRowData[key].value)
          }
        }
      }
    },
    getRowData(sourceRow) {
      let subField = this.getSubField()
      //  this.field = {
      //   a1: 'x',
      //   b1: 'y',
      // }
      console.log('field', this.field)
      for (let key in this.field) {
        if(key !== 'baseDataFields' && key !== 'filterField') {
          subField[key].value = sourceRow[this.field[key]]
          subField[key].readOnly = subField.hasOwnProperty(key)
        }
      }
      return subField
    },
    setMasterData() {
      let masterName = this.tableName.split('.')[1]
      let keys = Object.keys(this.field)
      keys.forEach((key) => {
        let value = ''
        let str = masterName + '.' + key
        for (let k in this.queryData[0]) {
          if (k === this.field[key]) {
            value = this.queryData[0][this.field[key]]
          }
        }
        this.bill.getMasterData().setValue(str, value)
      })
    },
    getSubField() {
      let fieldObj = {}
      if (this.bill.getAllSubFiledByName(this.tableName)) {
        fieldObj = JSON.parse(JSON.stringify(this.bill.getAllSubFiledByName(this.tableName)))
      } else {
        this.$message.error('子表字段获取失败')
      }
      return fieldObj
    },
  },
}
</script>

<style lang="less" scoped>
.gms-query{
    height: 100%;
}
.gms-query.customQueryBody {
//   padding-top: 40px;
  /deep/ .tabChangeBody {
      .spaceLine {
        height: 0;
        margin-top: 0;
      }
    }  
}
.modal-style{
  min-height: 80%;
  max-height: 90%;
}
</style>