<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"
      @sort-change="sortChange"
      @currentChange="pageHandle"
      @sizeChange="sizeChange"
    />
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
    <point-dtl
      :isShow="isPDShow"
      :curObj="curObj"
      @close="closePDPopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/App'
import PopupEdit from './components/popup/AppIntegralEdit'
import PointDtl from './components/popup/AppIntegralDtl'
import baseTable from '_m/baseTable.js'
import xData from './mixin'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    PointDtl,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'user.admwechatuserlist',
      searchForm: null,
      isDtlShow: false,
      curObj: {},
      isPDShow: false,
      ...xData
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
        const fpUser = window.sessionStorage.getItem('fp_user') ? JSON.parse(window.sessionStorage.getItem('fp_user')) : {}
        if (fpUser.username === 'admin') {
          item.phones = item.phone
        } else {
          item.phones = item.phone.substring(0, 7) + '****'
        }
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '头像', prop: 'avatar', type: 'img' },
        { label: '昵称', prop: 'nickname' },
        { label: '成长值', prop: 'integral', sortable: 'custom' },
        { label: '手机号', prop: 'phones' },
        { label: '注册时间', prop: 'create_at' },
        { label: '禁言状态', prop: 'is_gag', type: 'tag', tags: arrToObj(this.$dictData.sys_yesno), tagTypeObj: {'2': 'success', '1': 'danger'} },
        { label: '销售', prop: 'is_sale', type: 'tag', tags: arrToObj(this.$dictData.sys_yesno), tagTypeObj: {'2': 'info', '1': 'primary'} },
        { label: '渠道', prop: 'is_partner', type: 'tag', tags: arrToObj(this.$dictData.sys_yesno), tagTypeObj: {'2': 'info', '1': 'primary'} },
        { label: '备注', prop: 'tag', type: 'input', width: 100},
        { label: '操作', width: 260, type: 'handle2', operations:
          [
            { label: '保存标签', func: this.saveHandle, btnType: 'primary' },
            { label: '成长值编辑', func: this.openPopup, btnType: 'success' },
            { label: '成长值明细', func: this.openPDPopup, btnType: 'success' },
            { labelFor: 'is_gag', func: this.disHandle,
              labelConfig: {
                texts: {
                  1: '解禁',
                  2: '禁言'
                },
                btnTypes: {
                  1: 'success',
                  2: 'danger'
                }
              }
            },
            { labelFor: 'is_sale', func: this.isSaleHandle,
              labelConfig: {
                texts: {
                  1: '销售取消',
                  2: '设成销售'
                },
                btnTypes: {
                  1: 'danger',
                  2: 'info'
                }
              }
            },
            { labelFor: 'is_partner', func: this.isPartnerHandle,
              labelConfig: {
                texts: {
                  1: '渠道取消',
                  2: '设成渠道'
                },
                btnTypes: {
                  1: 'danger',
                  2: 'info'
                }
              }
            },
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    openPDPopup(row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isPDShow = true
    },
    closePDPopup(obj) {
      this.isPDShow = false
      if (obj) {
        this.fetchData()
      }
    },
    openPopup(row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isDtlShow = true
    },
    closePopup(obj) {
      this.isDtlShow = false
      if (obj) {
        this.fetchData()
      }
    },
    isPartnerHandle (row) {
      const is_partner = Number(row.is_partner) === 1 ? 2 : 1
      const msgText = Number(row.is_partner) === 1 ? '取消渠道合伙人' : '设置成渠道合伙人---'
      this.$msg(`确定要${msgText}${row.nickname}吗？`, 'confirm', ()=> {
        this.$api.user.admwechatuserpartner({
          id: row.id,
          is_partner
        }).then(data => {
          this.$msgs(`${msgText}成功！`)
          this.fetchData()
        })
      }, null, true)
    },
    isSaleHandle (row) {
      const is_sale = Number(row.is_sale) === 1 ? 2 : 1
      const msgText = Number(row.is_sale) === 1 ? '取消销售' : '设置成销售置业经理---'
      this.$msg(`确定要${msgText}${row.nickname}吗？`, 'confirm', ()=> {
        this.$api.user.admwechatusersale({
          id: row.id,
          is_sale
        }).then(data => {
          this.$msgs(`${msgText}成功！`)
          this.fetchData()
        })
      }, null, true)
    },
    disHandle (row) {
      const is_gag = Number(row.is_gag) === 1 ? 2 : 1
      const msgText = Number(row.is_gag) === 1 ? '解禁' : '禁言'
      this.$msg(`确定要${msgText}${row.nickname}吗？`, 'confirm', ()=> {
        this.$api.user.admwechatusergag({
          id: row.id,
          is_gag
        }).then(data => {
          this.$msgs(`${msgText}成功！`)
          this.fetchData()
        })
      }, null, true)
    },
    delHandle (row) {
      this.$msg(`您确定要删除该用户吗？`, 'confirm', () => {
        this.$api.user.admwechatuserpurge({
          id: row.id,
        }).then(data => {
          this.$msgs(`已删除！`)
          this.fetchData()
        })
      })
    },
    saveHandle (row) {
      this.$msg(`您确定要保存标签吗？`, 'confirm', () => {
        this.$api.user.admwechatuseredit({
          id: row.id,
          tag: row.tag,
        }).then(data => {
          this.$msgs(`已添加！`)
          this.fetchData()
        })
      })
    },
  }
}
</script>
