<template>
  <div
    class="fileManagement_List"
    v-loading.fullscreen.lock="fullscreenLoading"
  >
    <div class="fileManagement_List_headerSelect">
      <el-form
        slot="form"
        style="display: inline"
        :model="form"
        inline
        :addList="true"
      >
        <el-form-item class="header_top_form" label="分类名称：">
          <el-input
            v-model.trim="form.name"
            placeholder="请输入"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item class="header_top_form" label="文件名称：">
          <el-input
            v-model.trim="form.fileName"
            placeholder="请输入"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item class="header_top_form" label="状态：">
          <el-select v-model="form.status" size="small" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            style="width: 88px"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-button
        size="small"
        style="width: 104px; margin-right: 23px"
        @click="addList"
        >新增</el-button
      >
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      stripe
      :header-cell-style="{ background: '#F7F7F7' }"
      height="66vh"
    >
      <el-table-column
        v-for="(item, index) in listData"
        :key="index"
        :label="item.name"
        :prop="item.value"
        :width="item.width"
        :min-width="item.minWidth"
        :align="item.align"
      >
        <template slot-scope="scope">
          <div>
            <div v-if="item.value == 'status'">
              <el-switch
                v-model="scope.row.status"
                active-color="#17A8FF"
                inactive-color="#AFAFAF"
                @change="statusChange($event, scope.row)"
              >
              </el-switch>
            </div>
            <div v-else>
              {{ scope.row[scope.column.property] }}
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <div>
            <el-button type="text" @click="history(scope.row)"
              >历史版本</el-button
            >
            <el-button type="text" @click="edit">编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.pageNum"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="form.pageSize"
      background
      layout="total,prev, pager, next,jumper"
      :total="form.total"
    >
    </el-pagination>
    <editDialog ref="editDialogRefs" />
    <detailDialog ref="detailDialogRefs" />
  </div>
</template>
<script>
import editDialog from './edit'
import detailDialog from './detail'
import * as api from '@/api/api'
export default {
  name: 'fileManagement',
  components: {
    editDialog,
    detailDialog
  },
  data() {
    return {
      fullscreenLoading: false,
      tableData: [],
      options: [],
      listData: [
        {
          name: '分类名称',
          value: 'name',
          width: '',
          minWidth: '',
          align: 'center'
        },
        {
          name: '文件名称',
          value: 'typeName',
          width: '',
          minWidth: '',
          align: 'center'
        },
        {
          name: '连接地址',
          value: 'file',
          width: '',
          minWidth: '',
          align: 'center'
        },
        {
          name: '禁用/启用',
          value: 'status',
          width: '',
          minWidth: '',
          align: 'center'
        },
        {
          name: '创建人',
          value: 'createUser',
          width: '',
          minWidth: '',
          align: 'center'
        },
        {
          name: '创建时间',
          value: 'createTime',
          width: '',
          minWidth: '',
          align: 'center'
        }
      ],
      form: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        name: '',
        start: '',
        status: '',
        typeId: ''
      }
    }
  },
  mounted() {
    this.listDataInit()
  },
  methods: {
    /**
     * @method 搜索
     * **/
    search() {
      this.form.pageNum = 1
      this.listDataInit()
    },
    /**
     * @method 列表数据
     * **/
    listDataInit() {
      this.fullscreenLoading = true
      let code = {
        name: this.form.name,
        page: this.form.pageNum,
        pageSize: this.form.pageSize,
        start: this.form.start,
        status: this.form.status,
        typeId: this.form.typeId
      }
      api
        .POST('/doc/getDocList', code)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list
            this.tableData.forEach(item => {
              item.status == 1 ? (item.status = true) : (item.status = false)
            })
            this.form.pageNum = res.data.currPage
            this.form.pageSize = res.data.pageSize
            this.form.total = res.data.totalCount
            this.fullscreenLoading = false
          }
        })
        .catch(() => {
          this.fullscreenLoading = false
        })
    },
    handleSizeChange() {},
    handleCurrentChange() {},
    /**
     * @method 新增
     * **/
    addList() {
      let code = {
        title: '文件新增',
        show: true
      }
      this.$refs.editDialogRefs.openClose(code)
    },
    // 编辑
    edit() {
      this.$refs.editDialogRefs.openClose()
    },
    // 历史版本
    history(val) {
      const code = {
        id: val.id
      }
      this.$refs.detailDialogRefs.openClose(code)
    },
    /**
     * @method 状态修改
     * **/
    statusChange(val, item) {
      val ? (val = 1) : (val = 0)
      let code = {
        id: item.id,
        status: val
      }
      api.PUT('/doc/updateSwitch', code).then(data => {
        if (data.code == 0) {
          this.search()
          this.$message.success(data.message)
        }
      }).catch(()=>{
        this.search()
      })
    }
  }
}
</script>
<style lang="less">
.fileManagement_List {
  .fileManagement_List_headerSelect {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
    .el-form-item {
      margin-bottom: 0;
      margin-right: 60px;
      .el-input__inner {
        background: #f5f5f5;
        opacity: 1;
        border-radius: 17px;
      }
    }
  }
}
</style>