<template>
  <div class="page specs pagePadding">
    <ex-table
      v-if="rules.some(item => item == '001')"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      @page-curr-change="pageInfo.curr = $event; searchList();"
      @page-size-change="pageInfo.curr = 1;pageInfo.size = $event; searchList();"
      @screen-reset="pageInfo.curr = 1;searchList()"
      @screen-submit="pageInfo.curr = 1; searchList()"
    >
      <template #status="{scope}">
        <el-tag
          :type="scope.row.status == 1 ? 'success' : scope.row.status == 0 ? 'warning' : ''"
          v-text="(statusOptions.find(item => item.id == scope.row.status) || {}).label || '--'"
        ></el-tag>
      </template>
      <template #operation="{scope}">
        <el-tooltip
          v-if="rules.some(item => item == '004')"
          class="item"
          effect="dark"
          :content="scope.row.status == 0 ? '启用' : scope.row.status == 1 ? '禁用' : '--'"
          placement="top"
        >
          <i
            class="tb-icon"
            :class="scope.row.status == 0 ? 'el-icon-circle-check' : scope.row.status == 1 ? 'el-icon-circle-close' : ''"
            @click="enableOrDisableAuth( scope.row.status == 0 ? 1 : scope.row.status == 1 ? 0 : '', [scope.row])"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="rules.some(item => item == '007')"
          class="item"
          effect="dark"
          content="编辑"
          placement="top"
        >
          <i class="tb-icon el-icon-edit" @click="addOrUpdateHandle(scope.row.id, 'edit')"></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
    <!-- 弹窗 新增/修改 -->
    <!-- <add-or-update
      v-if="isAddOrUpdate"
      ref="addOrUpdate"
      :authOptions="authOptions"
      @refreshDataList="pageInfo.curr = 1;searchList()"
    /> -->
  </div>
</template>
<script>
import ExTable from "@/components/ExTable.vue";
import asyncRequest from "@/apis/service/goodStore/specs";
// import AddOrUpdate from "./addOrEditAuth";

export default {
  name: "dataAuth",
  components: { ExTable,
  //  AddOrUpdate 
   },
  computed: {
       rules() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "specs"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
    // rules() {
    //   let tran =
    //     this.$store.state.common.ruleslist.find(
    //       item => item.name == "data_power"
    //     ) || {};
    //   return this.$store.state.common.ruleslist.filter(
    //     item => item.pid === tran.id
    //   );
    // }
  },
  data() {
    return {
      isAddOrUpdate: false,
      // 状态
      statusOptions: [
        { id: 0, label: "禁用" },
        { id: 1, label: "启用" }
      ],
      // 共享数据权限
      authOptions: [
        { id: 1, label: "只读" },
        { id: 2, label: "读写" }
      ],
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"]
      },
      // 表格 - 分页
      pageInfo: {
        size: 10,
        curr: 1,
        total: 0
      },
      // 表格 - 列参数
      columns: [
        { type: "selection", fixed: "left", _noset_: true },
        {
          type: "index",
          label: "序号",
          fixed: "left",
          width: "50",
          _noset_: true
        },
        {
          prop: "id",
          label: "ID",
          "min-width": "70",
          sortable: true
        },
        {
          prop: "shar_rulenames",
          label: "菜单数据",
          "min-width": "120"
        },
        {
          prop: "usernames",
          label: "数据源用户",
          "min-width": "120"
        },
        {
          prop: "groupnames",
          label: "数据源用户组",
          "min-width": "120"
        },
        {
          prop: "shar_usernames",
          label: "数据共享用户",
          "min-width": "120"
        },
        {
          prop: "shar_groupnames",
          label: "数据共享用户组",
          "min-width": "120"
        },
        {
          prop: "data_shar_auth",
          label: "共享数据权限",
          "min-width": "140",
          sortable: true,
          _format_: d => {
            return (
              this.authOptions.find(item => {
                return item.id == d.data_shar_auth;
              }) || {}
            ).label;
          }
        },
        {
          prop: "created_at",
          label: "添加时间",
          "min-width": "160",
          sortable: true
        },
        {
          prop: "status",
          label: "状态",
          fixed: "right",
          "min-width": "100",
          sortable: true,
          _slot_: "status"
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          "min-width": "120",
          _noset_: true,
          _slot_: "operation"
        }
      ]
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    // 新建/编辑用户组
    addOrUpdateHandle(id, type) {
      let _this = this;
      _this.isAddOrUpdate = true;
      // 弹窗初始化
      _this.$nextTick(() => {
        _this.$refs.addOrUpdate.init(id, type);
      });
    },
    /**
     * 启用/禁用
     * @param {String} status 0-禁用 1-启用
     * @param {Array} selection 当前选择项
     */
    enableOrDisableAuth(status, selection) {
      if (selection.length == 0)
        return this.$message.warning("请至少选择一条数据！");
      this.$confirm(
        `此操作将${
          status == 0 ? "禁用" : status == 1 ? "启用" : "--"
        }选择的共享规则, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.enableOrDisableAuthService(
          status,
          selection.map(item => item.id)
        );
      });
    },
    /**
     * 启用/禁用服务
     */
    enableOrDisableAuthService(status, ids) {
      let loadding = this.$loading();
      this.axios
        .post(`/recruit/v1/shar_start_stop`, {
          status: status,
          id_list: ids
        })
        .then(res => {
          loadding.close();
          if (res && res.data && res.data.status == 0) {
            this.$message.success("操作成功");
            this.searchList();
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(error => {
          loadding.close();
          this.$message.error(error);
        });
    },
    // // 刷新表格
    // searchList() {
    //  this.loading = true;
    //   let res = await asyncRequest.list(this.parmValue);
    //   if (res.code === 0 && res.data) {
    //     this.tableData = res.data.list;
    //     this.tableData.map((v1) => {
    //       v1.addtime = v1.addtime.replaceAll(".000", "");
    //       return v1;
    //     });
    //     this.count = Number(res.data.count);
    //   } else {
    //     this.tableData = [];
    //     this.count = 0;
    //   }
    //   this.loading = false;
    // },
        restSearch() {
      this.parmValue = {
        status: "",
        name: "", // 名称
        page: 1, // 页码
        size: 10, // 每页显示条数
      };
      this.searchList();
    },
    //新建/编辑/详情/审核
    openModal(id, isDetail, approval, type) {
      if (!type) {
        return;
      }
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
      this.approval = approval;
    },
    //删除
    async deleteById(id) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await asyncRequest.delete({ id });
          if (res.code === 0) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
//提交审核申请
    async again(id, type) {
      if (!type) {
        return;
      }
      await this.$confirm("确定要提交审核？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await asyncRequest.again({ id: id });
          if (res.code === 0) {
            this.$notify.success({
              title: "提交成功",
              message: "",
            });
            this.searchList();
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    //刷新表格
    async searchList() {
      this.loading = true;
      let res = await asyncRequest.list(this.parmValue);
      if (res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map((v1) => {
          v1.addtime = v1.addtime.replaceAll(".000", "");
          return v1;
        });
        this.count = Number(res.data.count);
      } else {
        this.tableData = [];
        this.count = 0;
      }
      this.loading = false;
    },
    async statusConfirm(id, status, ex_status, type) {
      if (!type) {
        return;
      }
      if (ex_status === "1") {
        await this.$confirm(`确定要改为${status === "1" ? "禁用" : "启用"}？`, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            this.loading = true;
            const model = {
              id: id,
              status: status === "1" ? "0" : "1",
            };
            const res = await asyncRequest.status(model);
            if (res.code === 0) {
              this.loading = false;
              this.$notify.success({
                title: "状态修改成功！",
                message: "",
              });
              await this.searchList();
            } else {
              this.loading = false;
            }
          })
          .catch(() => {
            console.log("取消");
          });
      } else {
        if (status === "0") {
          this.$message.warning("只有审核通过的规格，才能启用！");
        }
      }
    },
  }
};
</script>

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