<template>
  <div class="view-table-content">
    <div style="width: 100%">
      <div class="view-dataType-title">
        <div class="view-dataType-title-btn">
          <el-button type="success" @click="openDialog()">上传文件</el-button>
          <el-button type="warning" @click="remove(tableCheckItems)" :disabled="tableCheckItems.length == 0">删除</el-button>
        </div>
        <div class="view-dataType-title-search">
          <el-input placeholder="请输入文件名称" v-model="keyWordData" class="input1">
            <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
          </el-input>
        </div>
      </div>
      <div class="view-dataType-table">
        <LTable ref="table" @selection-change="selection" :defaultFetch="false" :fetch="fetch" :columns="columns" :dataSource="tableData" :options="options" :pagination="tableRequset"></LTable>
      </div>
      <!-- 添加或修改模型信息对话框 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="文件名称" prop="fileName">
            <el-input v-model="form.fileName" placeholder="请输入文件名称" />
          </el-form-item>
          <el-form-item label="文件类型" prop="fileType">
            <el-select v-model="form.fileType" placeholder="请选择文件类型">
              <el-option v-for="item in fileTypeList" :key="item.key" :label="item.name" :value="item.key" />
            </el-select>
          </el-form-item>
          <el-form-item label="选择文件" prop="ossId">
            <FileUpload v-model="form.ossId" :limit="1" :fileSize="500" :fileType="['docx', 'doc', 'pdf']" />
          </el-form-item>
          <el-form-item label="备注" prop="remarks">
            <el-input type="textarea" :rows="2" v-model="form.remarks" placeholder="请输入备注" />
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAtlasFile, addAtlasFile, updateAtlasFile, removeAtlasFile } from '@/api/als/atlasFile'
import { deepClone, debounce } from '@/utils/index'
import FileUpload from '@/views/als/components/FileUpload'

export default {
  name: 'AtlasFile',
  components: { FileUpload },
  data() {
    // 这里存放数据
    return {
      dialogTitle: '新增',
      dialogVisible: false,
      keyWordData: '',
      searchValue: '',
      columns: [
        {
          prop: 'fileName',
          label: '文件名称'
        },
        {
          prop: 'fileType',
          label: '文件类型'
        },
        {
          prop: 'ossId',
          label: '文件编号'
        },
        {
          prop: 'remarks',
          label: '备注'
        },
        {
          button: true,
          label: '操作',
          width: '240px',
          group: [
            {
              name: '编辑',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.handUpdate(row)
              }
            },
            {
              name: '删除',
              type: 'text',
              round: false,
              plain: false,
              onClick: (row, index, scope) => {
                this.remove([row])
              }
            }
          ]
        }
      ],
      options: {
        stripe: false, // 斑马纹
        mutiSelect: true, // 多选框
        index: false, // 显示序号， 多选则 mutiSelect
        loading: false, // 表格动画
        initTable: false, // 是否一挂载就加载数据
        border: true,
        height: 'calc(100vh - 300px)'
      },
      tableCheckItems: [],
      tableData: [],
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 20,
        searchValue: ''
      },
      form: {
        id: '',
        fileName: '',
        fileType: '',
        ossId: '',
        status: '',
        remarks: ''
      },
      debounceFn: debounce(this.fetch, 500),
      fileTypeList: [
        { key: '术语', name: '术语' },
        { key: '故障案例', name: '故障案例' },
        { key: '专业手册', name: '专业手册' },
        { key: '期刊', name: '期刊' },
        { key: '其它', name: '其它' }
      ]
    }
  },
  watch: {
    keyWord() {
      this.tableRequset.pageIndex = 1
      this.debounceFn()
    }
  },
  mounted() {
    this.getAtlasFileAPI()
  },
  methods: {
    async removeAtlasFileAPI(params) {
      try {
        const { code } = await removeAtlasFile(params)
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          await this.getAtlasFileAPI()
          this.handleClose()
        }
      } catch (error) {}
    },

    async getAtlasFileAPI(params) {
      if (this.$refs.table) this.$refs.table.clearSelection()
      const { pageSize, pageIndex } = this.tableRequset
      const {
        data: { list, total }
      } = await getAtlasFile({ pageSize, pageNum: pageIndex, ...params })
      this.tableData = list
      this.tableRequset.total = total
    },

    fetch() {
      this.getAtlasFileAPI()
    },

    async searchClick() {
      this.getAtlasFileAPI({ fileName: this.keyWordData })
    },

    async addAtlasFileAPI() {
      try {
        delete this.form.aircaftModelName
        const { code } = await addAtlasFile({ ...this.form })
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getAtlasFileAPI()
        }
      } catch (error) {}
    },

    async updateAtlasFileAPI() {
      try {
        const { code } = await updateAtlasFile({ ...this.form })
        if (code === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getAtlasFileAPI()
        }
      } catch (error) {}
    },

    openDialog() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
    },

    handleClose() {
      this.dialogVisible = false
      this.form = {
        id: '',
        fileName: '',
        fileType: '',
        ossId: '',
        status: '',
        remarks: ''
      }
    },

    handUpdate(row) {
      this.dialogTitle = '编辑'
      this.form = deepClone(row)
      this.dialogVisible = true
    },

    submit() {
      switch (this.dialogTitle) {
        case '编辑':
          this.updateAtlasFileAPI()

          break
        case '新增':
          this.addAtlasFileAPI()

          break
      }
    },

    selection(val) {
      this.tableCheckItems = val
    },

    remove(row) {
      this.$confirm('是否删除该模型', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.removeAtlasFileAPI(row.map((e) => e.id))
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../index.scss';
</style>
