<template>
  <div class="doctorBody">
    <!-- 顶部筛选 -->
    <div class="screening">
      <div class="screening-title flex-vertical-center-l">
        <img src="~@/assets/filters.png" alt />
      </div>
      <div class="screening-form flex-vertical-center-l flex-wrap">
        <div class="screening-item flex-vertical-center-l">
          <span>名称：</span>
          <div class="input">
            <el-input size="mini" v-model="form.name" placeholder="请输入"></el-input>
          </div>
        </div>
        <el-button type="primary" size="mini" @click="search()">搜索</el-button>
        <el-button type="warning" size="mini" @click="clearFilter()">清空</el-button>
        <el-button type="primary" size="mini" @click="addData()" v-if="showAdd">新增</el-button>
      </div>
    </div>

    <!-- 表格 -->
    <div class="today-data">
      <div class="today-table">
        <el-table :data="tableData" stripe style="width: 100%" border height="100%">
          <el-table-column prop="id" label="序号" width="50" align="center"></el-table-column>
          <el-table-column prop="code" label="医共体ID" width="180" align="center"></el-table-column>
          <el-table-column prop="name" label="医共体名称" align="center"></el-table-column>
          <el-table-column prop="password" label="医共体密码" align="center"></el-table-column>
          <el-table-column prop="remark" label="医共体说明" align="center"></el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template slot-scope="scope">
              <div
                v-if="scope.row.isable==1"
                class="flex-center enable"
                @click="changeState(scope)"
              >禁用</div>
              <div
                v-if="scope.row.isable==0"
                class="flex-center disable"
                @click="changeState(scope)"
              >启用</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <div class="flex-center operation">
                <div class="flex-center" @click="editData(scope)" v-if="showEdit">编辑</div>
                <div class="flex-center bg-yellow" @click="deleteData(scope)" v-if="showdel">删除</div>
                <div
                  class="flex-center bg-default"
                  @click="$router.push({path:'/index/parameter?pid='+scope.row.pid})"
                  v-if="showCode"
                >参数管理</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="flex-vertical-center-r today-page">
        <el-pagination
          background
          layout=" prev, pager, next, jumper, total"
          :total="total"
          :page-size="limit"
          @current-change="sizeC($event)"
        ></el-pagination>
      </div>
    </div>

    <popup
      distanceTop="5vh"
      :showDialog="showDialog"
      @cancle="showDialog=false"
      @confim="submit()"
      title="医共体详情"
    >
      <div class="flex-center" slot="body">
        <div class="form">
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-l">
              <span>*</span>
              <div>医共体名称：</div>
            </div>
            <div class="input">
              <el-input v-model="diaform.name" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <span>*</span>
            <div class="name">医共体密码：</div>
            <div class="input">
              <el-input v-model="diaform.pas" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-l">
              <span>*</span>
              <div>医共体ID：</div>
            </div>
            <div class="input">
              <el-input v-model="diaform.code" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-l">
              <span></span>
              <div>账号数量：</div>
            </div>
            <div class="input">
              <el-input v-model="diaform.num" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <span></span>
            <div class="name">医共体说明：</div>
            <div class="input">
              <el-input type="textarea" v-model="diaform.desc" placeholder="请输入"></el-input>
            </div>
          </div>
        </div>
      </div>
    </popup>
  </div>
</template>
<script>
import {
  addDoctorBody,
  deleteDoctorBody,
  editDoctorBody,
  getDoctorBody,
  getDoctorBDetail,
  getDoctorBR
} from "@/api/system.js";
import popup from "@/components/Propup.vue";
export default {
  components: {
    popup
  },
  data() {
    return {
      showDialog: false,
      form: {
        name: ""
      },
      diaform: {
        name: "",
        pas: "",
        desc: "",
        pid: "",
        num: "",
        code: ""
      },
      tableData: [],
      total: 0,
      limit: 10,
      page: 1,

      type: "add",
      showAdd: true,
      showEdit: true,
      showdel: true,
      showEstate: true,
      showCode: true
    };
  },
  created() {
    this.getDoctorBody();
    this.getDoctorBR();
  },
  methods: {
    // 删除 当前数据
    deleteData(scope) {
      this.$confirm("确定要删除当前数据吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteDoctorBody(scope.row.pid);
        })
        .catch(() => {});
    },
    // 修改状态
    changeState(scope) {
      if (!this.showEstate) return;
      let isable = scope.row.isable;
      this.$confirm("确定要修改当前状态吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let form = {
            name: scope.row.name,
            pas: scope.row.password,
            desc: scope.row.remark,
            pid: scope.row.pid
          };
          this.editDoctorBody(form, isable == 0 ? 1 : 0);
        })
        .catch(() => {});
    },
    submit() {
      if (this.diaform.name == "") {
        this.$message({
          showClose: true,
          message: "请输入医共体名称",
          type: "error"
        });

        return;
      }
      if (this.diaform.pas == "") {
        this.$message({
          showClose: true,
          message: "请输入医共体密码",
          type: "error"
        });

        return;
      }
      if (this.diaform.code == "") {
        this.$message({
          showClose: true,
          message: "请输入医共体ID",
          type: "error"
        });

        return;
      }
      if (this.type == "add") {
        this.addDoctorBody();
      } else if (this.type == "edit") {
        this.editDoctorBody(this.diaform, this.diaform.isable);
      }
    },
    // 添加按钮
    addData() {
      this.showDialog = true;
      this.type = "add";
      this.diaform = {
        name: "",
        pas: "",
        desc: "",
        pid: "",
        num: "",
        code: ""
      };
    },
    // 修改数据
    editData(scope) {
      this.showDialog = true;
      this.type = "edit";
      this.getDoctorBDetail(scope.row.pid);
    },
    search() {
      this.page = 1;
      this.getDoctorBody();
    },
    clearFilter() {
      this.page = 1;
      this.form.name = "";
      this.getDoctorBody();
    },
    sizeC(e) {
      // console.log(e, '1')
      this.page = e;
      this.getDoctorBody();
    },

    // 获取医共体数据
    async getDoctorBody() {
      let params = {
        key: this.form.name,
        page: this.page,
        limit: this.limit
      };
      let res = await getDoctorBody(params);
      if (res.ResultCode == 0) {
        this.tableData = res.Data.Items;
        this.total = res.Data.TotalRecordCount;
        this.tableData.filter((item, index) => {
          return (item.id = index + 1);
        });

        this.showEdit = res.Data.ButtonRight.indexOf("edit") != -1;
        this.showdel = res.Data.ButtonRight.indexOf("del") != -1;
        this.showEstate = res.Data.ButtonRight.indexOf("setState") != -1;
        this.showCode = res.Data.ButtonRight.indexOf("paramManagement") != -1;
      }
    },
    //添加 医共体数据
    async addDoctorBody() {
      let params = {
        name: this.diaform.name,
        password: this.diaform.pas,
        remark: this.diaform.desc,
        usernum: this.diaform.num,
        code: this.diaform.code
      };
      const loading = this.$loading({
        lock: true,
        text: "正在提交...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await addDoctorBody(params).catch(err => {
        loading.close();
      });

      if (res.ResultCode == 0) {
        loading.close();

        this.$message({
          showClose: true,
          message: "提交成功",
          type: "success"
        });
        this.page = 1;
        this.getDoctorBody();
        this.showDialog = false;
      }
    },
    // 获取医共体详情
    async getDoctorBDetail(id) {
      let res = await getDoctorBDetail(id);
      if (res.ResultCode == 0) {
        this.diaform = {
          name: res.Data.name,
          pas: res.Data.password,
          desc: res.Data.remark,
          pid: res.Data.pid,
          num: res.Data.usernum,
          isable: res.Data.isable,
          code: res.Data.code
        };
      }
    },
    // 修改医共体数据
    async editDoctorBody(diaform, isable = "") {
      let params = {
        name: diaform.name,
        password: diaform.pas,
        remark: diaform.desc,
        pid: diaform.pid,
        isable: isable,
        usernum: this.diaform.num,
        code: this.diaform.code
      };
      const loading = this.$loading({
        lock: true,
        text: "正在提交...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await editDoctorBody(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();

        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success"
        });

        this.getDoctorBody();
        this.showDialog = false;
      }
    },
    // 删除医共体数据
    async deleteDoctorBody(id) {
      let arr = [];

      arr.push(id);

      let params = {
        ids: arr.length == 1 ? arr[0] + "," : arr.join(",")
      };
      const loading = this.$loading({
        lock: true,
        text: "正在删除...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await deleteDoctorBody(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();

        this.$message({
          showClose: true,
          message: "删除成功",
          type: "success"
        });

        this.getDoctorBody();
        this.showDialog = false;
      }
    },

    // 获取医共体权限按钮
    async getDoctorBR() {
      let res = await getDoctorBR();
      if (res.ResultCode == 0) {
        this.showAdd = res.Data.indexOf("add") != -1;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../../style/common.scss";
@import "../../style/base.scss";

.doctorBody {
  .form-item {
    margin-bottom: 20px;

    span {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #ff3a3a;
      display: inline-block;
      width: 8px;
    }

    .name {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #333;

      div {
        width: 96px;
        text-align: right;
      }
    }

    .input {
      width: 260px;
    }
  }
}

.today-data {
  margin-top: 5px;
  padding: 10px 10px;
  background: #ffffff;
  border-radius: 5px;
  height: 71vh;

  .add {
    width: 74px;
    height: 36px;
    background: #5386f6;
    border-radius: 4px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #ffffff;
    cursor: pointer;
    margin-bottom: 20px;
  }

  .today-table {
    height: 89%;

    .enable,
    .disable {
      width: 60px;
      height: 24px;
      background: #ff6245;
      border-radius: 2px;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #ffffff;
      cursor: pointer;
      margin: 0 auto;
    }

    .disable {
      background: #68a8ff !important;
    }

    .operation {
      div {
        width: 60px;
        height: 24px;
        background: #5386f6;
        border-radius: 2px;
        cursor: pointer;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
      }

      .bg-yellow {
        background: #ffae45;
        margin-left: 15px;
      }

      .bg-default {
        width: 80px;
        margin-left: 15px;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
@media screen and (min-width: 1681px) and (max-width: 1920px) {
  .today-data {
    height: 82vh;
    padding: 10px;
    // background: red;

    .today-table {
      height: 94%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 18px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 18px 0;
  }
}

@media screen and (min-width: 1601px) and (max-width: 1680px) {
  .today-data {
    height: 81vh;
    padding: 10px;
    // background: red;

    .today-table {
      height: 94%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 16px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 20px 0;
  }
}

@media screen and (min-width: 1361px) and (max-width: 1600px) {
  .today-data {
    height: 73vh;
    padding: 10px;
    // background: red;

    .today-table {
      height: 92%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 10px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 14px 0;
  }
}

@media screen and(min-width:1281px) and (max-width: 1360px) {
  .today-data {
    height: 73vh;
    padding: 5px;
    // background: red;

    .today-table {
      height: 92%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 5px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 5px 0;
  }
}
</style>