<template>
  <div class="app-container">
    <el-row :gutter="40">
      <el-col :lg="4" :md="8" :xs="24"  style="border-right: 1px solid #ebeef5;">
        <attach-classify-tree @node-click="handleClassifyNodeClick"/>
      </el-col>
      <el-col :lg="20" :md="16" :xs="24" >
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="资源名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入资源名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="所属部门" prop="deptId">
            <dept-select v-model="queryParams.deptId" @change="handleQuery" check-strictly/>
          </el-form-item>
          <el-form-item label="创建人" prop="createId">
            <user-select v-model="queryParams.createId" @change="handleQuery"/>
          </el-form-item>
          <el-form-item label="URL" prop="url">
            <el-input
              v-model="queryParams.url"
              placeholder="请输入URL"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-has-permi="['bst:attach:add']"
            >上传</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-row :gutter="20" v-loading="loading" v-if="attachList.length > 0">
          <el-col :lg="4" :md="8" :xs="12" v-for="item in attachList" :key="item.id">
            <attach-card :url="item.url" :name="item.name" :preview-list="[item.url]">
              <template #extra>
                <div class="attach-info">
                  <div class="info-item" v-for="column in showColumns" :key="column.key" v-if="isShow(column.key)">
                    <span class="label">{{ column.label }}:</span>
                    <el-tooltip :content="item[column.key]" placement="top" :disabled="!item[column.key]">
                      <span class="value">{{ item[column.key] | dv }}</span>
                    </el-tooltip>
                  </div>
                  <div class="action-buttons">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(item)"
                      v-has-permi="['bst:attach:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(item)"
                      v-has-permi="['bst:attach:remove']"
                    >删除</el-button>
                  </div>
                </div>
              </template>
            </attach-card>
          </el-col>
        </el-row>
        <el-empty v-else description="暂无文件"></el-empty>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

      </el-col>
    </el-row>

    <!-- 资源编辑弹窗 -->
    <attach-edit-dialog 
      :show.sync="open"
      :id="row.id" 
      :init-data="{classifyId: queryParams.classifyId}" 
      @success="getList"
    />
  </div>
</template>

<script>
import { listAttach, delAttach } from "@/api/bst/attach";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import AttachClassifyTree from '../attachClassify/components/AttachClassifyTree.vue';
import AttachCard from '@/components/AttachCard/index.vue';
import UserSelect from '@/components/Business/User/UserSelect.vue';
import AttachEditDialog from '@/views/bst/attach/components/AttachEditDialog.vue';
import DeptSelect from '@/components/Business/Dept/DeptSelect.vue';


  // 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "Attach",
  mixins: [$showColumns],
  components: {FormCol, AttachClassifyTree, AttachCard, UserSelect, AttachEditDialog, DeptSelect},
  data() {
    return {
      row: {},
      span: 24,
      // 字段列表
      columns: [
        {key: 'id', visible: false, label: '资源ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'classifyName', visible: true, label: '分类', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'name', visible: false, label: '名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'url', visible: false, label: 'URL', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'deptName', visible: true, label: '所属部门', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'createName', visible: true, label: '创建人', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'createTime', visible: true, label: '创建时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源表格数据
      attachList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 18,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        id: null,
        classifyId: null,
        deptId: null,
        name: null,
        url: null,
        createId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classifyId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "URL不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 分类树节点点击事件
    handleClassifyNodeClick(node) {
      this.queryParams.classifyAncestorId = node.id;
      this.getList();
    },
    /** 查询资源列表 */
    getList() {
      this.loading = true;
      listAttach(this.queryParams).then(response => {
        this.attachList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.row = {};
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.row = row;
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资源编号为"' + ids + '"的数据项？').then(function() {
        return delAttach(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/attach/export', {
        ...this.queryParams
      }, `attach_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.attach-info {
  padding: 10px;
  border-top: 1px solid #ebeef5;
  
  .info-item {
    margin-bottom: 5px;
    font-size: 12px;
    display: flex;
    align-items: center;
    
    .label {
      color: #909399;
      margin-right: 5px;
      flex-shrink: 0;
    }
    
    .value {
      color: #606266;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      max-width: 100%;
      cursor: pointer;
      
      &:hover {
        color: #409EFF;
      }
    }
  }

  .action-buttons {
    margin-top: 8px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
