<template>
  <a-modal
    :title="modalTitle"
    :width="1100"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <fill-form :ref="model.sbId" :sb-info="model" :project-quantity-list="projectQuantityList" :fill-info-list="model.fillInfoList" />
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'
import FillForm from '@/views/sb/run-fill/modules/FillForm'
import { updateSbRunFill } from '@/api/sb/run-fill'
import { updateSbInspectionFill } from '@/api/sb/inspection-fill'
import { getFillProjectQuantityTree } from '@/api/fill/project-quantity'

export default {
  name: 'BaseSbRunFill',
  components: {
    SbInfoSelectModal,
    FillForm
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      model: {},
      projectQuantityList: [],
      visible: false
      // 下拉框map
    }
  },
  props: {
    useType: {
      type: Number,
      default: null
    }
  },
  created () {
    // 下拉框map
    getFillProjectQuantityTree({ filter: 1 }).then(res => {
      this.projectQuantityList = res.data
    })
  },
  methods: {
    base (record) {
      this.model = record
      this.visible = true
      this.modalTitle = '编辑'
    },
    save () {
      this.confirmLoading = true
      const validateFieldsAndScroll = this.$refs[this.model.sbId].form.validateFieldsAndScroll
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        const fill = {
          id: this.model.id,
          fillDate: values.fillDate.format(this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        }
        fill.fillInfoList = this.fillInfoSaveValue(this.model, values)
        if (this.DictCache.VALUE.FILL_INFO_USE_TYPE.FILL_INFO_USE_TYPE_YUN_XING === this.useType) {
          updateSbRunFill(fill).then(() => {
            this.handleCancel(fill)
          }).catch(() => {
            this.confirmLoading = false
          })
        } else if (this.DictCache.VALUE.FILL_INFO_USE_TYPE.FILL_INFO_USE_TYPE_XUN_JIAN === this.useType) {
          updateSbInspectionFill(fill).then(() => {
            this.handleCancel(fill)
          }).catch(() => {
            this.confirmLoading = false
          })
        }
      })
    },
    fillInfoSaveValue (sbInfo, values) {
      const fillInfoList = sbInfo.fillInfoList
      const fillInfoMap = {}
      fillInfoList.forEach(item => {
        item.useType = this.useType
        fillInfoMap[item.infoId] = item
      })
      Object.keys(values).forEach(infoId => {
        const startIndex = infoId.indexOf('Start')
        const endIndex = infoId.indexOf('End')
        const eliminateIndex = infoId.indexOf('Eliminate')
        const projectStartIndex = infoId.indexOf('ProjectQuantityS')
        const projectEndIndex = infoId.indexOf('ProjectQuantityE')
        const projectEliminateIndex = infoId.indexOf('ProjectQuantityT')
        const projectResultNumberIndex = infoId.indexOf('ProjectQuantityV')
        const remarkProjectQuantityIndex = infoId.indexOf('RemarkProjectQuantity')

        const startTimeIndex = infoId.indexOf('TimeS')
        const endTimeIndex = infoId.indexOf('TimeE')
        const eliminateTimeIndex = infoId.indexOf('TimeR')

        if (startIndex > 0) {
          const realInfoId = infoId.substring(0, startIndex)
          fillInfoMap[realInfoId].resultStart = values[infoId]
        } else if (endIndex > 0) {
          const realInfoId = infoId.substring(0, endIndex)
          fillInfoMap[realInfoId].resultEnd = values[infoId]
        } else if (eliminateIndex > 0) {
          const realInfoId = infoId.substring(0, eliminateIndex)
          fillInfoMap[realInfoId].resultEliminate = values[infoId]
        } else if (startTimeIndex > 0) {
          const realInfoId = infoId.substring(0, startTimeIndex)
          fillInfoMap[realInfoId].startTime = values[infoId].format(this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        } else if (endTimeIndex > 0) {
          const realInfoId = infoId.substring(0, endTimeIndex)
          fillInfoMap[realInfoId].endTime = values[infoId].format(this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        } else if (eliminateTimeIndex > 0) {
          const realInfoId = infoId.substring(0, eliminateTimeIndex)
          fillInfoMap[realInfoId].resultEliminate = values[infoId]
        } else if (projectStartIndex > 0) {
          const realInfoId = infoId.substring(0, projectStartIndex)
          fillInfoMap[realInfoId].resultStart = values[infoId]
        } else if (projectEndIndex > 0) {
          const realInfoId = infoId.substring(0, projectEndIndex)
          fillInfoMap[realInfoId].resultEnd = values[infoId]
        } else if (projectEliminateIndex > 0) {
          const realInfoId = infoId.substring(0, projectEliminateIndex)
          fillInfoMap[realInfoId].resultEliminate = values[infoId]
        } else if (projectResultNumberIndex > 0) {
          const realInfoId = infoId.substring(0, projectResultNumberIndex)
          fillInfoMap[realInfoId].resultNumber = values[infoId]
        } else if (remarkProjectQuantityIndex > 0) {
          const realInfoId = infoId.substring(0, remarkProjectQuantityIndex)
          fillInfoMap[realInfoId].remark = values[infoId]
        } else {
          const fillInfo = fillInfoMap[infoId]
          if (fillInfo != null) {
            if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_INT) {
              fillInfoMap[infoId].resultValue = values[infoId]
            } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_DECIMAL) {
              fillInfoMap[infoId].resultNumber = values[infoId]
            } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_STRING) {
              fillInfoMap[infoId].resultDesc = values[infoId]
            } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_XIA_LA) {
              fillInfoMap[infoId].resultValue = values[infoId]
            } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_DAN_XUAN) {
              fillInfoMap[infoId].resultValue = values[infoId]
            } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_PROJECT_QUANTITY) {
              console.log(555555, values[infoId])
              fillInfoMap[infoId].resultDesc = values[infoId]
            } else {

            }
          }
        }
      })
      const resultInfoList = []
      Object.values(fillInfoMap).forEach(fillInfo => {
        resultInfoList.push(fillInfo)
      })
      return resultInfoList
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base({})
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.$refs[this.model.sbId].form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
