<!-- 企业列表 -->
<template>
  <div class="app-container">
    <el-container>
      <!-- 侧边栏 -->

      <el-main>
        <!-- 查询 -->
        <Conditions ref="conditions" :search-params.sync="searchParams" @search="search" />
        <!-- 表格 -->
        <Tables ref="dyTable" :tree-ids="treeIds" :search-params="searchParams" @rowOperation="rowOperation" />
        <!-- 弹窗 -->
        <Details ref="detail" />
        <!-- 查看弹窗 -->
        <SeeDetails ref="seeDetail" />
      </el-main>

    </el-container>
  </div>
</template>

<script>
import Tables from './components/table.vue'
import Conditions from './components/conditions.vue'
import Details from './components/detail.vue'
import SeeDetails from './components/seeDetail.vue'
import { getRegionTree } from '@/apiV2/common.js'
import { deleteCompany, disableCompany, enableCompany, resetCompanyPwd } from '@/apiV2/company'

export default {
  name: 'CustomerCompany',
  components: { Conditions, Tables, Details, SeeDetails },
  data() {
    return {
      global: {
        loading: true,
        companyids: '',
        companyid: '',
        projectid: '',
        dict: {
          status: { 0: '已停用', 1: '正常' }
        },
        regionTree: []
      },
      treeIds: '', // 侧边栏选中ID
      searchParams: {} // 查询(此处不赋值)
    }
  },
  provide() {
    return {
      global: this.global, // 全局数据对象
      reload: this.reload, // 重置
      refresh: this.refresh // 刷新
    }
  },
  created() {
    this.updateOptions()
  },
  methods: {
    /* common */
    reload() { // 重置搜索 回到第一页
      this.$refs.conditions.reset()
    },
    refresh() { // 不重置搜索 刷新当前页
      this.$refs.dyTable.getList(this.searchParams)
    },
    /* tree -> table */
    changeTree(e) {
      this.global.companyids = e.companyids
      this.global.companyid = e.companyid
      this.global.projectid = e.projectid
    },
    /* search -> table */
    search(params) { // 搜索
      this.searchParams = params
      this.$refs.dyTable.pageObj.current = 1
      this.$refs.dyTable.getList(this.searchParams)
    },
    /* table -> dialog */
    async rowOperation(row, idx, actionName, params) {
      if (['add', 'edit', 'see'].includes(actionName)) { // 增改查统一
        this.$refs['detail'].showDialog({
          mode: actionName,
          params,
          formData: row || {}
        })
      } else if (actionName === 'delete') { // 删除
        const txt = `<p class="delText">您确定要删除【${row.name}】吗？</p>`
        await this.$confirm(txt, '温馨提示', {
          dangerouslyUseHTMLString: true,
          type: 'warning'
        })
        try {
          this.loading = true
          const res = await deleteCompany(row.id)
          if (res.success) {
            this.reload()
          }
        } finally {
          this.loading = false
        }
      } else if (actionName === 'toggleStatus') { // 停用/启用
        const doDisable = row.status !== 0
        const actionNameString = doDisable ? '停用' : '启用'
        const txt = `<p class="delText">确定要${actionNameString}企业【${row.name}】吗？</p>`
        await this.$confirm(txt, '温馨提示', {
          dangerouslyUseHTMLString: true,
          type: 'warning'
        })
        try {
          this.loading = true
          let res = {}
          if (doDisable) {
            res = await disableCompany(row.id)
          } else {
            res = await enableCompany(row.id)
          }
          if (res.success) {
            this.refresh()
          }
        } finally {
          this.loading = false
        }
      } else if (actionName === 'resetPassword') { // 重置密码
        const txt = `<p class="delText" style="text-align:center;">确定要重置密码吗？</p>`
        await this.$confirm(txt, '温馨提示', {
          dangerouslyUseHTMLString: true,
          type: 'none'
        })
        try {
          this.loading = true
          let res = {}
          res = await resetCompanyPwd({ id: row.id })
          if (res.success) {
            const txt = `
            <p class="delText">密码已重置，初始密码为：${res.data}</p>
            <p class="delText">已发送至企业邮箱和联系人手机</p>
            `
            this.$confirm(txt, '温馨提示', {
              dangerouslyUseHTMLString: true,
              showCancelButton: false,
              confirmButtonText: '我知道了',
              type: 'none'
            })
            this.reload()
          }
        } finally {
          this.loading = false
        }
      }  else { // 表格其它操作
        this.$refs[actionName].showDialog({
          params,
          formData: row || {}
        })
      }
    },
    async updateOptions() {
      // 获取省市区
      const res = await getRegionTree()
      if (res.success) {
        this.global.regionTree = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
