<template>
  <div class="h-full w-full">
    <table-header>
      <div slot="left">
        <el-button
          size="small"
          type="primary"
          v-hasPermi="['system:resourcemanage:add']"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          size="small"
          type="danger"
          v-hasPermi="['system:resourcemanage:remove']"
          @click="handleDelAll"
          >批量删除</el-button
        >
      </div>
      <div slot="right">
        <el-form ref="searchForm" :model="searchForm" inline>
          <el-form-item prop="authType">
            <el-select
              size="small"
              style="width: 150px"
              v-model="searchForm.authType"
            >
              <el-option label="全部认证类型" value=""></el-option>
              <el-option
                v-for="(v, key) in dict.authType"
                :key="key"
                :label="v"
                :value="key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="name">
            <el-input
              size="small"
              v-model="searchForm.name"
              style="width: 200px"
              class="ml10 mr10"
              placeholder="请输入关键字搜索"
              @keyup.native.enter="handleSearch"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="handleSearch"
              >搜 索</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="handleReset">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </table-header>
    <div class="table-content">
      <y-table
        ref="MyTable"
        :loading="loading"
        :data="tableData"
        :column="column"
        border
        tooltip-effect="dark"
        :max-height="600"
        :row-key="(row) => row.id"
        @selection-change="handleSelectionChange"
      ></y-table>
    </div>
    <y-pagination
      :total="total"
      :page.sync="page"
      :pageSize.sync="size"
      @change="getResourceList"
    ></y-pagination>
    <!-- 新增 -->
    <AddResource
      ref="AddResource"
      :dict="dict"
      :show.sync="show"
      :title="title"
      @success="getResourceList"
    ></AddResource>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import { getResourceList, deleteResource } from "@/api/system";
import AddResource from "./components/AddResource.vue";
export default {
  components: {
    TableHeader,
    AddResource,
  },
  data() {
    return {
      loading: false,
      show: false,
      title: "新增资源",
      column: [
        {
          label: "全选",
          type: "selection",
          width: "55",
          align: "center",
          reserveSelection: true,
        },
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.page - 1) * this.size + index + 1;
          },
        },

        {
          label: "所属分组",
          prop: "groupName",
          align: "center",
        },

        {
          label: "名称",
          prop: "name",
          align: "center",
        },
        {
          label: "资源",
          prop: "url",
          align: "center",
          formatter: (row) => {
            if (row.url) {
              return row.url.split(",").map((v) => {
                return (
                  <el-tag
                    style="margin-right: 5px; margin-bottom: 5px;"
                    type="info"
                    size="mini"
                  >
                    {v}
                  </el-tag>
                );
              });
            }
          },
        },
        {
          label: "认证类型",
          prop: "authType",
          align: "center",
          formatter: (row) => {
            return this.dict.authType[row.authType];
          },
        },
        {
          label: "操作",
          align: "center",
          width: "120",
          formatter: (row) => {
            return (
              <div class="table-control">
                <span
                  v-hasPermi={["system:resourcemanage:edit"]}
                  class="primary pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  v-hasPermi={["system:resourcemanage:remove"]}
                  class="delete pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          },
        },
      ],
      searchForm: {
        authType: "",
        name: "",
      },
      total: 0,
      size: 10,
      page: 1,
      tableData: [],
      selectionRows: [],
      dict: {},
    };
  },
  created() {
    this.getResourceList();
  },
  methods: {
    // 获取资源列表
    async getResourceList() {
      this.loading = true;
      let res = await getResourceList({
        page: this.page,
        size: this.size,
        ...this.searchForm,
        name: `%${this.searchForm.name}%`,
      });
      this.loading = false;
      if (res.data.code == 1) {
        let { data, total, dict } = res.data.data;
        if (!data.length && this.page > 1) {
          this.page -= 1;
          this.getResourceList();
        }
        this.total = total;
        this.tableData = data;
        this.dict = dict;
      }
    },
    handleAdd() {
      this.title = "新增资源";
      this.$refs.AddResource.onAdd();
      this.show = true;
    },
    handleSelectionChange(rows) {
      this.selectionRows = rows;
    },
    handleDelAll() {
      if (!this.selectionRows.length) {
        this.$message.warning("请先勾选数据");
        return;
      }
      let ids = this.selectionRows.map((v) => v.id).join(",");
      this.handleDel(ids);
    },
    handleSearch() {
      this.page = 1;
      this.getResourceList();
    },
    handleReset() {
      this.page = 1;
      this.$resetForm("searchForm");
      this.$clearSelection("MyTable");
      this.getResourceList();
    },

    handleEdit(row) {
      let data = { ...row };
      this.title = "编辑资源";
      this.$refs.AddResource.onEdit(data);
      this.show = true;
    },
    handleDel(id) {
      this.$confirm("此操作将删除所选数据，是否继续？", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteResource({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getResourceList();
            this.$clearSelection("MyTable");
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
