<template>
  <div>
    <div class="button-box clearflex">
      <el-button @click="dialogFormVisible=true" type="primary">新增</el-button>
    </div>

    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
			<el-form-item  label="手机号码" prop="phone">
			  <el-input placeholder="请输入手机号码" v-model="searchInfo.phone" type="text" class="user-form-item" clearable></el-input>
			</el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData"  border stripe>
      <el-table-column label="ID" width="70" prop="groupId">
      </el-table-column>
	  
      <el-table-column label="手机号码"  prop="name">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" class="edit-input" size="small" v-model="row.phone" />
          <span v-else>{{row.phone}}</span>
        </template>
      </el-table-column>

      <el-table-column label="考题组"  prop="marks">
        <template slot-scope="{row}">  
          <el-input v-if="row.edit" type="number" size="small" v-model="row.groupName" />
          <span v-else>{{row.groupName}}</span>
        </template>
      </el-table-column>
      
      <el-table-column
        align="center"
        width="160px"
        label="操作"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row, $index}">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="deleteRow(row, $index)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog title="新增题库" center :visible.sync="dialogFormVisible"  >
      <el-form :model="form" :rules="rules" label-width="120px" ref="elForm">
        <el-form-item  label="选择题组" prop="title" >
         <drop-search placeholder="请选择组" type="wxGroup" attr="title" @select="okPhone"/>
        </el-form-item>
		
        <el-form-item  label="手机号码" prop="phones">
			<el-input placeholder="多个手机用空格分割" v-model="form.phones" type="textarea"  clearable></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button @click="doAddAction" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWxUserGroupList, postData} from "@/api/exam.js";
import {toDatetime} from "@/utils/date"
import infoList from "@/mixins/infoList";
import { time2str, str2time} from "@/utils/index.js";
import DropSearch from '@/components/select/DropSearch.vue';

export default {
  name: "Api",
  data() {
    return {
	  listApi: getWxUserGroupList,
      dialogFormVisible: false,
      dialogTitle: "新增广告",
      boolVal:[0,1],
      form: {
		groupId:0,
		phones:""
      },
      page: 1,
      total: 10,
      pageSize: 10,
      tableData: [],
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
		counter: [{ required: true, message: "请输入题数", trigger: "blur" }],
        fromDate: [ { required: true, message: "请输入开始时间", trigger: "blur" }],
		toDate: [ { required: true, message: "请输入结束时间", trigger: "blur" }],
		duration: [ { required: true, message: "请输入时长", trigger: "blur" }],
      }
    };
  },
  watch:{
	"searchInfo.phone"(val){
		this.page = 1
		this.getTableData()
	} 
  },
  components:{DropSearch},
  mixins: [infoList],
  methods: { 
    doAddAction(){
		let param= { groupId: this.form.groupId}
		let phones = this.form.phones.split(/[\s \n]/)
		param.phones = phones.filter( (res)=> {return res.length == 11})
		console.log( "phones", phones)
        postData("AddWxGroupUser", param).then(data => {
          this.$message.successMsg("新增成功", 1)
          this.dialogFormVisible = false
          this.getTableData()
        });
    },
	okPhone( item){
		this.form.groupId = +item.group_id
	},
	deleteRow(row, index){
		let param= {id: row.id}
		postData('delWxUserGroup', param).then((res) => {
			if( res.code == 200){
				this.$message.successMsg("删除成功", 1)
				this.tableData.splice( index, 1)
			}
		})
	}
  },
  filters: {
    toDatetime
  },
  created() {
    this.getTableData();
  }
};
</script>
<style scoped lang="scss">
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>
