<template>
  <div class="AddPlanWrapper clearfix">
    <crumbs title="子医院维护" class="topBack"></crumbs>
    <div class="AddPlanBox">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form ref="form" :model="form">
            <el-form-item>
              <el-table
                size="mini"
                :data="form.hospitalRelationVOList"
                border
                :style="form.hospitalRelationVOList.length > 0?'min-height: 200px':'60px'"
                :height="form.hospitalRelationVOList.length > 0?'calc(100vh - 260px)':'60px'"
                class="tabs"
                ref="tableList"
                :header-row-style="{height:'40px',cursor: 'pointer',fontSize:'14px'}"
                :row-style="{height:'40px',cursor: 'pointer',fontSize:'14px'}"
                :header-cell-style="{height:'40px',borderColor:'#DCDFE6',fontSize:'14px'}"
              >
                <el-table-column label="序号" type="index" width="80">
                  <template slot="header" slot-scope="scope">
                    <span class="name">序号</span>
                  </template>
                  <template slot-scope="scope">
                    <el-form-item>
                      <i
                        class="el-icon-warning"
                        style="color:#e6a23c;margin-right:3px"
                        v-if="form.hospitalRelationVOList[scope.$index].errorCurrent"
                      ></i>
                      <i
                        class="el-icon-warning"
                        style="color:#fe6c6f;margin-right:3px"
                        v-if="form.hospitalRelationVOList[scope.$index].errorOther"
                      ></i>
                      <span>{{scope.$index+1}}</span>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="医院名称">
                  <template slot="header" slot-scope="scope">
                    <span class="star">*</span>
                    <span class="name">医院名称</span>
                  </template>
                  <template slot-scope="scope">
                    <el-form-item
                      class="is-request"
                      :prop="'hospitalRelationVOList.' + scope.$index + '.name'"
                      :rules="{
                        required: true,message:'请输入医院名称', trigger: 'blur'
                      }"
                    >
                      <el-input
                        class="inp-name"
                        placeholder="请输入医院名称"
                        maxlength="50"
                        v-model.trim="form.hospitalRelationVOList[scope.$index].name"
                      ></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="编码">
                  <template slot="header" slot-scope="scope">
                    <span class="star">*</span>
                    <span class="name">编码</span>
                  </template>
                  <template slot-scope="scope">
                    <el-form-item
                      :prop="'hospitalRelationVOList.' + scope.$index + '.code'"
                      :rules="{
                        required: true,message:'请输入编码', trigger: 'blur'
                      }"
                    >
                      <el-input
                        class="inp-name"
                        placeholder="请输入编码"
                        maxlength="50"
                        v-model.trim="form.hospitalRelationVOList[scope.$index].code"
                      ></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="150" align="center">
                  <template slot-scope="scope">
                    <div class="operation">
                      <el-button @click="addIndex(scope.$index)" type="text" size="small">
                        <img src="../../../images/icon-add.png" alt="新增" />
                      </el-button>
                      <el-button @click="delIndex(scope.$index)" type="text" size="small">
                        <img src="../../../images/icon-delete.png" alt="删除" />
                      </el-button>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
              <div class="add" v-if="form.hospitalRelationVOList.length == 0">
                <p class="add-border" @click="add"></p>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="onSubmit"
              >确定</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <div class="diag-center">
        <p class="diag-title">操作失败,请检查“医院名称+医院编码”重复项</p>
        <p class="title-warning">
          <i class="el-icon-warning" style="color:#e6a23c"></i>标记为当前父医院的“子医院+子医院编码”重复项
        </p>
        <p class="title-warning title-warning-2">
          <i class="el-icon-warning" style="color:#fe6c6f"></i>标记为与其它父医院的“子医院+子医院编码”重复项
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@api/icss.js';
export default {
  name: 'AddHospital',
  data() {
    return {
      form: {
        hospitalRelationVOList: []
      },
      dialogVisible: false,
      saveDisable: false
    };
  },
  created() {
    this.getHospitalRelation(
      JSON.parse(localStorage.getItem('hospitalLoginDTO')).id
    );
  },
  methods: {
    getHospitalRelation(id) {
      api.getHospitalRelation({ id: id }).then(res => {
        if (res.data.code === '0') {
          const data = res.data.data;
          data.hospitalRelationDTOList.forEach((item, index) => {
            item.errorOther = false;
            item.errorCurrent = false;
          });
          this.form.hospitalRelationVOList = data.hospitalRelationDTOList;
        }
      });
    },

    // 确定保存
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveDisable = true;
          let params = {
            hospitalRelationVOList: this.form.hospitalRelationVOList
          };
          this.form.hospitalRelationVOList.forEach((item, index) => {
            item.errorOther = false;
            item.errorCurrent = false;
          });
          api.saveHospitalRelation(params).then(res => {
            console.log('添加的数据？？', res);
            if (res.data.code === '0') {
              this.$message({
                showClose: true,
                message: '保存成功',
                type: 'success',
                duration: 1000
              });
              this.getHospitalRelation();
              this.saveDisable = true;
            } else if (res.data.code === '20020009') {
              const data = res.data.data;
              this.dialogVisible = true;
              let errorOtherList = [];
              let errorCurrentList = [];
              this.getIndex(data.errorOther, errorOtherList);
              this.getIndex(data.errorCurrent, errorCurrentList);
              errorOtherList.forEach(i => {
                this.form.hospitalRelationVOList[i].errorOther = true;
              });
              errorCurrentList.forEach(i => {
                this.form.hospitalRelationVOList[i].errorCurrent = true;
              });
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
                duration: 1000
              });
            }
            this.saveDisable = false;
          });
        } else {
          this.locateToErr();
          return false;
        }
      });
    },
    locateToErr() {
      setTimeout(() => {
        const errorDiv = document.getElementsByClassName('is-error');
        errorDiv[0].scrollIntoView();
      }, 0);
    },
    getIndex(data, list) {
      let hospitalRelationVOList = JSON.parse(
        JSON.stringify(this.form.hospitalRelationVOList)
      );
      hospitalRelationVOList.forEach(it => {
        delete it.errorOther;
        delete it.errorCurrent;
      });
      hospitalRelationVOList.forEach((item, index) => {
        data.forEach((it, i) => {
          if (item.name == it.name && item.code == it.code) {
            list.push(index);
          }
        });
      });
    },

    // 添加
    add() {
      this.form.hospitalRelationVOList.push({
        name: '',
        code: '',
        errorOther: false,
        errorCurrent: false
      });
    },

    // 新增
    addIndex(i) {
      this.form.hospitalRelationVOList.splice(i + 1, 0, {
        name: '',
        code: '',
        errorOther: false,
        errorCurrent: false
      });
      // 延迟执行
      this.$nextTick(() => {
        this.$refs.tableList.bodyWrapper.scrollTop += 55;
      });
    },
    
    // 删除
    delIndex(i) {
      this.form.hospitalRelationVOList.splice(i, 1);
    }
  }
};
</script>

<style lang="less" scoped>
.AddPlanWrapper {
  min-width: 940px;
  .AddPlanBox {
    padding: 20px 60px 0 60px;
    margin: 70px 20px 0 20px;
    background: #fff;
  }
  color: #606266;
  .topBack {
    top: 0;
  }
  .title {
    background-color: #f2f2f2;
    display: flex;
    .handleIcon {
      width: 30px;
      cursor: pointer;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 20px;
        height: 20px;
      }
      .open {
        transform: rotate(180deg);
      }
      .close {
        transform: rotate(0deg);
      }
    }
    .titlwSwitch {
      width: 120px;
    }
    h4 {
      flex: 1;
    }
    .titlwSwitchStatus {
      margin-left: 16px;
    }
  }
  .sub {
    .planItem {
      display: flex;
      .sort {
        width: 60px;
        display: flex;
        .top {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 30px;
          cursor: pointer;
          img {
            width: 10px;
            height: 14px;
          }
        }
        .down {
          width: 30px;
          cursor: pointer;
          display: flex;
          justify-content: center;
          align-items: center;
          img {
            width: 10px;
            height: 14px;
          }
        }
      }
      .openOrClose {
        display: flex;
        flex: 1;
        .planInfo {
          width: 140px;
        }
        .switch {
        }
        .planStatus {
          margin-left: 16px;
        }
      }
      .showNum {
        display: flex;
        width: 160px;
        /deep/.el-input--small {
          width: 60px;
        }
      }
    }
  }
  .el-button {
    float: right;
  }
  .plus-icon-enter-active {
    transition: all 0.8s;
  }
  .plus-icon-enter {
    opacity: 0;
    margin-top: 12px;
  }
  .plus-icon-leave-active {
    transition: all 0.8s;
  }
  .plus-icon-leave-active {
    opacity: 0;
    margin-top: 12px;
  }
}
.leaveBox {
  /deep/ .leaveBtn {
    // margin-right: 46px;
    background-color: #d7d7d7 !important;
    border-color: transparent;
  }
}
.tabs {
  max-width: 100%;
  min-width: 100%;
  border-color: #dcdfe6;
  .star {
    color: #ff545b;
  }
  /deep/.el-input__inner {
    height: 32px;
    margin-bottom: 18px;
    font-size: 12px;
  }
  /deep/ .is-error .el-form-item__error {
    top: 34px;
  }
  .operation {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 8px;
    & img {
      width: 16px;
      height: 16px;
    }
  }
}

.add {
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid #ebeef5;
  border-top: none;
  .add-border {
    border: 1px #48c5d7 dashed;
    width: 96%;
    height: 30px;
    text-align: center;
    line-height: 30px;
    cursor: pointer;
    background: url('../../../images/add-nob.png') no-repeat center center;
    background-size: 16px 16px;
  }
}
.diag-center {
  text-align: center;
  .diag-title {
    font-size: 16px;
    margin-bottom: 5px;
    color: #ff545b;
  }
  .title-warning {
    display: block;
    margin: 10px 0;
  }
  .title-warning-2 {
    padding-left: 13px;
  }
}
.dialog-footer {
  display: flex;
  overflow: hidden;
  justify-content: center;
}
/deep/.el-form-item.is-success .el-input__inner,
.el-form-item.is-success .el-input__inner:focus,
.el-form-item.is-success .el-textarea__inner,
.el-form-item.is-success .el-textarea__inner:focus {
  border-color: #ccc;
}
</style>