<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加部门
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{child: 'child', haschild: 'child'}">
      <el-table-column
        prop="deptName"
        label="部门名称"
        width="180">
      </el-table-column>
      <el-table-column
        label="负责人"
        prop="leader"
      ></el-table-column>
      <el-table-column
        label="联系电话"
        prop="phone"
      ></el-table-column>
      <el-table-column
        label="排序"
        prop="sortNum"
      >
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.status==1" type="danger">
            停用
          </el-tag>
          <el-tag v-else="row.status==0" type="success">
            正常
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
      >
        <template slot-scope="{row}">
          <span v-if="row.createTime != null || row.createTime != '' ">{{row.createTime | getTime}}</span>

        </template>
      </el-table-column>
            <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
              <template slot-scope="{row}">
                <template v-if="webType">
                  <template v-if="!isChoose">
                <el-button @click="handleUpdate(row)" type="primary" size="mini">
                  编辑
                </el-button>
                <el-button v-if="row.parentId == 0" @click="handleCreate(row)" size="mini" type="success">
                  新增
                </el-button>
                <el-button @click="editStatus(row.id,'0')" v-if="row.status==1" size="mini">
                  启用
                </el-button>
                <el-button @click="editStatus(row.id,'1')" type="danger" v-else="row.status==0" size="mini">
                  停用
                </el-button>
                  </template>
                  <template v-else>
                    <el-button @click="sourceType == 1? chooseThisTo(row):chooseThis(row)" type="primary" size="mini">
                      选择
                    </el-button>
                  </template>
                </template>
                <template v-else>
                  <el-button @click="ERPChoose(row)" type="primary" size="mini">
                    选择
                  </el-button>
                </template>

              </template>
            </el-table-column>
    </el-table>
    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :parentId="parentId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { getDeptList, getMenuInfo } from '@/api/system/department.js'
import { getMenu } from '@/utils/getTable.js'
import Servicedialog from './Servicedialog.vue'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'

export default {
  components: {
    Cancledialog,
    Servicedialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      webType: true,
      dialogStatus: '',
      titleDialog: '',
      dialogVisible: false,
      confirmdialog: false,
      listLoading: false,
      tableData: [],
      parentId: 0
    }
  },
  mounted() {
    this.getHomeInfo()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {
    handleCreateAdd() {
      // 增加系统目录
      const _that = this
      _that.titleDialog = '增加部门'
      _that.dialogStatus = 'createClient.vue'
      _that.parentId = 0
      _that.$refs.menuDialog.createInfoAdd()
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getHomeInfo()
    },
    handleUpdate(row) {
      console.log(row)
      const _that = this
      _that.dialogStatus = 'update'

      _that.parentId = row.parentId
      // console.log(_that.menuId)
      _that.titleDialog = '修改部门'
      _that.$refs.menuDialog.getInfo(row.id)
    },
    handleCreate(row) {
      const _that = this
      _that.parentId = row.id
      _that.dialogStatus = 'createClient.vue'
      _that.titleDialog = '增加部门'
      _that.$refs.menuDialog.createInfo(row)
    },
    getHomeInfo() {
      const _that = this
      _that.listLoading = true
      getDeptList().then(response => {
        console.log(response)
        const data = response.data
        // console.log('getMenuList', data)
        const menuData = getMenu(data)
        // console.log(menuData)
        // console.log(menuData)
        // //  组合表单数据
        _that.tableData = menuData
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },
    // 启用禁用
    editStatus(id, status) {
      const _that = this
      const printerTypeInfo = {
        id: id,
        status: status
      }
      const params = {
        'dept': JSON.stringify(printerTypeInfo)
      }

      const url = '/system/voidDept'
      _that.$refs.confirmDialog.updataClick(params, url)
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.deptName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
</style>
