<template>
  <div class="account 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="4">
            <el-select
              :size="searchSize"
              v-model="parmValue.status"
              filterable
              clearable
              placeholder="账号状态"
              style="width: 100%"
            >
              <el-option
                v-for="item in statusList"
                :key="'status' + item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-col>
          <el-col :span="4">
            <el-input
              :size="searchSize"
              v-model="parmValue.name"
              :maxlength="40"
              placeholder="业务员姓名"
            />
          </el-col>
          <el-col :span="4">
            <el-input
              :size="searchSize"
              v-model="parmValue.username"
              :maxlength="40"
              placeholder="账号"
            />
          </el-col>
          <el-col :span="4" style="width: 152px">
            <el-button
              :size="searchSize"
              type="primary"
              icon="el-icon-search"
              @click="searchList"
            />
            <el-button type="warning" v-if="powers.some((item) => item == '024')"  :size="searchSize" @click="restSearch">
              重置
            </el-button>
          </el-col>
          <el-col :span="3" style="width: 195px; 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')"
              type="success"
              :size="searchSize"
              style="float: right"
              @click="openModal('add', false)"
            >
              添加
            </el-button>
          </el-col>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        :size="size"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="username"
          label="账号"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="name"
          label="业务员姓名"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="role_name"
          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 == '020')"
              type="primary"
              style="margin: 0 4px"
              :underline="false"
              :size="tablebtnSize"
              @click="openPasswordModal(scope.row.id, false)"
            >
              重置密码
            </el-link>
            <el-link
              v-if="powers.some((item) => item == '005')"
              type="primary"
              :underline="false"
              style="margin: 0 4px"
              :size="tablebtnSize"
              @click="openModal(scope.row.id, false)"
            >
              修改
            </el-link>
            <!-- <el-link
            type="primary"
            style="margin:0 4px;"
             :underline="false"
            :size="tablebtnSize"
            @click="deleteById(scope.row.id, scope.row.status)"
          >
            删除
          </el-link> -->
            <el-link
              v-if="powers.some((item) => item == '007')"
              type="primary"
              style="margin: 0 4px"
              :underline="false"
              :size="tablebtnSize"
              @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
          :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"
      />

      <reset-password
        :id="passwordModelId"
        :show-model="passwordModel"
        :is-detail="isPasswordDetail"
        @refresh="searchList"
        @cancel="passwordModel = false"
      />
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import asyncRequest from "@/apis/service/interest/account";
import addEdit from "./addEdit";
import resetPassword from "./resetPassword";
import statusList from "@/assets/js/statusList";
import { mapGetters } from "vuex";
export default {
  name: "Account",
  components: {
    addEdit,
    resetPassword,
  },
  mixins: [mixinPage],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "account"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      statusList: statusList,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        name: "", // 业务员名字
        username: "", // 账号
        status: "", //
        page: 1, // 页码
        size: 10, // 每页显示条数
      },
      count: 0, // 总条数
      tableData: [],
      passwordModel: false,
      passwordModelId: 0,
      isPasswordDetail: false,
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      this.parmValue = {
        name: "", // 业务员名字
        username: "", // 账号
        status: "", //
        page: 1, // 页码
        size: 10, // 每页显示条数
      };
      this.searchList();
    },
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    openPasswordModal(id, isDetail) {
      this.passwordModel = true;
      this.passwordModelId = id;
      this.isPasswordDetail = isDetail;
    },

    async deleteById(id, status) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            id: id,
            status: status === "1" ? "0" : "1",
          };
          const res = await asyncRequest.status(model);
          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;
            // this.tableData = [];
            // this.count = 0;
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
  },
};
</script>
   <style lang="scss" scoped>
.account {
}
</style>
