<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"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Channel'
import PopupEdit from './components/popup/ChannelEdit'
import baseTable from '_m/baseTable.js'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'user.admdeptlist',
      searchForm: null,
      isDtlShow: false,
      curObj: {},
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
        item.nickname = item.leader_user ?  item.leader_user.nickname : item.nickname
        item.phone = item.leader_user ?  item.leader_user.phone : item.phone
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '渠道码', prop: 'dept_code' },
        { label: '渠道名称', prop: 'dept_name' },
        { label: '昵称', prop: 'nickname' },
        { label: '手机号', prop: 'phone' },
        { label: '更新时间', prop: 'update_at' },
        { label: '操作', width: 70, type: 'handle2', operations:
          [
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    delHandle(row) {
      this.$msg(`您确定要删除该渠道吗？`, 'confirm', () => {
        this.$api.user.admdeptdel({
          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 = 'admdeptedit'
        if (obj.id) apiStr = 'admdeptedit'
        this.$api.user[apiStr]({
          ...params
        }).then(data => {
          this.$msgs('修改成功')
          this.fetchData()
          this.isDtlShow = false
        })
      } else {
        this.isDtlShow = false
      }
    }
  }
}
</script>
