<template>
  <div class="dataShare 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
              v-model="parmValue.loginName"
              :maxlength="40"
              placeholder="账户"
            ></el-input>
          </el-col>
          <el-col :span="3">
            <el-input
              v-model="parmValue.fullName"
              :maxlength="40"
              placeholder="姓名"
            ></el-input>
          </el-col> -->
          <!-- <el-col :span="3" style="width: 152px;">
            <el-button
              type="primary"
              @click="searchList"
              icon="el-icon-search"
            ></el-button>
            <el-button type="warning" @click="restSearch">
              重置
            </el-button>
          </el-col> -->
          <el-col :span="3" style="width: 235px; float: right">
            <el-button
            v-if="powers.some((item) => item == '002')"
              type="primary"
              :size="searchSize"
              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="actionlist"
          label="菜单数据"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="share_user"
          label="数据源用户"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="to_user"
          label="数据共享用户"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="to_group"
          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
          prop="addtime"
          label="创建时间"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-link
              v-if="powers.some((item) => item == '005')"
              type="primary"
              style="margin: 0 4px"
              :underline="false"
              @click="openModal(scope.row.id, false)"
            >
              修改
            </el-link>
            <!-- <el-link
              type="primary"
              style="margin:0 4px;"
               :underline="false"
            @click="deleteById(scope.row.id)"
          >
            删除
          </el-link> -->
            <el-link
              v-if="powers.some((item) => item == '007')"
              type="primary"
              style="margin: 0 4px"
              :underline="false"
              @click="openModal(scope.row.id, true)"
            >
              详情
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <div
        v-show="count > 0"
        class="Pagination"
        style="text-align: right; margin-top: 10px"
      >
        <el-pagination
          :size="searchSize"
          :current-page="parmValue.page"
          :page-sizes="[5, 10, 20, 30, 40]"
          :page-size="parmValue.size"
          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/dataShare";
import addEdit from "./addEdit";
import { mapGetters } from "vuex";
export default {
  name: "DataShare",
  components: {
    addEdit,
  },
  mixins: [mixinPage],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "dataShare"
        ) || {};
      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((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, type) {
      if (!type) {
        return;
      }
      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>
.dataShare {
}
</style>
