<template>
<div>
<el-form :inline="true" :model="listQuery" style="margin-top:25px;margin-left:2%">
  <el-form-item label="名称">
    <el-input v-model="listQuery.name" placeholder="名称"></el-input>
  </el-form-item>
  <el-form-item label="号码">
    <el-input v-model="listQuery.tel" placeholder="号码" oninput="value=value.replace(/[^\d]/g,'')"></el-input>
  </el-form-item>
  <el-form-item label="备注">
    <el-input v-model="listQuery.remark" placeholder="备注"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="onSubmit">查询</el-button>
  </el-form-item>
  <el-form-item>
    <el-button type="info" @click="empty">清空</el-button>
  </el-form-item>

</el-form>
  <el-row>
  <el-button type="success" round style="margin-left:2%" @click="addPhone">添加</el-button>
  <el-button type="warning" round style="margin-left:2%"  @click="editPhone">修改</el-button>
  <el-button type="danger" round style="margin-left:2%" @click="delPhone">删除</el-button>
  </el-row>
<div style="margin-top:25px;">
  <el-table
    v-loading="listLoading"
      ref="multipleTable"
      :data="tableData"
      border
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
     prop="name"
      label="名称"
      width="180">
    </el-table-column>
    <el-table-column
      prop="tel"
      label="号码"
      width="180">
    </el-table-column>
    <el-table-column
      prop="remark"
      label="备注">
    </el-table-column>
    <el-table-column
      prop="userUame"
      label="编辑人">
    </el-table-column>
    <el-table-column
      prop="updateDate"
      label="编辑时间">
      <template slot-scope="scope">{{ formatDate(scope.row.updateDate) }}</template>
    </el-table-column>
  </el-table>
  <div class="footer-page">
      <el-pagination
        :current-page.sync="listQuery.pageNum"
        :page-sizes="[10, 20, 40, 100]"
        :page-size.sync="listQuery.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
</div>
</div>
</template>

<script>
export default {

}
</script>

<style>

</style>

<script>
  export default {
    data() {
      return {
        listQuery: {
          user: '',
          region: '',
          tel: '',
          remark: '',
          pageNum: 1,
          pageSize: 10,
          allId: [],
        },
        total: 0, // 总数
        tableData: [],
        selectId: [], // 表格选择框
        listLoading: false // 表格加载框
      }
    },
    mounted() {
    this.getPhoneList()
  },
    methods: {
    getPhoneList() { // 查询Role列表
      this.listLoading = true
      this.$store.dispatch('GetPhoneList', this.listQuery).then((response) => {
          this.tableData = response.data.item
          this.total = response.data.total
          this.listLoading = false
          return
        this.listLoading = false
      }).catch(() => {
        console.log('error GetPhoneList')
        this.listLoading = false
      })
    },

      onSubmit() {
       this.getPhoneList()
      },
      // 表格批量选择
      handleSelectionChange(rows) {
      this.selectId = []
      for (const i in rows) {
        this.selectId.push(rows[i].id)
      }
    },
     handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.listQuery.pageSize = val
      this.getPhoneList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.listQuery.pageNum = val
      console.log('val',val)
      this.getPhoneList()
    },
    // 时间
    formatDate(val) {
      if (val === null) {
        return
      }
      var value = new Date(val)
      var year = value.getFullYear()
      var month = value.getMonth() + 1
      var day = value.getDate()
      var hour = value.getHours()
      var minutes = value.getMinutes()
      var seconds = value.getSeconds()
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
      return year + '-' + month + '-' + day
    },
    addPhone(){
         this.$router.push({ name: 'phoneconfig-phoneconfigAdd'})
    },
    editPhone(){
        const ids = this.selectId[0]
        if(this.selectId.length > 1){
            this.$message.error("只能选择一条数据进行修改");
            this.listLoading = false
            return
        }
        if(this.selectId.length < 1){
            this.$message.error("请选择一条数据进行修改");
            this.listLoading = false
            return
        }
        this.$router.push({ name: 'phoneconfig-phoneedit', query: { id: ids }})
    },
    delPhone(){
         this.listQuery.allId = this.selectId
        if(this.selectId.length < 1){
            this.$message.error("请至少选择一条数据");
            this.listLoading = false
            return
        }
        console.log('this.listQuery.allId')
        this.$store.dispatch('Operatedel', this.listQuery).then((res) => {
        if(res.code == '0'){
         this.$message({
          message: '成功',
          type: 'success'
            });
         this.getPhoneList()
       }       
      }).catch(() => {
        console.log('error Operatedel')
      })
    },
    empty(){
        this.listQuery.name = ''
        this.listQuery.remark = ''
        this.listQuery.tel = ''
        this.listQuery.pageNum = 1
        this.listQuery.pageSize = 10
        this.getPhoneList()
    }
  }
}
</script>
<style>
.footer-page {
  display: flex;
  justify-content: flex-end;
}
</style>
