<template>
  <div>
    <Input
      v-model="inputValue"
      type="text"
      @on-enter="handleInputEnter"
      @on-change="handleInputChange"
      clearable
    />
    <Modal
      v-model="matrixModal"
      title="矩阵导入"
      width="900"
      :mask="true"
      :mask-closable="true"
      :closable="true"
      :footer-hide="true"
      @on-cancel="handleModalCancel"
    >
      <matrix 
        v-if="matrixModal" 
        :productId="productId"
        :productCode="inputValue"
        :options="matrixOption" 
        @closeMatrix="handleCloseMatrix"
        @refreshData="handleRefreshMainData"
      />
    </Modal>
  </div>
</template>

<script>
  import matrix from './inputMatrixPop.vue';
  const R3 = window.R3;
  const { network, urlSearchParams, store } = R3;
  export default {
    components: {
      matrix
    },
    props: {
      options: {
        type: Object,
        default: () => {}
      }
    },
    data() {
      return {
        inputValue: '',
        productId: '',
        matrixModal: false,
        matrixOption: {}
      }
    },
    created() {
    },
    mounted() {
      window.__drpYunqueTextMatrixEnterFun = () => {
        this.handleInputEnter()
      }
    },
    beforeDestroy() {
      window.__drpYunqueTextMatrixEnterFun = null
    },
    methods: {
      handleInputChange(e) {
        this.$emit('change', e.target.value)
      },
      handleInputEnter() {
        const {moduleComponentName,formName,tableName} = this.options
        const {saveInfo} = this.$store.state[moduleComponentName]
        const {formData,formItemLists} = document.getElementById(formName)._vue_
        const mainTableName = this.$route.params.tableName
        const itemId = this.$route.params.itemId
        
        let isSendRequest = true
        let notNullMessage = ''
        let formKey = []
        const formItemData = {...formData, ID: -1}
        const formItem = formItemLists[0].childs
        for (let key in formItem) {
          const item = formItem[key]
          if (item.display !== 'none' && item.isnotnull) {
            formKey.push(item.colname)
          }
          if (item.display !== 'none' && item.isnotnull && !formItemData[item.colname]) {
            isSendRequest = false
            notNullMessage = `${item.coldesc}不能为空！`
            break
          }
        }
        // 矩阵保存配置
        const matirxSaveOptions = {
          url: saveInfo.paths,
          mainTableName,
          tableName,
          mainTableValue: {ID: itemId},
          formDataKey: formKey
        }
        if (isSendRequest) {
          let params = {}
          params[mainTableName] = {ID: itemId}
          params[tableName] = [formItemData]
          this.apiInputScan(saveInfo.paths, params, matirxSaveOptions)
        } else {
          this.$Message.warning(notNullMessage)
        }
      },
      async apiInputScan(url, params, options) {
        try {
          const { data } = await network.post(url, params)
          if (data.code === 0) {
            // productId存在打开矩阵弹框，不存在清空数据调用刷新
            if (data.data && data.data.productId) {
              this.productId = data.data.productId
              this.matrixOption = {
                ...options
              }
              this.matrixModal = true
            } 
            else {
              this.inputValue = ''
              window.__drpYunqueMatrixRefresh()
            }
          }
          
        } catch {}
      },
      handleModalCancel() {

      },
      handleRefreshMainData() {
        this.inputValue = ''
        this.matrixModal = false
        window.__drpYunqueMatrixRefresh()
      },
      handleCloseMatrix() {
        this.matrixModal = false
      }
    }
  };
</script>
<style lang='less'>
  
</style>