<template>
  <div class="app-container">
   
    <div style="margin-top:6px">
      <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
    </div>
    <el-table v-loading="listLoading"
              :data="detectoryList.results"
              style="width: 100%;margin-top:10px;"
              border
              fit
              stripe
              highlight-current-row
              max-height="600">
      <el-table-column type="index" width="50" />
      <el-table-column label="实验室编号">
        <template slot-scope="scope">
          {{ scope.row.code }}
        </template>
      </el-table-column>
      <el-table-column label="实验室名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="通讯地址">
        <template slot-scope="scope">
          {{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="联系人姓名">
        <template slot-scope="scope">
          {{ scope.row.contactman }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="联系人电话">
        <template slot-scope="scope">
          {{ scope.row.ontactManTel }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="联系人手机">
        <template slot-scope="scope">
          {{ scope.row.contactmanmoblie }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="登录名">
        <template slot-scope="scope">
          {{ scope.row.loginname }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="密码">
        <template slot-scope="scope">
          {{ scope.row.password }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="是否自有">
        <template slot-scope="scope">
          {{ scope.row.iszy==true?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="备注">
        <template slot-scope="scope">
          {{ scope.row.note }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-edit"
                     :disabled="!checkPermission(['testorg_update'])"
                     @click="handleUpdate(scope)" />
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     :disabled="!checkPermission(['testorg_delete'])"
                     @click="handleDelete(scope)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="detectoryList.count>0"
                :total="detectoryList.count"
                :page.sync="listQuery.page"
                :limit.sync="listQuery.page_size"
                @pagination="getList" />
    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='update'?'编辑标准':'新增标准'">
      <el-form ref="Form"
               :model="standard"
               label-width="100px"
               label-position="right"
               :rules="rule1">

        <el-form-item label="实验室编号" prop="code">
          <el-input v-model="standard.code" placeholder="编号" />
        </el-form-item>
        <el-form-item label="实验室名称" prop="name">
          <el-input v-model="standard.name" placeholder="名称" />
        </el-form-item>
        <el-form-item label="通讯地址" prop="address">
          <el-input v-model="standard.address" placeholder="通讯地址" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactman">
          <el-input v-model="standard.contactman" placeholder="联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="ontactManTel">
          <el-input v-model="standard.ontactManTel" placeholder="联系人电话" />
        </el-form-item>
        <el-form-item label="联系人手机" prop="contactmanmoblie">
          <el-input v-model="standard.contactmanmoblie" placeholder="联系人手机" />
        </el-form-item>
        <el-form-item label="登录名" prop="loginname">
          <el-input v-model="standard.loginname" placeholder="登录名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="standard.password" placeholder="密码" />
        </el-form-item>
        <el-form-item label="自有" prop="iszy">
          <el-switch v-model="standard.iszy"
                     active-color="#13ce66"
                     inactive-color="#8C969D">
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input type="textarea"
                    :rows="2"
                    placeholder="备注"
                    v-model="standard.note">
          </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="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { getTestOrgList, createTestOrg, updateTestOrg, deleteTestOrg } from "@/api/laboratory"
 
  import Pagination from "@/components/Pagination"
  import checkPermission from '@/utils/permission'
  const defaultStandard = {
    id: null,
    code: null,
    name: null,
    address: null,
    contactman: null,
    ontactManTel: null,
    contactmanmoblie: null,
    loginname: null,
    password: null,
    iszy: false,
    note:null
  }
  export default {
    components: { Pagination },
    data() {
      return {
        detectoryList: { count: 0 },
        standard: Object.assign({}, defaultStandard),
        listLoading: true,
        listQuery: {
          page: 1,
          page_size: 20
        },
        
        dialogVisible: false,
        dialogType: 'create',
        rule1: {
         
        },
        fileList: []
      };
    },
    created() {
      this.getList();
    },
    methods: {
      checkPermission,
      getList() {
        this.listLoading = true;
        getTestOrgList(this.listQuery).then(response => {
          if (response.data) {
            this.detectoryList = response.data
          }
          this.listLoading = false;
        });
      },
      resetFilter() {
        this.listQuery = {
          page: 1,
          page_size: 20
        };
        this.getList();
      },
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },
      handleCreate() {
        this.standard = Object.assign({}, defaultStandard);
       
        this.dialogType = "create";
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs["Form"].clearValidate();
        });
      },
      handleUpdate(scope) {
        this.standard = 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(() => {
          deleteTestOrg(scope.row.id).then(res=>{
        this.$message.success('删除成功')
              this.getList()
    })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
},
 
    
      async confirm(form) {
        this.$refs[form].validate(valid => {
        
          if (valid) {
            const isEdit = this.dialogType === "update";
            if (isEdit) {
              updateTestOrg(this.standard.id, this.standard).then(res => {
               
                  this.getList();
                  this.dialogVisible = false
                  this.$message.success('成功')
                
              });
            } else {
              createTestOrg(this.standard).then(res => {
               
                this.getList();
                this.dialogVisible = false
                this.$message.success('成功')

              }).catch(error => { })
            }
          } else {
            return false;
          }
        });
      }
    }
  };
</script>
