<template>
  <div class="app-container">
        <div>
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
        </div>
        <el-table v-loading="listLoading"
                  :data="eltList"
                  style="width: 100%;margin-top:10px;"
                  border
                  fit
                  stripe="true"
                  highlight-current-row
                  max-height="600">
          <el-table-column type="index" width="50" />
         
         
          <el-table-column align="center" label="评审内容">
            <template slot-scope="scope">
              {{ scope.row.content }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="260px">
            <template slot-scope="scope">
              <el-button type="primary"
                         size="small"
                         @click="handleUpdate(scope)">编辑</el-button>

              <el-button type="danger"
                         size="small"
                         @click="handleDelete(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
            <el-dialog :visible.sync="dialogVisible" :title="dialogType==='update'?'编辑':'新增'">
              <el-form ref="Form"
                       :model="evalutionsData"
                       label-width="80px"
                       label-position="right"
                      >
               
                <el-form-item label="评审项:" prop="content">
                  <el-input type="textarea" :rows="2" v-model="evalutionsData.content"
                            placeholder="请输入评审项"
                            clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
               
                
</el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirms('Form')">确认</el-button>
      </div>
    </el-dialog>
   
  </div>
</template>
<script>
import { getEvaluationsList, createEvaluations, updateEvaluations, deleteEvaluations} from "@/api/evaluationitem"
import Pagination from "@/components/Pagination"
import checkPermission from '@/utils/permission'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name:'evalutionsData',
  components: {Treeselect, Pagination},
   props:['evItem'],
  data() {
    return {
     evalutionsData: {
         id: null,
         content: null,
        
      },
       listQuery: {
         evItem: this.evItem,
      },
      
      eltList: [],
      listLoading: true,
     dialogVisible:false,
      dialogType:'create',
     
     
    };
  },
  
 created() {
     
    this.getList()
  },
  methods: {
    checkPermission,
    getList() {
     
      this.listLoading = true;
      getEvaluationsList(this.listQuery).then(response => {
        if (response.data) {
          this.eltList = response.data
         console.log(this.listQuery)
          
        }
        this.listLoading = false
      });
    },
     
   

   
    handleCreate() {
      this.evalutionsData= {
         id: null,
         content: null,
        evItem: this.evItem,
        

      },
      this.dialogType = 'create'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['Form'].clearValidate()
      })
    },
   handleUpdate(scope) {
      this.evalutionsData = Object.assign({}, scope.row) // copy obj
      this.dialogType = 'update'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['Form'].clearValidate()
      })
    },
handleDelete(scope){
   
  this.$confirm('确定删除本条数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
  }).then(() => {


     deleteEvaluations(scope.row.id).then(res=>{
        this.$notify({
                title: '删除成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
    })
         
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
},
   
   async confirms(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          const isEdit = this.dialogType === 'update'
          if (isEdit) {
             
            updateEvaluations(this.evalutionsData.id, this.evalutionsData).then(() => {
              this.getList()
              this.dialogVisible = false
              this.$notify({
                title: '成功',
                type: 'success',
                duration: 2000
              })
            })
          } else {

            
   
            createEvaluations(this.evalutionsData).then(res => {
              this.getList()
              this.dialogVisible = false
              this.$notify({
                title: '成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        } else {
          return false
        }
      })
    }
  }
};
</script>
