<template>
  <div class="group pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <el-row :gutter="10">
        <el-col :span="24" style="padding: 0 0 18px 0">
          <!-- <el-col :span="3">
          <el-input
          :size="searchSize"
            v-model="parmValue.loginName"
            :maxlength="40"
            placeholder="账户"
          ></el-input>
        </el-col>
        <el-col :span="3">
          <el-input
          :size="searchSize"
            v-model="parmValue.fullName"
            :maxlength="40"
            placeholder="姓名"
          ></el-input>
        </el-col>
        <el-col :span="3" style="width: 152px">
          <el-button
          :size="searchSize"
            type="primary"
            @click="searchList"
            icon="el-icon-search"
          ></el-button>
          <el-button :size="searchSize" type="warning" @click="restSearch"> 重置 </el-button>
        </el-col> -->
          <el-col :span="3" style="width: 210px; float: right">
            <el-button
              v-if="powers.some((item) => item == '002')"
              :size="searchSize"
              type="primary"
              style="float: right; margin-left: 5px"
              @click="searchList"
            >
              刷新
            </el-button>
            <el-button
              v-if="powers.some((item) => item == '003')"
              :size="searchSize"
              type="success"
              style="float: right"
              @click="openModal('add', false)"
            >
              添加
            </el-button>
          </el-col>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :size="size"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column
          prop="group_name"
          label="组名"
          show-overflow-tooltip
          align="center"
        />

        <el-table-column
          label="组员"
          prop="userGroupList"
          show-overflow-tooltip
          align="center"
        />

        <el-table-column
          prop="group_remark"
          label="备注"
          show-overflow-tooltip
          align="center"
        />

        <el-table-column
          prop="addtime"
          label="添加时间"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column label="状态" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.status === '1'"
              type="success"
              :class="{ 'hover-tag': powers.some((item) => item == '004') }"
              :size="tablebtnSize"
              @click="
                statusConfirm(
                  scope.row.id,
                  scope.row.status,
                  powers.some((item) => item == '004')
                )
              "
              ><i
                class="el-icon-thumb"
                v-if="powers.some((item) => item == '004')
                "
                style="margin-right: 1px"
              ></i>启用</el-tag
            >
            <el-tag
              v-else
              :class="{ 'hover-tag': powers.some((item) => item == '004') }"
              :size="tablebtnSize"
              type="warning"
              @click="
                statusConfirm(
                  scope.row.id,
                  scope.row.status,
                  powers.some((item) => item == '004')
                )
              "
              ><i
                class="el-icon-thumb"
                v-if="powers.some((item) => item == '004')
                "
                style="margin-right: 1px"
              ></i>禁用</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="powers.some((item) => item == '005')"
              type="success"
              :size="tablebtnSize"
              @click="openModal(scope.row.id, false)"
            >
              修改
            </el-button>
            <!-- <el-button
            type="danger"
            :size="tablebtnSize"
            :disabled="scope.row.isAdmin === 1"
            @click="deleteById(scope.row.id)"
          >
            删除
          </el-button> -->
            <el-button
              v-if="powers.some((item) => item == '007')"
              type="warning"
              :size="tablebtnSize"
              @click="openModal(scope.row.id, true)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div
        v-show="count > 0"
        class="Pagination"
        style="text-align: right; margin-top: 10px"
      >
        <el-pagination
          :current-page="parmValue.page"
          :page-sizes="[5, 10, 20, 30, 40]"
          :page-size="parmValue.size"
          :size="searchSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="count"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </div>
      <add-edit
        :id="modelId"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      />
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import asyncRequest from "@/apis/service/interest/group";
import addEdit from "./addEdit";
import { mapGetters } from "vuex";
export default {
  name: "Group",
  components: {
    addEdit,
  },
  mixins: [mixinPage],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "group"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        loginName: "", // 账户
        fullName: "", // 姓名
        type: 2, // 用户类型（1运营人员 2物业人员）
        page: 1, // 页码
        size: 10, // 每页显示条数
      },
      count: 0, // 总条数
      tableData: [],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      this.parmValue = {
        loginName: "", // 账户
        fullName: "", // 姓名
        type: 2, // 用户类型（1运营人员 2物业人员）
        page: 1, // 页码
        size: 10, // 每页显示条数
      };
      this.searchList();
    },
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    async deleteById(id) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await asyncRequest.delete({ id });
          if (res.code === 0) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list(this.parmValue);
      if (res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map((e) => {
          e.userGroupList = e.userlist.join(",");
          e.addtime = e.addtime.replaceAll(".000", "");
          return e;
        });

        this.count = Number(res.data.count);
      } else {
        this.tableData = [];
        this.count = 0;
      }
      this.loading = false;
    },
    async statusConfirm(id, status, type) {
      if (!type) {
        return;
      }
      console.log(id, status);
      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("取消");
        });
    },
  },
};
</script>
   <style lang="scss" scoped>
.group {
  .infoLeft {
    margin-top: 5px;
  }
}
</style>
