<template>
  <div>
	<div class="button-box clearflex">
	  <h1> 所有题库</h1>
	</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.title" type="text" class="user-form-item" clearable></el-input>
			</el-form-item>
	  </el-form>
	</div>

    <el-table :data="tableData"  border stripe v-if="isReloadData">
	   
      <el-table-column label="ID" width="70" prop="answerId">
      </el-table-column>
	  <el-table-column label="题型" width="70" prop="type">
		<template slot-scope="{row}">
		    <span>{{typeOption[row.type]}}</span>
		 </template>
	  </el-table-column>
	  
      <el-table-column label="题目"  prop="name">
        <template slot-scope="{row}">  
          <span>{{row.title}}</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 v-if="answers[row.answerId]">
				已经加入
			</el-button>
			
			<el-button v-else-if="row.isAdd">
				已经加入
			</el-button>
			
            <el-button v-else
              type="success"
              size="small"
              icon="el-icon-circle-check"
              @click="select(row, $index)"
            >加入题库 {{row.isAdd}}</el-button>
          </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="size"
      :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>
  </div>
</template>

<script>
import infoList from "@/mixins/infoList";
import {getWxAnswerList} from "@/api/exam.js";
export default {
  name: "Api",
  data() {
    return {
		isReloadData: true,
      listApi: getWxAnswerList,
      typeOption: { 1: "单选", 2: "多选", 3: "判断" },
    };
  },

  props: {
    answers: {
      type: Object,
      default: () =>{}
    },
  },
  watch:{
	"searchInfo.title"( val ){
		let lastnum = val[val.length - 1]
		if (val.length == 1) return;
		if (lastnum <= "z" && lastnum >= "A") return;
		this.getTableData()
	},
	answers:{
		handler(newName, oldName) {
			
		},
		immediate: true
	}
  },

  mixins: [infoList],
  methods: {
	reload(){
		console.log("reload")
		this.isReloadData = false;
		this.$nextTick(() => {
			this.isReloadData = true;
		})
	},
    select(row, index) {
      this.$emit("selectAnswer", row, this.reload);
    },
  },
  created() {
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.el-tag--mini {
  margin-left: 5px;
}
.warning {
  color: #dc143c;
}
</style>