<template>
  <el-container>
    <el-header height="60px" class="content-header">
      <MyHeader
        :title="'接入方管理'"
        :formData="headerFormData"
        @submit="handleHeaderFormSubmit"
      ></MyHeader>
    </el-header>
    <el-container>
      <el-main>
        <MyTableHeader
          :title="tableHeaderTitle"
          :tabs="tabs"
          :buttons="buttons"
          @tabClick="handleTabClick"
          @buttonClick="handleButtonClick"
        ></MyTableHeader>

        <MyTable
          :tableData="tableData"
          :formData="formData"
          :minWidth="240"
          :isOperate="isOperate"
          ref="myTableRef"
          @selection-change="handleSelectionChange"
        >
          <!-- 自定义操作按钮，传递 row 和 index -->
          <template #operation="{ row, index }">
            <el-button
              size="mini"
              @click="handleView(row, 'view')"
              type="primary"
              >查看</el-button
            >
            <el-button
              size="mini"
              @click="handleEdit(row, 'edit')"
              type="primary"
              >编辑</el-button
            >

            <el-button size="mini" @click="handleDelete(row)" type="primary"
              >删除</el-button
            >
            <el-button
              v-if="row.isApply === 0"
              size="mini"
              @click="handleStart(row)"
              type="primary"
              >启用</el-button
            >
            <el-button
              v-if="row.isApply === 1"
              size="mini"
              @click="handleStop(row)"
              type="primary"
              >停用</el-button
            >
          </template>
        </MyTable>
        <MyPagination
          :total="tableTotal"
          :page-size="pageSize"
          :current-page="currentPage"
          @pageChange="pagi_changePage"
          @sizeChange="pagi_changeSize"
        ></MyPagination>
      </el-main>
    </el-container>

    <!-- 新增弹窗 -->
    <div v-if="showDialog">
      <AccessPartyMaintenanceDialog
        v-model="showDialog"
        :title="title"
        :rowObj="rowObj"
        @confirm="getList"
      >
      </AccessPartyMaintenanceDialog>
    </div>
    <!-- 导入 -->
    <div v-if="fileUploadShowDialog">
      <FileUploadDialog
        v-model="fileUploadShowDialog"
        :uploadUrl="uploadUrl"
        @confirm="FileUploadConfirm"
      ></FileUploadDialog>
    </div>
  </el-container>
</template>
<script>
import {
  getTabAccessList,
  updateTabAccessList,
  delTabAccessList,
  importDataTabAccessList,
} from "@/api/unified-access/access-party-maintenance.js";
import AccessPartyMaintenanceDialog from "./dialog/AccessPartyMaintenanceDialog.vue";
import FileUploadDialog from "./dialog/FileUploadDialog.vue";
import MyHeader from "@/components/MyHeader";
import MyTable from "@/components/MyTable";
import MyPagination from "@/components/MyPagination";
import MyTableHeader from "@/components/MyTableHeader";
export default {
  name: "user-manage",
  components: {
    MyHeader,
    MyTableHeader,
    MyTable,
    MyPagination,
    AccessPartyMaintenanceDialog,
    FileUploadDialog,
  },
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/web/tab-access/importData",
      fileUploadShowDialog: false, // 上传弹窗
      title: "", //弹窗标题
      rowObj: {}, //列表点击项
      showDialog: false, // 新增弹窗
      searchForm: {}, //头部搜索
      isOperate: true,
      tableData: [], //表格数据
      selectedRows: [], // 存储选中的行数据
      formData: [
        {
          label: "系统名称",
          fieldName: "name",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "所属单位/部门",
          fieldName: "dept",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "联系人姓名",
          fieldName: "contact",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "联系人电话",
          fieldName: "phone",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "备注",
          fieldName: "ps",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "接入需求联系人姓名",
          fieldName: "contact",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "接入需求联系人电话",
          fieldName: "phone",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
        {
          label: "状态",
          fieldName: "isApply",
          type: "input",
          showInTable: true,
          value: "",
          width: "",
        },
      ], // 表头
      tableTotal: 0,
      pageSize: 10,
      currentPage: 1,
      tableHeaderTitle: "项目列表",
      dataType: "", //tab类型
      tabs: [
        { label: "全部", name: "全部", active: true },
        {
          label: "数据提供方",
          name: "数据提供方",
          active: false,
        },
        { label: "数据接收方", name: "数据接收方", active: false },
      ], //头部tab
      buttons: [
        {
          label: "新建",
          type: "primary",
          methodName: "action1",
          icon: "el-icon-circle-plus",
        },
        {
          label: "删除",
          type: "primary",
          methodName: "action2",
          icon: "el-icon-remove",
        },
        {
          label: "导入",
          type: "primary",
          methodName: "action3",
          icon: "el-icon-download",
        },
        {
          label: "导出",
          type: "primary",
          methodName: "action4",
          icon: "el-icon-upload2",
        },
      ],

      headerFormData: [
        {
          label: "系统名称",
          fieldName: "username",
          type: "input",
          value: "",
          placeholder: "请输入系统名称",
          // isRequired: true,
        },
      ],
    };
  },
  methods: {
    // 头部搜索框
    handleHeaderFormSubmit(form) {
      this.searchForm = form;
      this.getList();
    },
    // 列表头部功能按钮
    handleButtonClick(methodName) {
      console.log(methodName, "methodName");

      switch (methodName) {
        case "action1":
          // 新增
          this.rowObj = {};
          this.title = "新增";
          this.showDialog = true;
          break;
        case "action2":
          // 删除
          if (this.selectedRows.length > 0) {
            // exportTabAccessConfig().then((res) => {
            //   if (+res.code === 200) {
            //   }
            // });
            let ids = [];
            ids = this.selectedRows.map((item) => item.id);
          } else {
            this.$confirm("请选择列表项?", "提示", {
              confirmButtonText: "关闭",
              cancelButtonText: "取消",
              showCancelButton: false,
              showConfirmButton: false,
              type: "warning",
              center: true,
            })
              .then(() => {})
              .catch(() => {});
          }
          break;

        case "action3":
          // 导入
          this.fileUploadShowDialog = true;
          break;

        case "action4":
          // 导出
          console.log(this.selectedRows, "this.selectedRows==action4");
          if (this.selectedRows.length > 0) {
            let ids = [];
            ids = this.selectedRows.map((item) => item.id);
            this.download(
              "/web/tab-access/export",
              {
                ids,
              },
              `接入方管理_${new Date().getTime()}.xlsx`
            );
          } else {
            this.$confirm("请选择列表项?", "提示", {
              confirmButtonText: "关闭",
              cancelButtonText: "取消",
              showCancelButton: false,
              showConfirmButton: false,
              type: "warning",
              center: true,
            })
              .then(() => {})
              .catch(() => {});
          }
          break;

        default:
          break;
      }
    },
    FileUploadConfirm() {
      this.fileUploadShowDialog = false
      this.getList()
    },

    // 处理选中项变化
    handleSelectionChange(selectedRows) {
      this.selectedRows = selectedRows; // 获取选中的行
    },
    handleTabClick(tab) {
      this.tabs.forEach((item) => {
        if (item.label === tab.label) {
          item.active = true;
          if (tab.label === "全部") {
            this.dataType = "";
          } else {
            this.dataType = item.label;
          }
          this.getList();
        } else {
          item.active = false;
        }
      });
    },

    getList() {
      getTabAccessList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        dataType: this.dataType,
        ...this.searchName,
      }).then((res) => {
        if (+res.code === 200) {
          this.tableData = res.rows;
          this.tableTotal = +res.total;
        }
      });
    },
    // 列表分页
    pagi_changePage(page) {
      this.currentPage = page;
      this.getList();
    },
    pagi_changeSize(size) {
      this.pageSize = size;
      this.getList();
    },
    handleView(row) {
      this.rowObj = row;
      this.title = "查看";
      this.showDialog = true;
    },
    handleEdit(row) {
      console.log(row, "row");
      this.rowObj = row;
      this.title = "编辑";
      this.showDialog = true;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning",
      })
        .then(() => {
          delTabAccessList({ ids: [row.id] }).then((res) => {
            if (+res.code === 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleStart(row) {
      let pForm = {};
      pForm = { ...row };
      pForm.isApply = 1;
      updateTabAccessList({ ...pForm }).then((res) => {
        if (+res.code === 200) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    handleStop(row) {
      let pForm = {};
      pForm = { ...row };
      pForm.isApply = 0;
      updateTabAccessList({ ...pForm }).then((res) => {
        if (+res.code === 200) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
  created() {},
  mounted() {
    this.getList();
  },
};
</script>
<style lang="scss" scoped>
.el-main {
  padding: 0 20px;
}

.el-icon-search {
  font-size: 16px;
  font-weight: 700;
  color: #1e80e0;
}
</style>
