<template>
  <div class="h-full w-full">
    <table-header>
      <div slot="operation">
        <el-button size="small" type="primary" v-hasPermi="['system:access:add']" @click="handleAdd"
          >新增</el-button
        >
        <el-button
          size="small"
          type="danger"
          v-hasPermi="['system:access:remove']"
          @click="handleDelAll"
          >批量删除</el-button
        >
      </div>
      <div slot="search" v-hasPermi="['system:access:query']">
        <el-form ref="searchForm" :model="searchForm" inline>
          <el-form-item prop="areaName">
            <el-input
              size="small"
              v-model="searchForm.areaName"
              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="getAccessList"
    ></y-pagination>
    <!-- 新增 -->
    <AddAccess
      ref="AddAccess"
      :show.sync="show"
      :title="title"
      @success="getAccessList"
    ></AddAccess>
    <AccessSystem
      ref="AccessSystem"
      :show.sync="accessShow"
      :systemList="systemList"
      @success="getAccessList"
    ></AccessSystem>
  </div>
</template>

<script>
import TableHeader from '@/components/TableHeader/TableHeader.vue';
import YTable from '@/components/YTable/YTable.vue';
import YPagination from '@/components/YPagination/YPagination.vue';
import AddAccess from './components/AddAccess.vue';
import AccessSystem from './components/AccessSystem.vue';
import { getAccessList, delAccess } from '@/api/system';
export default {
  components: {
    TableHeader,
    AddAccess,
    AccessSystem,
    YTable,
    YPagination
  },
  data() {
    return {
      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: 'areaName',
          align: 'center'
        },
        {
          label: '区域编码',
          prop: 'areaCode',
          align: 'center'
        },
        {
          label: '背景图片',
          prop: 'bigPath',
          align: 'center',
          formatter: (row) => {
            return <el-image src={row.bigPath} previewSrcList={[row.bigPath]} class="h-[25px]" />;
          }
        },
        {
          label: '接入系统',
          align: 'center',
          formatter: (row) => {
            if (row.accessSystemList && row.accessSystemList.length) {
              return (
                <div class="flex flex-wrap gap-2">
                  {row.accessSystemList.map((v) => {
                    return <el-tag size="small">{v.systemName}</el-tag>;
                  })}
                </div>
              );
            }
          }
        },
        {
          label: '系统标签',
          align: 'center',
          formatter: (row) => {
            if (row.tag) {
              return (
                <div class="flex flex-wrap gap-2">
                  {row.tag.split(',').map((v) => {
                    return (
                      <el-tag type="info" size="small">
                        {v}
                      </el-tag>
                    );
                  })}
                </div>
              );
            }
          }
        },
        {
          label: '对接时间',
          align: 'center',
          formatter: (row) => {
            if (row.accessTime) {
              return this.$moment(row.accessTime).format('YYYY-MM-DD');
            }
          }
        },
        {
          label: '排序',
          prop: 'sort',
          width: '100',
          align: 'center'
        },
        {
          label: '描述',
          prop: 'content',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          label: '备注',
          prop: 'remark',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          label: '操作',
          align: 'center',
          width: '180',
          formatter: (row) => {
            return (
              <div class="flex justify-center gap-4">
                <span
                  v-hasPermi={['system:access:accessSystem']}
                  class="primary cursor-pointer"
                  onClick={() => this.accessSystem(row)}
                >
                  接入系统
                </span>
                <span
                  v-hasPermi={['system:access:edit']}
                  class="primary cursor-pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  v-hasPermi={['system:access:remove']}
                  class="delete cursor-pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          }
        }
      ],
      searchForm: {
        areaName: ''
      },
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      loading: false,
      selectRows: [],
      show: false,
      title: '新增',
      dict: {}, // 字典
      systemList: [],
      accessShow: false,
      areaInfo: {}
    };
  },
  created() {
    this.getAccessList();
  },
  computed: {},
  methods: {
    // 获取接入区域列表
    async getAccessList() {
      this.loading = true;
      let res = await getAccessList({
        page: this.page,
        size: this.size,
        areaName: `%${this.searchForm.areaName}%`
      });
      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.getAccessList();
        }
        this.tableData = data;
        this.total = total;
        this.dict = dict;
      }
    },
    // 批量移除
    handleDelAll() {
      if (!this.selectRows.length) {
        this.$message.warning('请先勾选数据');
        return;
      }
      let ids = this.selectRows.map((v) => v.id).join(',');
      this.handleDel(ids);
    },
    // 搜索
    handleSearch() {
      this.page = 1;
      this.$clearSelection('MyTable');
      this.getAccessList();
    },
    // 重置
    handleReset() {
      this.page = 1;
      this.$clearSelection('MyTable');
      this.$resetForm('searchForm');
      this.getAccessList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectRows = select;
    },

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

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