<template>
  <div>
    <Drawer v-if="isDrawer" v-model="drawerShow" width="900" :mask-closable="false">
      <gms-query v-if="templateName" class="gms-query" :templateName="templateName" :extrCondition="extrCondition"></gms-query>
      <div class="drawer-footer">
        <Button style="margin-right: 8px" @click="drawerShow = false">取消</Button>
        <Button type="primary" :disabled="queryData.length === 0" @click="handleConfirm">确定</Button>
      </div>
    </Drawer>
  </div>
</template>

<script>
export default {
  name: 'CustomerSelect',
  props: {
    context: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      bill: {},
      drawerShow: false,
      templateName: '',
      queryData: [],
      tableName: '',
      isSub: true,
      field: {},
      baseDataFields: [],
      extrCondition: [],
      isDrawer: false,
      actionParam: null
    }
  },
  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)
      })
      this.bill.runFormulaCheckData(this.actionParam, function(msg){})
    },
    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.customQueryBody {
  padding-top: 40px;
  /deep/ .tabChangeBody {
    overflow: scroll;
      .spaceLine {
        height: 0;
        margin-top: 0;
      }
      .seniorQueryBody{
        margin-top: 30px;
      }
      .ivu-select-dropdown {
        top: -110px !important;
        left: -500px !important;
        }
    }  
}
.drawer-footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  background: #fff;
}
</style>