<template>
  <div>
    <template>
      <el-dialog title="回收站" :close-on-click-modal="false" :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll append-to-body>
        <el-table v-loading="listLoading" :data="tableData" border :height="550" @selection-change="handleSelectionChange" style="width: 100%">
          <el-table-column type="selection" width="55" fixed> </el-table-column>
          <el-table-column label="序号" align="center" width="50" fixed>
            <template slot-scope="scope">
              {{ scope.$index + 1 + size * (current - 1) }}
            </template>
          </el-table-column>
          <el-table-column prop="operator" label="操作人" align="center" fixed></el-table-column>
          <el-table-column prop="createTime" label="删除时间" align="center" fixed></el-table-column>
          <template v-for="item in config">
            <el-table-column v-if="item.isShow" :label="item.label" align="center">
              <template slot-scope="scope">
                <!--                如果有值-->
                <div v-if="editCurrentClick(scope.row, item)">
                  <!--                  判断类型-->
                  <div v-if="item.jnpfKey === 'numInput'">
                    {{ editCurrentClick(scope.row, item) === 0 ? '0' : editCurrentClick(scope.row, item) }}
                  </div>
                  <div v-else-if="item.jnpfKey === 'uploadImg' || item.jnpfKey === 'uploadFz'">
                    <template v-if="item.jnpfKey === 'uploadFz'">
                      <span style="color: #1890ff" v-for="(i, k) in getImgFun(editCurrentClick(scope.row, item))" :key="k">{{ i.name }}</span>
                    </template>
                    <template v-if="item.jnpfKey === 'uploadImg'">
                      <template v-for="(imgEle, imgKey) in getImgFun(editCurrentClick(scope.row, item))">
                        <el-image v-if="imgKey < 3" :key="imgKey" style="width: 50px; height: 50px; margin-right: 10px" :src="getImgUrl() + imgEle.url"> </el-image>
                      </template>
                      <el-tag style="vertical-align: text-bottom" v-if="getImgFun(editCurrentClick(scope.row, item)).length > 3" type="info">
                        + {{ getImgFun(editCurrentClick(scope.row, item)).length - 3 }}</el-tag
                      >
                    </template>
                  </div>
                  <div v-else-if="item.jnpfKey === 'radio' || item.jnpfKey === 'select' || item.jnpfKey === 'checkbox'">
                    {{ getOptionData(editCurrentClick(scope.row, item), item.propId) }}
                  </div>
                  <div v-else-if="item.jnpfKey === 'date' || item.jnpfKey === 'datetime'">
                    {{ getSetData(editCurrentClick(scope.row, item), item.propId, 'format') }}
                  </div>
                  <div v-else>
                    {{ editCurrentClick(scope.row, item) }}
                  </div>
                </div>
              </template>
            </el-table-column>
          </template>
          <el-table-column label="操作" align="center" width="100" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="recycleCurrentClick(scope.row)">详情</el-button>
              <el-button type="text" size="small" @click="recycleClick(scope.row)">恢复</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin-top: 9px; float: left">
          <el-button type="primary" size="mini" @click="toggleSelection()">批量恢复</el-button>
          <el-button v-if="assemblyOptions.includes('comDelete')" type="primary" size="mini" @click="toggleSelectionDelete()">彻底删除</el-button>
        </div>
        <template>
          <pagination :total="total" :page.sync="current" :limit.sync="size" @pagination="init()" />
        </template>
        <el-dialog class="JNPF-dialog JNPF-dialog_center" title="详细信息" :visible.sync="innerVisible" append-to-body>
          <el-table :data="innerData" border :height="500" style="width: 100%">
            <template v-for="(item, index) in innerList">
              <el-table-column v-if="item.value" :label="item.label" align="center">
                <template slot-scope="scope">
                  {{ item.value }}
                </template>
              </el-table-column>
            </template>
          </el-table>
        </el-dialog>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import { recycleModelDelete, recycleModelList, recycleModelrecovery } from '@/api/onlineDev/visualDev'
import moment from 'moment/moment'
export default {
  name: 'recycleList',
  props: ['modelId', 'assemblyOptions', 'config', 'formData'],
  data() {
    return {
      visible: false,
      innerVisible: false,
      listLoading: false,
      tableData: [],
      current: 1,
      size: 10,
      total: 0,
      SelectionList: [],
      innerData: [],
      innerList: []
    }
  },
  methods: {
    //初始化数据
    init() {
      this.visible = true
      this.listLoading = true
      let parmes = {
        current: this.current,
        size: this.size,
        modelId: this.modelId
      }
      recycleModelList(parmes).then((res) => {
        this.tableData = res.data.data.records
        this.total = res.data.data.total
        this.listLoading = false
      })
    },
    //多选
    handleSelectionChange(val) {
      if (val && val.length) {
        this.SelectionList = val.map((i) => {
          return i.id
        })
      } else {
        this.SelectionList = []
      }
    },
    //批量恢复
    toggleSelection() {
      if (!this.SelectionList.length) return this.$message({ message: '至少选择一条数据', type: 'warning' })

      recycleModelrecovery(this.SelectionList).then((res) => {
        if (res.data.code === 200) {
          this.$message({ message: '恢复成功', type: 'success' })
          this.init()
          this.$emit('recycleDataList')
        }
      })
    },
    toggleSelectionDelete() {
      if (!this.SelectionList.length) return this.$message({ message: '至少选择一条数据', type: 'warning' })

      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          recycleModelDelete(this.SelectionList).then((res) => {
            if (res.data.code === 200) {
              this.$message({ message: '操作成功', type: 'success' })
              this.init()
            }
          })
        })
        .catch(() => {})
    },
    //恢复
    recycleClick(row) {
      recycleModelrecovery([row.id]).then((res) => {
        if (res.data.code === 200) {
          this.$message({ message: '恢复成功', type: 'success' })
          this.init()
          this.$emit('recycleDataList')
        }
      })
    },
    //行点击事件
    recycleCurrentClick(val) {
      this.innerData = []
      if (val.dataView) {
        try {
          const toObj = JSON.parse(val.dataView)
          if (Array.isArray(toObj) && toObj.length) {
            this.innerList = toObj
            this.innerData.unshift({})
            this.innerVisible = true
          }
        } catch {
          if (Array.isArray(val.dataView) && val.dataView.length) {
            this.innerData.unshift({})
            this.innerList = val.dataView
          }
        }
      }
    },
    //行点击事件
    editCurrentClick(val, row) {
      if (val.dataView) {
        try {
          const toObj = JSON.parse(val.dataView)
          if (Array.isArray(toObj) && toObj.length) {
            let aa = toObj.find((o) => o.fieldId === row.propId)
            return aa.value
          }
        } catch {
          if (Array.isArray(val.dataView) && val.dataView.length) {
            let aa = val.dataView.find((o) => o.fieldId === row.propId)
            return aa.value
          }
        }
      }
    },
    //下拉属性的组件  如果key和value值不相同 查询title
    getOptionData(v, id) {
      let list = []
      let str = v
      this.formData.fields.forEach((item) => {
        if (item.__vModel__ === id) {
          list = item.__slot__.options
        }
      })
      if (list.length) {
        let obj = list.find((i) => i.id == v)
        if (obj) str = obj.fullName
      }
      return str
    },
    //图片类型处理
    getImgFun(value) {
      try {
        const toObj = JSON.parse(value)
        if (Array.isArray(toObj) && toObj[0].url) {
          return toObj
        }
      } catch {}
      return value
    },
    getImgUrl() {
      let ImgUrl
      if (process.env.NODE_ENV === 'development') {
        ImgUrl = this.define.comUrl
      } else {
        ImgUrl = document.location.origin
      }
      return ImgUrl + '/'
    },
    getSetData(val, propId, type) {
      let str = this.getFormat(propId, type)
      if (str === 'yyyy-MM-dd') {
        str = 'yyyy-MM-DD'
      } else if (str === 'yyyy-MM-dd HH:mm:ss') {
        str = 'yyyy-MM-DD HH:mm:ss'
      }
      return moment(val).format(str)
    },
    //获取时间值类型
    getFormat(id, type) {
      let str
      this.formData.fields.forEach((item) => {
        if (item.__vModel__ === id) {
          if (type === 'format') {
            str = item.format
          }
        }
      })
      return str
    }
  }
}
</script>

<style lang="scss" scoped>
.JNPF-dialog_center {
  >>> .el-dialog__body {
    padding: 20px !important;
  }
}
::v-deep .el-table {
  .el-table__fixed {
    // 左固定列
    height: auto !important;
    bottom: 18px; // 改为自动高度后，设置与父容器的底部距离，高度会动态改变，值可以设置比滚动条的高度稍微大一些
  }
  .el-table__fixed-right {
    // 右固定列
    height: auto !important;
    bottom: 18px; // 改为自动高度后，设置与父容器的底部距离，高度会动态改变，值可以设置比滚动条的高度稍微大一些
  }
}
</style>
