<template>
  <div class="app-container">
    <div style="margin-top:10px">
      <el-input
        v-model="listQuery.search"
        placeholder="输入手机号进行搜索"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleSearch"
      />
      <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleSearch"
          >搜索</el-button>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-refresh-left"
            @click="resetFilter"
          >刷新重置</el-button>
          <div style="margin-top:10px">
            <el-button type="primary" slot="reference" @click="handleAdd()">新增</el-button>
</div>
    </div>
<el-table
      :data="tableData"
      style="width: 100%;margin-top:10px;"
      border
      stripe
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="手机号">
        <template slot-scope="scope">{{ scope.row.phone }}</template>
      </el-table-column>
      <el-table-column label="验证码">
        <template slot-scope="scope">{{ scope.row.code }}</template>
      </el-table-column>
      <el-table-column label="发送时间">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
        <el-dialog :visible.sync="dialogVisible" title="新增发送记录" >
      <el-form :model="msg" label-width="80px" :rules="rule1" ref="Form">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="msg.phone" placeholder="手机号" />
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="msg.code" placeholder="验证码" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  getMsgList,
  createMsg
} from "@/api/msg";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";
const defaultmsg = {
        phone:null,
        msg:null
      };

const listQuery = {
  page: 1,
  limit: 20,
  search:''
};
export default {
  components: { Pagination },
  data() {
    return {
      total: 0,
      listQuery: listQuery,
      tableData: [],
      listLoading: true,
      msg:{},
      dialogVisible:false,
      rule1: {
        phone: [{ required: true, message: "请输入", trigger: "blur" }],
        code: [{ required: true, message: "请输入", trigger: "change" }],
      },
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getMsgList(this.listQuery).then(response => {
        this.tableData = response.data.results;
        this.total = response.data.count;
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = listQuery
      this.getList();
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList();
    },
    handleAdd() {
      this.msg = Object.assign({}, defaultmsg);
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    confirm(){
        createMsg(this.msg).then(res=>{
            this.$message.success('成功')
            this.dialogVisible = false
            this.listQuery = listQuery
            this.getList()
        })
    }
  }
};
</script>
