<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="团队名称"
        prop="name">
        <el-input v-model="form.name" clearable></el-input>
      </el-form-item>
      <el-form-item
        label="联系人"
        prop="contactName">
        <el-input v-model="form.contactName" clearable></el-input>
      </el-form-item>
      <el-form-item
        label="联系方式"
        prop="contactPhone">
        <el-input v-model="form.contactPhone" clearable></el-input>
      </el-form-item>
      <el-form-item
        label="备注"
        prop="remark">
        <el-input v-model="form.remark" clearable></el-input>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList(true)">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="openAddDialog(true)">
          新增团队
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        团队列表
      </div> -->
      <el-table
        border
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="name"
          label="团队名称">
        </el-table-column>
        <el-table-column
          prop="contactName"
          label="联系人">
        </el-table-column>
        <el-table-column
          prop="contactPhone"
          label="联系方式">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注">
        </el-table-column>
        <el-table-column
          width="150px"
          label="操作">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="openEditDialog(scope.row)"
            >
              编辑
            </el-link>

            <el-link
              type="primary"
              @click="deleteGroup(scope.row)">
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 弹框 -->

    <ElDialog
      :title="`${currentItem.id ? '编辑' : '新增'}团队`"
      width="500px"
      v-model="dialogVisible"
    >
      <el-form
        inline
        :model="currentItem"
        ref="form"
        class="form-wrap"
        label-width="150px"
        style="margin-top: 20px">
        <el-form-item
          label="团队名称"
          verify
          prop="name">
          <el-input v-model="currentItem.name"></el-input>
        </el-form-item>
        <el-form-item
          label="联系人"
          prop="contactName">
          <el-input v-model="currentItem.contactName"></el-input>
        </el-form-item>
        <el-form-item
          label="联系方式"
          prop="contactPhone">
          <el-input v-model="currentItem.contactPhone"></el-input>
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark">
          <el-input v-model="currentItem.remark"></el-input>
        </el-form-item>
      </el-form>
      <div style="margin: 20px; float: right;">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmOptGroup">确定</el-button>
      </div>
    </ElDialog>
  </div>
</template>

<script>
import { getTeamList, addTeam, updateTeam, deleteTeam } from '@/api/checker'
import ElDialog from '@/components/Dialog'

// import ConfigSetting from './deviceManage/ConfigSetting'
export default {
  data () {
    return {
      form: {
        name: '', // 名称
        pageNum: 1,
        pageSize: 10,
        contactName: '', // 联系人
        contactPhone: '', // 联系电话
        remark: '' // 备注
      },
      loading: false,
      tableData: [],
      total: 0,
      dialogVisible: false,
      configDialogVisible: false,
      currentItem: {}
    }
  },
  computed: {
    scenicList () {
      return this.$store.state.app.scenicList
    }
  },
  created () {
    this.getList()
  },
  components: {
    ElDialog
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    // 获取通道列表
    getList (goFirst) {
      let params = { ...this.form }

      goFirst && (params.pageNum = 1)
      this.loading = true
      getTeamList(params).then(res => {
        const list = res.data?.records || []
        this.total = res.data?.total || 0
        this.tableData = list
        this.loading = false
      })
    },
    // 打开弹框
    openAddDialog () {
      this.currentItem = {
        id: '',
        name: '',
        contactName: '',
        contactPhone: '',
        remark: ''
      }
      this.dialogVisible = true
    },
    openEditDialog (item) {
      this.currentItem = Object.assign({}, item)
      this.dialogVisible = true
    },
    deleteGroup (data) {
      this.$confirm('确定删除团队吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTeam(data.id).then(res => {
          this.$message.success('操作成功')
          this.getList()
        })
      }).catch(() => {})
    },
    confirmOptGroup () {
      const opt = this.currentItem.id ? updateTeam : addTeam
      opt(this.currentItem).then(res => {
        this.$message.success('操作成功')
        this.dialogVisible = false
        this.getList()
      })
    }
  }
}
</script>

<style scoped>

</style>
