<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 prop="Code" label="编码" width="300"></el-table-column>
      
      <el-table-column
        prop="ApplyMan"
        label="申请人姓名"
        width="200"
      ></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="ApplyTime"
        label="申请日期"
        min-width="300"
      ></el-table-column>
      <el-table-column
        prop="IsolationTime"
        label="隔离日期"
        min-width="300"
      ></el-table-column>
      <el-table-column
        prop="IsolationPiontName"
        label="隔离点编码"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="SamplingBatch"
        label="采样批次"
        width="200"
      ></el-table-column>
    </el-table>
    <el-dialog
      title="增加申请人"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
      @open="openDialog()"
    >
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="组织名称" required>
          <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-item label="申请人">
          <el-input v-model="form.ApplyMan"></el-input>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input v-model="form.CreateMan"></el-input>
        </el-form-item>
        <el-form-item label="审核人">
          <el-input v-model="form.Checker"></el-input>
        </el-form-item>
        <el-form-item label="申请日期">
          <el-date-picker
            v-model="form.ApplyTime"
            type="datetime"
            placeholder="选择日期时间"
            style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="隔离日期">
          <el-date-picker
          v-model="form.IsolationTime"
          type="datetime"
          placeholder="选择日期时间"
          style="width:100%">
        </el-date-picker>
        </el-form-item>
        <el-form-item label="隔离点关联" required>
          <el-select v-model="form.IsolationPiontName" placeholder="请选择" style="width:100%">
            <el-option v-for="data in IsolationPiont" :key="data.Code" :label="data.label" :value="data.Code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采样批次">
          <el-input v-model="form.SamplingBatch"></el-input>
        </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 {delAdmissions,getAdmissionsList,addAdmissions,getIsolationpointsList} from "../../api/apply/apply"
export default {
  data() {
    return {
       centerDialogVisible: false,
      dialogVisibled:false,
      tableData: [
        // {
        //   Code: "WSFJGFJDS-SDFJGSH5256",
        //   SamplingBatch:"第一批",
        //   ApplyMan: "程雪鹏",
        //   OrganizationName: "应用开发室",
        //   ApplyTime: "2022-2-4 19:45:45",
        //   IsolationTime: "2022-2-4 19:45:45"
        // },
        // {
        //   Code: "WSFJGFJDS-SDFJGSH5256",
        //   SamplingBatch:"第一批",
        //   ApplyMan: "程雪鹏",
        //   OrganizationName: "应用开发室",
        //   ApplyTime: "2022-2-4 19:45:45",
        //   IsolationTime: "2022-2-4 19:45:45"
        //   //   1可用 2 不可用
        // },
        // {
        //   Code: "WSFJGFJDS-SDFJGSH5256",
        //   SamplingBatch:"第一批",
        //   ApplyMan: "程雪鹏",
        //   OrganizationName: "应用开发室",
        //   ApplyTime: "2022-2-4 19:45:45",
        //   IsolationTime: "2022-2-4 19:45:45"
        //   //   1可用 2 不可用
        // },
        // {
        //   Code: "WSFJGFJDS-SDFJGSH5256",
        //   SamplingBatch:"第一批",
        //   ApplyMan: "程雪鹏",
        //   OrganizationName: "应用开发室",
        //   ApplyTime: "2022-2-4 19:45:45",
        //   IsolationTime: "2022-2-4 19:45:45"
        // },
      ],
      form: {
          OrganizationName:'',
          ApplyMan:'',
          CreateMan:'',
          Checker:'',
          ApplyTime:'',
          IsolationTime:'',
          IsolationPiontName:'',
          SamplingBatch:''
      },
      multipleSelection: [],
       requestdata:"",
       IsolationPiont:[]
    };
  },
  methods: {
      onSubmit() {
        // console.log('submit!');
        addAdmissions(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();
        }
      },
      getlist(){
        getAdmissionsList().then(res=>{
          this.tableData = res.data
        })
      },
      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)
      },
      deleteF(){
        console.log(this.requestdata)
        if(this.multipleSelection.length===0){
          this.$message.error("请选择要删除的内容！");
        }else{
          this.dialogVisibled = true
        }
      },
      handleDelete(data){
        delAdmissions(data).then(res=>{
          console.log(res)
          this.dialogVisibled = false
          this.getlist()
        })
      },
      openDialog(){
        this.form = {
          OrganizationName:'',
          ApplyMan:'',
          CreateMan:'',
          Checker:'',
          ApplyTime:'',
          IsolationTime:'',
          IsolationPiontName:'',
          SamplingBatch:''
        }
        getIsolationpointsList().then(res=>{
          console.log(res.data)
          const arr = res.data.map(item=>{
            return {"label":item.IsolationPiontName,"value":item.IsolationPiontName,"Code":item.Code}
          })
          console.log(arr)
          this.IsolationPiont = arr
        })
      }
  },
  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>