<template>
  <div class="common-page qyzz page-t1">
   
    
    <div class="head-container">
      <!--表单组件-->
      <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-row>
           
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="phone">
                <el-input v-model="form.phone" />
              </el-form-item>
            </el-col>
          </el-row>
         
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="cancel">取消</el-button>
          <el-button  type="primary" @click="submitForm">确认</el-button>
        </div>
      </el-dialog>
      <!--工具栏-->
      <div class="panel-bottom page-row">
        <div class="ctin-box">
          <div class="content-within">
            <div class="content-fix">
              <div class="toolbar">
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <el-button
                      type="primary"
                      icon="el-icon-plus"
                      size="mini"
                      @click="handleAdd"
                    >新增</el-button>
                  </el-col>
                  
                  <el-col :span="1.5">
                    <el-button
                      type="danger"
                      icon="el-icon-delete"
                      size="mini"
                      :disabled="multiple"
                      @click="handleDelete"
                    >删除</el-button>
                  </el-col>
                </el-row>
                
              </div>
              <!--表格渲染-->
              <el-table ref="table" v-loading="loading" :data="dataList" size="small" style="width: 100%;" border @selection-change="handleSelectionChange">
                  <el-table-column type="selection" width="55" align="center"/>
                  <el-table-column label="姓名" align="center" prop="name" />
                  <el-table-column label="电话" align="center" prop="phone" />
                 
                  <el-table-column  label="操作" width="150px" align="center">
                    <template slot-scope="scope">
                      <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                    >删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
                <el-pagination
                  layout="total, prev, pager, next"
                  :current-page="queryParams.page"
                  @current-change="changePage"
                  @size-change="changeSize"
                  :total="total"
                  :page-size="10"
                >
                </el-pagination>
            </div>
          </div>
        </div>
      </div>
      
      
      <!-- <pagination /> -->
    </div>
  </div>
</template>

<script>
import {getPhone,addPhone,editPhone,delPhone} from '@/api/alarmMsg'

// import pagination from '@crud/Pagination'

export default {
  name: 'ToolQiniuConfig',
  // components: { pagination, crudOperation, rrOperation, udOperation },
 
  data() {
    return {
     // 遮罩层
     loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "a",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page:1,
        size: 10,
      },
      // 表单参数
      form: {},
      rules: {
        
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }    
    }
  },
  created(){
    this.getList()
  },
  methods: {
    reset() {
      this.form = {
        id:undefined,
        phone:undefined,
        name:undefined,
       
      };
    },
    getList(){
      getPhone({page:0,size:999}).then(res => {
        console.log(res)
        this.dataList = res.body.list
        this.total = res.body.total;
      })
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      console.log(this.ids)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      // const id = row.name 
      // getPhone({page:0,size:999,exact:{name:id}}).then(response => {
      //   // console.log(response.body.list[0])
      //   this.form = response.body.list[0];
      //   this.open = true;
      //   this.title = "修改信息";
      // });
      this.form = row;
      this.open = true;
      this.title = "修改信息";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editPhone(this.form).then(response => {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
            });
          } else {
            addPhone(this.form).then(response => {
              this.$message({
                message: '新增成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids;
    
      this.$confirm('是否确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delPhone(ids)
          return
        }).then(()=>{
          this.getList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
             
        });
    },
    changePage(page) {
      this.queryParams.page = page;
      this.getList();
    },
    changeSize(size){
      console.log(size)
      this.queryParams.size = size;
    },
    daterangeChange(val){
      console.log(val.join(','))
      this.queryParams.daterange=val.join(',');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.dateRange = []
      this.queryParams={
        page:1,
        size: 10,
        phone:null,
        name:null,
        daterange:null,
      }
      this.handleQuery();
    },
  }
}
</script>

<style scoped>

</style>
