<template>
  <div class="app-container">
    <search-form
      :list-loading="listLoading"
      @change="searchHandle"
    />
    <table-list
      :list-loading="listLoading"
      :data="tableData2"
      :columns="listConfig"
      :current-page="currentPage"
      :page-size="pageSize"
      :total-records="totalRecords"
      @currentChange="pageHandle"
      @sizeChange="sizeChange"
      :isAdd="true"
      @add="openPopup"
    />
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
    <pwd-reset
      :isShow="isPrShow"
      :curObj="curObj"
      @close="closePrPopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Index'
import PopupEdit from './components/popup/IndexEdit'
import PwdReset from './components/popup/PwdReset'
import baseTable from '_m/baseTable.js'
import xData from './mixin'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    PwdReset,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'user.admadminlist',
      searchForm: null,
      isDtlShow: false,
      isPrShow: false,
      curObj: {},
      ...xData
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: 'id', prop: 'id' },
        { label: '昵称', prop: 'nickname' },
        { label: '账号', prop: 'username' },
        // { label: '头像', prop: 'avatar', type: 'img' },
        { label: '角色', prop: 'role_name' },
        { label: 'wxId', prop: 'wx_user_id' },
        { label: '更新人', prop: 'update_by' },
        { label: '更新时间', prop: 'update_at' },
        { label: '操作', width: 200, type: 'handle2', operations:
          [
            { label: '编辑', func: this.openPopup, btnType: 'primary' },
            { label: '重置密码', func: this.openPrPopup, btnType: 'success' },
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    delHandle(row) {
      this.$msg(`您确定要删除该用户吗？`, 'confirm', () => {
        this.$api.user.admadmindel({
          id: row.id
        }).then(data => {
          this.$msgs(`已删除！`)
          this.fetchData()
        })
      }, null, true)
    },
    openPopup(row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isDtlShow = true
    },
    closePopup(obj) {
      if (obj) {
        const params = obj
        let apiStr = 'admadminadd'
        if (obj.id) apiStr = 'admadminedit'
        this.$api.user[apiStr]({
          ...params
        }).then(data => {
          this.$msgs(obj.id ? '编辑成功' : '新增成功')
          this.fetchData()
          this.isDtlShow = false
        })
      } else {
        this.isDtlShow = false
      }
    },
    openPrPopup(row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isPrShow = true
    },
    closePrPopup(obj) {
      this.isPrShow = false
      if (obj) {
        this.fetchData()
      }
    }
  }
}
</script>
