<template>
  <el-dialog title="三维文件" :visible.sync="visible" width="800px" append-to-body custom-class="custom-modal">
    <div class="app-container">
      <div class="filter-container">
        <el-input
          v-model="listQuery.keyWord"
          placeholder="关键字"
          style="width: 200px"
          class="filter-item"
          :maxlength="30"
          @keyup.enter.native="handleFilter"
        />
        <el-button
          v-waves
          style="margin-left: 40px"
          class="filter-item"
          type="primary"
          @click="handleFilter"
        >
          查询
        </el-button>
        <el-button v-waves class="filter-item" style="margin-left: 10px" type="primary" @click="handleCreate">
          上传
        </el-button>
      </div>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        :max-height="tableHeight"
      >
        <el-table-column label="序号" align="center" width="80" type="index">
          <template scope="scope">
            <span>{{ (listQuery.pageNum - 1) * 10 + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="文件名称" align="center" prop="name" />
        <el-table-column label="上传日期" align="center" prop="time" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
          <template slot-scope="{ row, $index }">
            <el-button v-waves type="text" size="mini" @click="handleView(row, $index)">查看</el-button>
            <el-button v-waves type="text" size="mini" @click="handleDelete(row, $index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.pageNum"
        :limit.sync="listQuery.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog append-to-body :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="600px" @closed="handleClosedModal">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="120px"
      >
        <el-form-item label="附件：" prop="addition">
          <el-upload
            ref="uploadRef"
            :on-change="handleChangeFile"
            :on-remove="handleRemoveFile"
            action="#"
            :auto-upload="false"
            :file-list="fileList">

            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-waves @click="dialogFormVisible = false"> 关闭 </el-button>
        <el-button v-waves v-throttle-click="createData" type="primary">
          提交
        </el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

import _ from 'lodash/fp'
import { add3dFileItem, delete3dFileItem, get3dFileList, update3dFileItem } from '@/api/basic/3dFile'

const defaultTmp = {
  gxId: ''
}
export default {
  name: '3dFileTable',
  components: { Pagination },
  directives: { waves },
  props: ['currentId', 'componentVisible'],
  data() {
    return {
      tableKey: 0,
      list: [],
      fileList: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        keyword: ''
      },
      tableHeight: 650,
      temp: _.cloneDeep(defaultTmp),
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '上传'
      },
      rules: {
        gxName: [{ required: false, message: '请选择', trigger: 'change' }]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.componentVisible
      },
      set(val) {
        this.$emit('update:componentVisible', val)
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList()
      }
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery.id = this.currentId
      get3dFileList(this.listQuery).then((response) => {
        this.list = response.rows
        this.total = response.total
      }).finally(() => {
        this.listLoading = false
      })
    },
    // 查询
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    // 新增
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      const reqData = new FormData()
      this.fileList.forEach(item => {
        if (item.hasOwnProperty('raw')) {
          reqData.append('file[]', item.raw)
        }
      })
      add3dFileItem(reqData).then((response) => {
        this.$message({
          showClose: true,
          message: response.msg,
          type: 'success',
          duration: 2000
        })
        this.getList()
        this.dialogFormVisible = false
      })
    },
    // 删除数据
    handleDelete(row, index) {
      this.$confirm(`确定要删除该数据吗？`, {
        type: 'warning',
        title: '操作提示',
        beforeClose: async(action, instance, done) => {
          if (action === 'confirm') {
            delete3dFileItem({ id: row.id }).then((response) => {
              done()
              this.$message({
                showClose: true,
                message: response.msg,
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
          } else {
            done()
          }
        }
      })
    },
    handleClosedModal() {
      this.$refs['dataForm'].resetFields()
      this.temp = _.cloneDeep(defaultTmp)
      this.fileList = []
    },
    handleChangeFile(file, files) {
      this.fileList = files
    },
    handleRemoveFile(file, files) {
      this.fileList = files
    },
    handleView(row) {

    }
  }
}
</script>
