<template>
  <!-- 文件名称下拉选择框 -->
  <div>
    <base-select v-model="currentValue"
               :clearable="clearable"
               :filterable="filterable "
               :multiple="multiple"
               :options.sync="options"
               :queryParas="queryParas"
               :collapsetags="collapsetags"
               :disabled="disabled"
               :reload="reload"
               :selectedField="selectedField"
               @loaded="loaded"
               :placeholder="placeholder"
               @select-changed="selectChanged"
               no-data-text="数据加载中或加载失败">
    </base-select>
  </div>
</template>

<script>
import effectDealFeedBackApi from '@/api/assetoutfeedback/effectDealFeedBack/effectDealFeedBackApi'
import components from 'newepoch-components'
export default {
  name: 'fileNameList',
  components: {
    BaseSelect: components.BaseSelect
  },
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Number],
      default: function () {
        return null
      }
    },
    // 是否禁用修改
    disabled: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可多选
    multiple: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 重新加载数据（每次取非即可）
    reload: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 是否可搜索
    filterable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 是否可清除
    clearable: {
      type: Boolean,
      default: function () {
        return true
      }
    },
    // 多选折叠
    collapsetags: {
      type: Boolean,
      default: function () {
        return false
      }
    },
    // 默认提示语
    placeholder: {
      type: String,
      default: function () {
        return '请选择'
      }
    },
    name: {
      type: String,
      default: 'file',
      require: false
    },
    // 列表请求参数
    queryParas: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      // 下拉列表数据
      'options': [
        // {
        //   fileCode: '1',
        //   fileName: '2',
        //   targetType: '3',
        //   UnsealedFileId: '4'
        // }
      ],
      // 自定义键名
      selectedField: ['fileCode', 'fileName']
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    assetApi () {
      return effectDealFeedBackApi.getAssetFile
    },
    porApi () {
      return effectDealFeedBackApi.getProFiles
    }
  },
  mounted () {
    this.assetApi.getDefaultInfo({ assetInfoId: this.queryParas.assetInfoId }).then(res => {
      if (res.data.length > 0) {
        let resq = res.data[0]
        this.porApi.getList({ materialGroupId: resq.materialGroupId, bussNeedType: '5' }).then(ress => {
          if (ress.data.length > 0) {
            for (let index in ress.data) {
              let resqp = ress.data[index]
              let obj = {}
              if (resqp.projectMaterialFileDTO) {
                let projectMaterialFileDTO = resqp.projectMaterialFileDTO
                // 电子章
                if (this.queryParas.editModel === 'FIS') {
                  if (this.queryParas.stampWay140 === this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.elecSeal && projectMaterialFileDTO.fileSource === '1') {
                    return
                  }
                  if (this.queryParas.stampWay150 === this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.elecSeal && projectMaterialFileDTO.fileSource === '3') {
                    return
                  }
                  obj.fileCode = projectMaterialFileDTO.fileCode
                  obj.fileName = projectMaterialFileDTO.fileName
                  obj.targetType = projectMaterialFileDTO.fileSource
                  obj.UnsealedFileId = resq.assetPoolMaterialGroupDetails[0].originalFileId
                  this.options.push(obj)
                // 鲜章
                } else {
                  obj.fileCode = projectMaterialFileDTO.fileCode
                  obj.fileName = projectMaterialFileDTO.fileName
                  obj.targetType = projectMaterialFileDTO.fileSource
                  obj.UnsealedFileId = resq.assetPoolMaterialGroupDetails[0].originalFileId
                  this.options.push(obj)
                }
              }
            }
          }
        })
      }
    })
  },
  methods: {
    selectChanged (val, row) {
      this.$emit('selectChanged', val, this.name, row)
    },
    loaded (val) {
      let $this = this
      let selectItemts = null
      if (val.length > 0) {
        if (!this.multiple) {
          selectItemts = val.find(it => {
            return $this.currentValue === it[$this.selectedField[0]]
          })
        } else {
          selectItemts = val.filter(it => {
            var isTrue = false
            $this.currentValue.forEach(kit => {
              if (kit === it[$this.selectedField[0]]) {
                isTrue = true
              }
            })
            return isTrue
          })
        }
        this.$emit('selectChanged', '', this.name, selectItemts)
      }
    }
  }
}
</script>
