<template>
  <div class="page_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <bg-filter-group @search="changeSearch" v-model="filter.keyword" placeholder="请输入规则名称或规则">
        <template v-slot:left_action>
          <div class="apaas_button">
            <el-button type="primary" @click="addRule">
              <bg-icon style="font-size: 12px; color: #fff; margin-right: 8px" icon="#bg-ic-add"></bg-icon>
              新增
            </el-button>
            <el-button @click="deleteBatch"> 批量删除 </el-button>
            <span class="sleceted_tip">已选<span class="num">3</span>项</span>
            <span class="clean" @click="cleanSelected">清空</span>
          </div>
        </template>

        <template v-slot:filter_group>
          <div class="left-filter filter_list">
            <div class="filter_item">
              <span class="filter_title">修改时间</span>
              <el-date-picker
                v-model="filter.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
                style="width: 300px" />
            </div>
          </div>
          <div class="right-action apaas_button">
            <el-button type="primary" @click="filterAction"> 查询 </el-button>
            <el-button type="default" @click="filterClear"> 重置 </el-button>
          </div>
        </template>
      </bg-filter-group>
      <div class="table_container">
        <bg-table ref="bgTable" :headers="headers" :rows="tableRows" :stripe="true" select>
          <template v-slot:ability_name="{ row }">
            <span @click="goDetail(row)" class="can_click_text">
              {{ row.ability_name }}
              <span v-if="row.include_mock_service" class="mock_tip"> mock </span>
            </span>
          </template>
          <template v-slot:ability_state="{ row }">
            <span class="circle" :class="'bgc_' + row.ability_state"></span>
            {{ ["待上架", "已上架", "已下架", "上架中", "下架中"][row.ability_state] }}
          </template>
          <template v-slot:start_use="{ row }">
            <el-switch></el-switch>
          </template>
          <template v-slot:action="{ row }">
            <bg-table-btns2 :limit="3" :key="row.id">
              <bg-table-btn @click="edit(row)">编辑</bg-table-btn>
              <bg-table-btn @click="deleteCurrent(row)">删除</bg-table-btn>
            </bg-table-btns2>
          </template>
        </bg-table>
        <bg-pagination
          :page="filter.page"
          :size="filter.size"
          :total="tableTotal"
          @change-page="changePage"
          @change-size="changeSize">
        </bg-pagination>
      </div>
    </div>

    <el-dialog class="dialog_box_detail" title="用户详情" v-model="dialogDetail" width="1062px">
      <div class="content_detail">
        <div class="form_filter">
          <div class="left">
            <el-select placeholder="全部类型" style="width: 200px; margin-left: 16px">
              <el-option label="选共享1" value="1"> </el-option>
              <el-option label="选共享2" value="2"> </el-option>
            </el-select>
            <el-select placeholder="请选择所属组织" style="width: 200px; margin-left: 16px">
              <el-option label="选共享1" value="1"> </el-option>
              <el-option label="选共享2" value="2"> </el-option>
            </el-select>
            <el-input placeholder="请输入关键词" style="width: 200px; margin-left: 16px" :prefix-icon="Search" />
          </div>
          <div class="right">
            <el-button type="primary" @click="cancelCache">查询</el-button>
            <el-button type="default" @click="confirmCache">重置</el-button>
          </div>
        </div>

        <div class="table_content">
          <bg-table ref="bgTable" :headers="detailHeaders" :rows="tableRows" height="430" :stripe="true">
            <template v-slot:ability_name="{ row }">
              <span @click="goDetail(row)" class="can_click_text">
                {{ row.ability_name }}
                <span v-if="row.include_mock_service" class="mock_tip"> mock </span>
              </span>
            </template>
            <template v-slot:ability_state="{ row }">
              <span class="circle" :class="'bgc_' + row.ability_state"></span>
              {{ ["待上架", "已上架", "已下架", "上架中", "下架中"][row.ability_state] }}
            </template>
            <template v-slot:action="{ row }">
              <bg-table-btns2 :limit="3" :key="row.id">
                <bg-table-btn @click="approve(row)">审批</bg-table-btn>
                <bg-table-btn @click="approveDetail(row)">审批详情</bg-table-btn>
              </bg-table-btns2>
            </template>
          </bg-table>
          <bg-pagination
            :page="filter.page"
            :size="filter.size"
            :total="tableTotal"
            @change-page="changePage"
            @change-size="changeSize">
          </bg-pagination>
        </div>
      </div>
    </el-dialog>

    <el-dialog class="dialog_box" title="提示" v-model="cacheDialog" width="400px">
      <div style="font-size: 16px; color: #404a62">你有未提交的服务，是否继续编辑该服务？</div>
      <template v-slot:footer>
        <div class="apaas_button">
          <el-button type="default" @click="cancelCache">取 消</el-button>
          <el-button type="primary" @click="confirmCache">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, toRefs, computed, onBeforeMount } from "vue";
import { Search } from "@element-plus/icons-vue";

import { useRouter } from "vue-router";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import store from "@/store";
import axios from "@/request/http.js";
import { ElMessage } from "element-plus";

const router = useRouter();
const state = reactive({
  filter: {
    state: "",
    keyword: "",
    page: 1,
    size: 10,
    abilityType: 0,
    time: [],
  },
  stateOptions: [
    {
      name: "全部",
      value: "",
    },
    {
      name: "已上架",
      value: "1",
    },
    {
      name: "已下架",
      value: "2",
    },
    {
      name: "上架中",
      value: "3",
    },
    {
      name: "下架中",
      value: "4",
    },
    {
      name: "待上架",
      value: "0",
    },
  ],
  headers: [
    {
      label: "规则名称",
      prop: "ability_name",
      // minWidth: 280,
    },
    {
      label: "规则类型",
      prop: "synopsis",
      // minWidth: 360,
    },
    {
      label: "用户数量",
      prop: "created_time",
      // width: 200,
    },
    {
      label: "规则",
      prop: "ability_state",
      // width: 120,
    },
    {
      label: "是否启用",
      prop: "start_use",
      // width: 120,
    },
    {
      label: "修改时间",
      prop: "ability_state",
      width: 240,
    },
    {
      label: "操作",
      prop: "action",
      width: 120,
      fixed: "right",
    },
  ],
  detailHeaders: [
    {
      label: "账号",
      prop: "ability_name",
      // minWidth: 280,
    },
    {
      label: "类型",
      prop: "synopsis",
      // minWidth: 360,
    },
    {
      label: "用户手机号",
      prop: "created_time",
      // width: 200,
    },
    {
      label: "所属组织",
      prop: "ability_state",
      // width: 120,
    },
  ],
  tableRows: [],
  tableTotal: 0,
  actionRow: {},
  dialogDetail: false,
  cacheDialog: false,
});

const { filter, stateOptions, headers, detailHeaders, tableRows, tableTotal, dialogDetail, cacheDialog } =
  toRefs(state);
// 认证方案未确定，暂时使用假数据registerValid，后续更改
const registerValid = computed(() => {
  return store.state.registerValid;
});
//新增
const addRule = () => {
  router.push("/develop/menu/add");
};

//批量删除
const deleteBatch = () => {};
//清空
const cleanSelected = () => {};
const cancelCache = () => {
  axios
    .get(`/apaas/service/v5/ability/clear/cach?abilityType=0`)
    .then((res) => {
      if (res.data.code == 200) {
        state.cacheDialog = false;
        router.push("/ability-register/add");
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      router.push("/ability-register/add");
    });
}; // 取消按钮，清空缓存跳转注册页
const confirmCache = () => {
  state.cacheDialog = false;
  router.push("/ability-register/add");
}; // 确定按钮，直接跳转注册页
const changeSearch = (val) => {
  state.filter.keyword = val;
  changePage(1);
};
const changePage = (page) => {
  state.filter.page = page;
  getTableRows();
};
const getTableRows = () => {
  let params = { ...state.filter };
  params.registerStartTime = params.time ? params.time[0] || "" : "";
  params.registerEndTime = params.time ? params.time[1] || "" : "";
  delete params.time;
  axios
    .get(`/apaas/service/v5/ability/list`, {
      params,
    })
    .then((res) => {
      if (res.data.code == 200) {
        state.tableRows = res.data.data || [];
        state.tableTotal = res.data.total;
      } else {
        ElMessage.error(res.data.data);
      }
    });
};
const filterAction = () => {
  changePage(1);
};
const changeSize = (size) => {
  state.filter.size = size;
  changePage(1);
};
const filterClear = () => {
  state.filter = {
    state: "",
    keyword: "",
    page: 1,
    size: 10,
    abilityType: 0,
    time: [],
  };
  changePage(1);
};
//编辑
const edit = (row) => {
  state.dialogDetail = true;
  // router.push(`/develop/account/add`);
};
//删除
const deleteCurrent = (row) => {
  router.push(`/develop/account/detail`);
};

const goDetail = (row) => {
  router.push({
    path: "/ability-manage/real-list/detail",
    query: {
      id: row.id,
    },
  });
};
onBeforeMount(() => {
  getTableRows();
});
</script>

<style lang="scss" scoped>
.page_container {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  width: 100%;
  padding: 0 24px;
  min-height: 100%;
  .main_container {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    flex-grow: 1;
    width: 100%;
    background-color: #fff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    padding: 0 0 16px 0;
    margin-bottom: 16px;
    position: relative;
    .sleceted_tip {
      margin: 0 24px 0 40px;
      vertical-align: middle;
      font-size: 14px;
      color: #404a62;
      .num {
        margin: 0 3px 0 3px;
        font-weight: 600;
        color: #202531;
      }
    }
    .clean {
      vertical-align: middle;
      font-size: 14px;
      color: #3759be;
      cursor: pointer;
    }
    .filter-group {
      .left-filter {
        flex: 1;
        display: flex;
        justify-content: start;
        flex-wrap: wrap;
        .filter_item {
          display: flex;
          align-items: center;
          .filter_title {
            width: 84px;
            white-space: pre-wrap;
            font-weight: normal;
          }
        }
      }
      .right-action {
        width: 144px;
        padding-bottom: 16px;
        .el-button {
          width: 64px;
        }
      }
    }
    .table_container {
      padding: 0 16px;
      .el-table {
        flex: 1;
        .circle {
          display: inline-block;
          width: 6px;
          height: 6px;
          border-radius: 3px;
          margin-right: 8px;
          transform: translateY(-2px);
        }
        .bgc_1 {
          background-color: #48ad97;
        }
        .bgc_2 {
          background-color: #d75138;
        }
        .bgc_3 {
          background-color: #3759be;
        }
        .bgc_4 {
          background-color: #ea7d19;
        }
        .bgc_0 {
          background-color: #9e9e9e;
        }
        .mock_tip {
          display: inline-block;
          margin-left: 4px;
          width: 43px;
          height: 20px;
          font-size: 12px;
          color: #2b4695;
          text-align: center;
          background-color: #eff2fa;
          border-radius: 3px;
          border: solid 1px #b0bee8;
        }
        ::v-deep td {
          padding: 9px 0 !important;
        }
        ::v-deep .el-switch {
          height: 20px;
          width: 44px;
          .el-switch__core {
            height: 20px;
            min-width: 44px;
          }
        }
      }
    }
  }

  ::v-deep .dialog_box_detail {
    .el-dialog__body {
      padding: 0 0 18px 0;
      height: 580px;
    }

    .form_filter {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 69px;
      border-bottom: 1px solid #e6e9ef;
      .right {
        margin-right: 16px;
        .el-button {
          width: 64px;
          &:last-child {
            margin-left: 16px;
          }
        }
      }
    }
    .table_content {
      padding: 0 16px;
    }
  }
}
</style>
