<template>
  <a-modal
    :title="modalTitle"
    :width="850"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <title-divider title="设备信息" width="150px"></title-divider>
    <detail-list title="" :col="2">
      <detail-list-item term="设备名称">{{ model.sbName }}</detail-list-item>
      <detail-list-item term="设备编号">{{ model.sbNo }}</detail-list-item>
      <detail-list-item term="日期">{{ model.fillDate }}</detail-list-item>
    </detail-list>
    <template v-for="natureDict in natureList" >
      <title-divider :key="natureDict.id" :title="natureDict.label" v-show="natureDict.show" width="150px"></title-divider>
      <detail-list :key="natureDict.id" :col="2" v-if="natureList.length > 0">
        <template v-for="fillInfo in fillInfoList" v-if="fillInfo.nature === natureDict.value">
          <template v-if="fillInfo.type === DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_PROJECT_QUANTITY">
            <detail-list-item term="作业名称" :key="fillInfo.infoId">
              {{ getProjectQuantityValue(fillInfo) }}&nbsp;
            </detail-list-item>
            <detail-list-item term="作业位置" :key="fillInfo.infoId+1">
              {{ getProjectQuantityValueValue(fillInfo) }}
            </detail-list-item>
            <detail-list-item term="完成量" :key="fillInfo.infoId+2">
              {{ getProjectQuantityNumberValue(fillInfo) }}
            </detail-list-item>
            <detail-list-item term="作业备注" :key="fillInfo.infoId+3">
              {{ fillInfo.remark }}&nbsp;
            </detail-list-item>
          </template>

          <template v-else-if="fillInfo.type === DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_DATE_TIME">
            <detail-list-item term="运行开始时间" :key="fillInfo.infoId">
              {{ fillInfo.startTime }}&nbsp;
            </detail-list-item>
            <detail-list-item term="运行结束时间" :key="fillInfo.infoId+1">
              {{ fillInfo.endTime }}&nbsp;
            </detail-list-item>
            <detail-list-item term="未工作小时" :key="fillInfo.infoId+2">
              {{ fillInfo.resultEliminate }}小时
            </detail-list-item>
          </template>

          <detail-list-item v-else :key="fillInfo.infoId" :term="fillInfo.name" >
            {{ getFillValue(fillInfo) }}
          </detail-list-item>
        </template>
      </detail-list>
    </template>

    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="handleCancel()">返回</a-button>
    </template>
  </a-modal>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
const DetailListItem = DetailList.Item

export default {
  name: 'SbRunFillDetail',
  components: {
    DetailList,
    DetailListItem
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      natureList: [],
      natureShowMap: {},
      fillInfoList: [],
      projectQuantityList: [],
      selectOptionMap: {},
      projectQuantityUnitMap: {},
      radioGroupMap: {},
      // 下拉框map
      model: {
        'sbId': null,
        'sbNo': null,
        'sbName': null,
        'executeId': null,
        'insertId': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateUserName': null,
        'updateTime': null
      }
    }
  },
  watch: {
    fillInfoList (fillInfoListValue) {
      this.natureShowMap = {}
      fillInfoListValue.forEach(fillInfo => {
        this.natureShowMap[fillInfo.nature] = true
      })
      const natureList = this.DictCache.getChildrenList(this.DictCache.TYPE.FILL_INFO_NATURE)
      natureList.forEach(natureItem => {
        if (this.natureShowMap[natureItem.value]) {
          natureItem.show = true
        } else {
          natureItem.show = false
        }
      })
      this.natureList = natureList
    }
  },
  created () {
    // 下拉框map
    this.natureShowMap = {}
    // 下拉框map
    this.fillInfoList.forEach(fillInfo => {
      this.natureShowMap[fillInfo.nature] = true
    })
    const natureList = this.DictCache.getChildrenList(this.DictCache.TYPE.FILL_INFO_NATURE)
    natureList.forEach(natureItem => {
      if (this.natureShowMap[natureItem.value]) {
        natureItem.show = true
      } else {
        natureItem.show = false
      }
    })
    this.natureList = natureList
    this.projectQuantityUnitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FILL_PROJECT_QUANTITY_UNIT)
  },
  methods: {
    base (record, projectQuantityList) {
      this.visible = true
      this.modalTitle = '详情'
      this.model = record
      this.fillInfoList = record.fillInfoList
      this.projectQuantityList = projectQuantityList
    },
    getProjectQuantityValue (fillInfo) {
      let selectItem = {}

      this.BaseTool.TREE.treeFilter(this.projectQuantityList, (item) => {
        if (item.id === fillInfo.resultDesc) {
          selectItem = item
        }
        return item
      })
      return selectItem.name
    },
    getProjectQuantityValueValue (fillInfo) {
      let selectItem = {}

      this.BaseTool.TREE.treeFilter(this.projectQuantityList, (item) => {
        if (item.id === fillInfo.resultDesc) {
          selectItem = item
        }
        return item
      })
      let result = this.BaseTool.Amount.formatter(fillInfo.resultStart) + '~' + this.BaseTool.Amount.formatter(fillInfo.resultEnd)
      result = result + this.projectQuantityUnitMap[selectItem.siteUnit]
      return result
    },
    getProjectQuantityNumberValue (fillInfo) {
      let selectItem = {}

      this.BaseTool.TREE.treeFilter(this.projectQuantityList, (item) => {
        if (item.id === fillInfo.resultDesc) {
          selectItem = item
        }
        return item
      })
      let result = fillInfo.resultNumber
      result = result + this.projectQuantityUnitMap[selectItem.unit]
      return result
    },
    getFillValue (fillInfo) {
      if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_XIA_LA) {
        return this.getSelectLabel(fillInfo)
      } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_DAN_XUAN) {
        return this.getRadioGroupLabel(fillInfo)
      } else if (fillInfo.type === this.DictCache.VALUE.FILL_INFO_TYPE.FILL_INFO_TYPE_FAN_WEI) {
        let result = this.BaseTool.Amount.formatter(fillInfo.resultStart) + '~' + this.BaseTool.Amount.formatter(fillInfo.resultEnd) +
          '(' + this.BaseTool.Amount.formatter(fillInfo.resultEliminate) + ')'
        if (fillInfo.codeValue === this.DictCache.VALUE.FILL_INFO_CODE_VALUE.FILL_INFO_CODE_VALUE_GLS) {
          result = result + 'KM'
        } else if (fillInfo.codeValue === this.DictCache.VALUE.FILL_INFO_CODE_VALUE.FILL_INFO_CODE_VALUE_SJS) {
          result = result + '小时'
        }
        return result
      } else {
        return fillInfo.resultDesc
      }
    },
    getSelectLabel (fillInfo) {
      const options = this.getSelectOptions(fillInfo)
      return options[fillInfo.resultValue]
    },
    getSelectOptions (fillInfo) {
      if (this.BaseTool.Object.isBlank(this.selectOptionMap[fillInfo.dictType])) {
        this.selectOptionMap[fillInfo.dictType] = this.DictCache.getLabelByValueMapByType(fillInfo.dictType)
      }
      return this.selectOptionMap[fillInfo.dictType]
    },
    getRadioGroup (fillInfo) {
      if (this.BaseTool.Object.isBlank(this.radioGroupMap[fillInfo.dictType])) {
        this.radioGroupMap[fillInfo.dictType] = this.DictCache.getLabelByValueMapByType(fillInfo.dictType)
      }
      return this.radioGroupMap[fillInfo.dictType]
    },
    getRadioGroupLabel (fillInfo) {
      const options = this.getRadioGroup(fillInfo)
      return options[fillInfo.resultValue]
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    }

  }
}
</script>
