<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" size="small" @click="centerDialogVisible = true">新增</el-button>
      <el-button type="primary" size="small" @click="deleteF()">删除</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column  label="组织名称" width="300">
        <!-- prop="organizationName" -->
        <template slot-scope="scope">
          <span>{{ scope.row.organizationName | OrganizationNameFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="编码" width="300"></el-table-column>
      <el-table-column
        prop="personName"
        label="姓名"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="createMan"
        label="创建人名字"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        min-width="300"
      ></el-table-column>
    </el-table>
    <el-dialog
      title="增加申请人"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
      @open="openDialog()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名" required>
          <el-input v-model="form.PersonName"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" required>
          <el-input v-model="form.Phone"></el-input>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input v-model="form.CreateMan"></el-input>
        </el-form-item>
        <el-form-item label="人员类型" required>
          <el-select v-model="form.isIn" placeholder="请选择" style="width:100%">
            <el-option label="内部人员" value="1"></el-option>
            <el-option label="非内部人员" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="form.CreateTime"
            type="datetime"
            placeholder="选择日期时间"
            style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人">
          <el-input v-model="form.ModifyMan"></el-input>
        </el-form-item>
        <el-form-item label="修改时间">
          <el-date-picker
          v-model="form.ModifyTime"
          type="datetime"
          placeholder="选择日期时间"
          style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="组织名称">
          <!-- <el-input v-model="form.OrganizationName"></el-input> -->
          <el-select v-model="form.OrganizationName" placeholder="请选择" style="width:100%">
            <el-option label="养猪组织" value="1"></el-option>
            <el-option label="养鱼组织" value="2"></el-option>
            <el-option label="养花组织" value="3"></el-option>
            <el-option label="养羊组织" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit"
          >确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="删除"
      :visible.sync="dialogVisibled"
      width="30%">
      <span>确认要删除选中的内容吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibled = false">取 消</el-button>
        <el-button type="primary" @click="handleDelete(requestdata)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getApplyManList,addNewApplyMan,delAppyMan } from "@/api/apply/apply"
export default {
  data() {
    return {
      dialogVisibled:false,
      centerDialogVisible: false,
      tableData: [
      ],
      form: {
          PersonName:'',
          Phone:'',
          CreateMan:'',
          isIn:'',
          CreateTime:'',
          ModifyTime:'',
          ModifyMan:'',
          OrganizationName:''
      },
      multipleSelection: [],
      requestdata:""
    };
  },
  methods: {
      onSubmit() {
        addNewApplyMan(this.form).then(res=>{
          console.log(res)
          this.centerDialogVisible = false
          this.getlist()
        })
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach((row) => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log(this.getParams(this.multipleSelection))
        this.requestdata = this.getParams(this.multipleSelection)
      },
      getParams(arr){
        let str = ''
        arr.forEach(item=>{
          str = str + item.code +"__,__"
        })
        return str.substring(0,str.length-5)
      },
      getlist(){
        getApplyManList().then(res=>{
          console.log(res.data.data)
          this.tableData = res.data
        })
      },
      deleteF(){
        console.log(this.requestdata)
        if(this.multipleSelection.length===0){
          this.$message.error("请选择要删除的内容！");
        }else{
          this.dialogVisibled = true
        }
      },
      handleDelete(data){
        delAppyMan(data).then(res=>{
          console.log(res)
          this.dialogVisibled = false
          this.getlist()
        })
      },
      openDialog(){
        this.form= {
          PersonName:'',
          Phone:'',
          CreateMan:'',
          isIn:'',
          CreateTime:'',
          ModifyTime:'',
          ModifyMan:'',
          OrganizationName:''
        }
      }
  },
  mounted(){
    this.getlist()
  },
  filters:{
    OrganizationNameFilter(val){
      if(val==="1"){
        return "养猪组织"
      }else if(val==="2"){
        return "养鱼组织"
      }else if(val==="3"){
        return "养花组织"
      }else{
        return "养羊组织"
      }
    }
  }
};
</script>

<style>
/* .el-dialog{
  z-index: 9999;
} */
</style>