<template>
  <div class="app-container">
    <div v-if="!enumLoading">
      <AddModal ref="addModel" :dept-list="deptList" @success="modalSuccess" />
    </div>
    <!--操作区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      class="margin-bottom"
      @click="handleCreate"
    >添加账号</el-button>
    <!--搜索区-->
    <div class="float">
      <el-select
        v-model="page.data.adminId"
        filterable
        clearable
        placeholder="请选择账号"
        class="margin-left input"
      >
        <el-option
          v-for="item in accountList"
          :key="item.adminId"
          :label="item.username"
          :value="item.adminId"
        />
      </el-select>
      <el-input
        v-model="page.data.manageName"
        placeholder="管理员姓名"
        clearable
        class="margin-left input"
      />
      <el-input
        v-model="page.data.managePhone"
        placeholder="管理员手机号"
        clearable
        class="margin-left input"
      />
      <el-button
        type="primary"
        icon="el-icon-search"
        class="margin-left"
        @click="fetchData"
      >搜索</el-button>
    </div>
    <!--主表格-->
    <el-table
      ref="table_form"
      v-loading="enumLoading || listLoading"
      class="table"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="账号名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="所在部门" align="center">
        <template slot-scope="scope">
          {{ scope.row.deptName }}
        </template>
      </el-table-column>
      <el-table-column label="管理员姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.manageName }}
        </template>
      </el-table-column>
      <el-table-column label="管理员手机号 " align="center">
        <template slot-scope="scope">
          {{ scope.row.managePhone | desensitization(3, 7) }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="200">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope.row.adminId)"
          >删除</el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { belowList } from '@/api/user_manage'
import { authDelete, getAuthAccountPageList, getDeptList } from '@/api/authAccount'
import AddModal from './modal/AddModal.vue'
export default {
  components: { Pagination, AddModal },
  filters: {
    // 脱敏
    desensitization(string = '', start, end) {
      if (string) {
        var star = ''
        function makeStar(number) {
          if (number > 0) {
            star += '*'
            return makeStar(--number)
          } else {
            return star
          }
        }
        return (
          string.substring(0, start) +
          makeStar(end - start) +
          string.substring(end)
        )
      }
    }
  },
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      enumLoading: false, // 是否加载中
      listLoading: true,
      // 分页参数
      page: {
        data: {
          adminId: null,
          manageName: null,
          managePhone: null
        },
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      deptList: [],
      accountList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.selectInit()
    this.fetchData()
  },
  methods: {
    async selectInit() {
      this.enumLoading = true
      const deptAxios = await getDeptList()
      const belowAxios = await belowList()
      await Promise.all([
        deptAxios,
        belowAxios
      ]).then(res => {
        const deptAxiosRes = res[0]
        this.deptList = (deptAxiosRes.data || []).map(v => {
          v.deptId = String(v.deptId)
          return v
        })
        const belowAxiosRes = res[1]
        this.accountList = belowAxiosRes.data
      }).finally(() => {
        this.enumLoading = false
      })
    },
    // 获取列表数据
    fetchData() {
      this.listLoading = true
      getAuthAccountPageList(this.page).then((res) => {
        this.list = res.data.records
        this.total = res.data.total
        this.listLoading = false
      })
    },
    handleCreate() {
      this.$refs.addModel.open()
    },
    handleDelete(id) {
      this.$confirm('确定要删除此授权账号吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        authDelete(id).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    handleUpdate(row) {
      this.$nextTick(() => {
        this.$refs.addModel.open(Object.assign({}, row))
      })
    },
    clearPageParams() {
      this.page.current = 1
      this.page.pageSize = 10
    },
    modalSuccess(isEdit) {
      if (!isEdit) {
        this.clearPageParams()
      }
      this.fetchData()
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1
    }
  }
}
</script>
<style scoped lang="scss">
</style>

<style  lang="scss">
.unitType-select {
  width: 320px;
  .el-select-dropdown__item {
    height: auto;
    line-height: 1;
    .desc, .detail {
      line-height: 30px;
      white-space: normal;
      width: 100%;
    }
    .desc {
      font-size: 14px;
      padding-top: 5px;
      color: #333333;
    }
    .detail {
      padding-bottom: 5px;
      line-height: 20px;
      font-size: 12px;
      text-align: left;
      color: #909399;
    }
  }
}
</style>
