<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"
      :operationsDefaultLength="6"
    />
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
    <pwd-edit
      :isShow="isPwdShow"
      :curObj="curObj"
      @close="closePwdPopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Index'
import PopupEdit from './components/popup/IndexEdit'
import PwdEdit from './components/popup/PwdEdit'
import baseTable from '_m/baseTable.js'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    PwdEdit,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'user.admuserlist',
      searchForm: null,
      isDtlShow: false,
      isPwdShow: false,
      curObj: {},
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
        if (!item.role_name) item.role_name = '---'
        let GTObj = arrToObj(this.$dictData.group_type)
        item.roleName = `${GTObj[item.group_type]}/${item.role_name}`
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '推荐人', prop: 'referrer' },
        { label: '渠道', prop: 'dept_name', fullShow: true },
        { label: '昵称', prop: 'nickname' },
        { label: '手机号', prop: 'phone' },
        { label: '角色', prop: 'roleName', fullShow: true, minWidth: 150 },
        { label: '身份认证', prop: 'auth_state', type: 'tag', tags: arrToObj(this.$dictData.auth_state), tagTypeObj: {'1': 'success', '2': 'warning', '3': 'danger', '4': 'info'}  }, 
        { label: '后台登录', prop: 'is_backend', type: 'tag', tags: arrToObj(this.$dictData.sys_yesno), tagTypeObj: {'1': 'success', '2': 'danger'} },
        { label: '绑定微信', prop: 'bind_wechat', type: 'tag', tags: arrToObj(this.$dictData.sys_yesno), tagTypeObj: {'1': 'success', '2': 'danger'} },
        { label: '类型', prop: 'user_type', type: 'flag', flags: arrToObj(this.$dictData.user_type)},
        { label: '楼盘角色', prop: 'manage_type', type: 'flag', flags: arrToObj(this.$dictData.manage_type) }, 
        { label: '关联楼盘', prop: 'estate_name' }, 
        { label: '更新时间', prop: 'update_at' },
        // { label: '系统', prop: 'is_admin', type: 'tag', tags: arrToObj(this.$dictData.sys_yesno), tagTypeObj: {'1': 'success', '2': 'danger'} },
        { label: '系统', prop: 'is_admin' },
        { label: '操作', width: 260, type: 'handle2', operations:
          [
            { label: '编辑', func: this.openPopup, btnType: 'primary' },
            { label: '删除', func: this.delHandle, btnType: 'danger' },
            { labelFor: 'is_admin', func: this.adminHandle,
              labelConfig: {
                texts: {
                  1: 'NO',
                  2: 'YES'
                },
                btnTypes: {
                  1: 'danger',
                  2: 'success'
                }
              }
            },
            { label: '重置密码', func: this.openPwdPopup, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    closePwdPopup (obj) {
      if (obj) {
        const params = obj
        let apiStr = 'admuserchangepwd'
        this.$api.user[apiStr]({
          ...params,
          user_id: this.curObj.id
        }).then(data => {
          this.$msgs('修改成功')
          this.fetchData()
          this.isDtlShow = false
        })
      } else {
        this.isDtlShow = false
      }
    },
    openPwdPopup (row) {
      this.curObj = row
      this.isPwdShow = true
    },
    adminHandle (row) {
      const is_admin = Number(row.is_admin) === 1 ? 2 : 1
      const msgText = Number(row.is_admin) === 1 ? '禁用' : '设置'
      this.$msg(`确定把${row.nickname}${msgText}为超级管理员吗？`, 'confirm', ()=> {
        this.$api.user.admuseradmin({
          id: row.id,
          is_admin
        }).then(data => {
          this.$msgs(`${msgText}成功！`)
          this.fetchData()
        })
      }, null, true)
    },
    delHandle(row) {
      this.$msg(`您确定要删除该用户吗？`, 'confirm', () => {
        this.$api.user.admuserdel({
          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 = 'admuseradd'
        if (obj.id) apiStr = 'admuseredit'
        this.$api.user[apiStr]({
          ...params,
        }).then(data => {
          this.$msgs(obj.id ? '编辑成功' : '新增成功')
          this.fetchData()
          this.isDtlShow = false
        })
      } else {
        this.isDtlShow = false
      }
    }
  }
}
</script>
