<template>
  <app-container>
    <search-area :need-clear="false" :need-search-more="false" :size="size" type="default" search-more-type="default" @search="search">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.keywords" :size="size" placeholder="关键字" clearable/>
      </search-item>
      <el-button slot="btns" class="filter-item" style="" size="small" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      <!--<el-button slot="btns" class="filter-item" style="" size="small" type="primary" icon="el-icon-delete" @click="del">删除</el-button>-->
    </search-area>
    <normal-table
      :data="list"
      :head="tableOption.head"
      :size="size"
      :query="listQuery"
      :total="total"
      :columns="columns"
      :list-loading="listLoading"
      :options="tableOption.options"
      :tools-option="tableOption.toolsOption">
      <template slot="columns">
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!--<el-button type="text" size="small" @click.stop="detail(scope.row)">详情</el-button>-->
            <!--<el-button type="text" size="small" @click.stop="edit(scope.row)">编辑</el-button>-->
            <el-button type="text" size="small" @click.stop="bind(scope.row)">绑定座席</el-button>
            <el-button type="text" size="small" @click.stop="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-phone ref="editdialog" @refresh="fetchData"/>
    <bind-seats ref="bindseat" @refresh="fetchData"/>
  </app-container>
</template>

<script>
import AppContainer from '@/components/layout/AppContainer'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import NormalTable from '@/components/NomalTable'
import { phoneList, delPhone } from '@/api/phone'
import EditPhone from './editPhone'
import BindSeats from './bindSeats'

export default {
  name: 'PhoneManage',
  components: { BindSeats, SearchArea, SearchItem, EditPhone, AppContainer, NormalTable },
  data() {
    return {
      modularId: '',
      status: '1',
      list: [],
      total: 0,
      listLoading: true, // 列表加载动画
      multipleSelection: [], // 多选选中项
      listQuery: {
        keywords: '',
        offset: 1,
        limit: 100,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '分机号',
          value: 'phonenumber',
          align: 'center'
        },
        {
          text: '已绑定座席',
          value: 'seats',
          align: 'center'
        },
        {
          text: '备注',
          value: 'message',
          align: 'center'
        }
      ], // 显示列
      tableOption: {
        head: {
          show: false, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true, // 是否需要序号列
          needMultipleSelection: false // 需要多选列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      },
      size: 'small'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      this.listLoading = true
      phoneList(this.listQuery).then(response => {
        if (response.code === 200) {
          // response.data.rows = [
          //   { 'id': '1', 'phoneNumber': '6042', 'seats': '张三', 'seatsIds': '1', 'message': '测试分机' }
          // ]
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }
      })
    },
    detail(row) {
      this.$refs.editdialog.initDialog('info', true, row)
    },
    // 添加分机
    add(row) {
      this.$refs.editdialog.initDialog('create', true, row)
    },
    // 编辑分机
    edit(row) {
      this.$refs.editdialog.initDialog('update', true, row)
    },
    // 绑定座席
    bind(row) {
      this.$refs.bindseat.initDialog(true, row)
    },
    // 删除分机
    del(row) {
      this.$confirm(
        '确定要删除该分机吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delPhone(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    watchChild() {
      console.log('watchChild')
      this.fetchData()
    }
  }
}
</script>

<style scoped>
  .btn-div{
    margin-bottom: 10px;
  }
</style>
