<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-select
              size="mini"
              v-model="form.name"
              placeholder="请选择"
              @change="getMedSelect(form.name,0)"
            >
              <el-option
                :label="item.name"
                :value="item.pid"
                v-for="(item,index) in doctorBodyList"
                :key="index"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="screening-item flex-vertical-center-l">
          <span>医疗机构名称：</span>
          <div class="input">
            <el-select size="mini" v-model="form.medName" placeholder="请选择">
              <el-option
                :label="item.name"
                :value="item.pid"
                v-for="(item,index) in medSelectList"
                :key="index"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="screening-item flex-vertical-center-l">
          <span>科室名称：</span>
          <div class="input">
            <!-- <el-input v-model="form.departName" placeholder="请输入"></el-input> -->
            <el-autocomplete
              size="mini"
              class="inline-input"
              v-model="form.departName"
              :fetch-suggestions="querySearch"
              placeholder="请输入"
              @select="handleSelect"
            ></el-autocomplete>
          </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="add flex-center" @click="addData()" v-if="showAdd">新增</div> -->
      <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="organizationidSelsourceName" label="医共体名称" align="center"></el-table-column>
          <el-table-column prop="institutionidSelsourceName" label="医疗机构名称" align="center"></el-table-column>

          <el-table-column prop="name" label="科室名称" width="180" align="center"></el-table-column>

          <el-table-column prop="parentdepartmentidSelsourceName" label="上级科室" align="center"></el-table-column>
          <!-- <el-table-column prop="remark" label="备注">
          </el-table-column>-->
          <el-table-column prop="state" label="状态" align="center" width="80">
            <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="操作">
            <template slot-scope="scope">
              <div class="flex-center operation">
                <div class="flex-center" @click="edidData(scope)" v-if="showEdit">编辑</div>
                <div class="flex-center bg-yellow" @click="deleteData(scope)" v-if="showDel">删除</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-r">
              <span>*</span>
              <div>医共体名称：</div>
            </div>
            <div class="input">
              <el-select
                v-model="diaform.name"
                placeholder="请选择"
                @change="getMedSelect(diaform.name,1)"
              >
                <el-option
                  :label="item.name"
                  :value="item.pid"
                  v-for="(item,index) in doctorBodyList"
                  :key="index"
                ></el-option>
              </el-select>
            </div>
          </div>

          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-r">
              <span>*</span>
              <div>医疗机构名称：</div>
            </div>
            <div class="input">
              <el-select
                v-model="diaform.medName"
                placeholder="请选择"
                @change="getDepartSelect(diaform.medName)"
              >
                <el-option
                  :label="item.name"
                  :value="item.pid"
                  v-for="(item,index) in medSelectList1"
                  :key="index"
                ></el-option>
              </el-select>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-r">
              <span>*</span>
              <div>科室名称：</div>
            </div>
            <div class="input">
              <el-input v-model="diaform.departName" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-r">
              <span></span>
              <div>上级科室：</div>
            </div>
            <div class="input">
              <el-select v-model="diaform.fDepart" placeholder="请选择">
                <el-option label="请选择" value></el-option>
                <el-option
                  :label="item.name"
                  :value="item.pid"
                  v-for="(item,index) in departList"
                  :key="index"
                ></el-option>
              </el-select>
            </div>
          </div>
        </div>
      </div>
    </popup>
  </div>
</template>
<script>
import {
  getDoctorBodySelect,
  getMedSelect,
  getDepartSelect,
  getDepartList,
  addDepart,
  getDepartDetail,
  editDepart,
  deleteDepart,
  getDepartBR
} from "@/api/system.js";
import popup from "@/components/Propup.vue";
import { load } from "mime";
export default {
  components: {
    popup
  },
  data() {
    return {
      showDialog: false,
      form: {
        name: "",
        departName: "",
        medName: ""
      },
      diaform: {
        name: "",
        medName: "",
        departName: "",
        fDepart: "",
        pid: ""
      },
      tableData: [],
      doctorBodyList: [], // 医共体数据
      medSelectList: [], // 医疗机构 选择器 数据
      medSelectList1: [],
      departList: [], // 上级科室选择器
      page: 1,
      total: 0,
      limit: 10,
      type: "add",

      showAdd: true,
      showEdit: true,
      showDel: true,
      showState: true
    };
  },
  created() {
    this.getDoctorBodySelect();
    this.getMedSelect("", 0);
    this.getDepartList();
    this.getDepartBR();
  },
  methods: {
    querySearch(queryString, cb) {
      var results = [
        {
          value: "外科"
        },
        {
          value: "内科"
        }
      ];
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    handleSelect(item) {
      // console.log(item);
      this.form.departName = item.value;
    },
    // 修改状态
    changeState(scope) {
      if (!this.showState) return;
      let isable = scope.row.isable;
      this.$confirm("确定要修改当前状态吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.diaform.pid = scope.row.pid;
          this.getDepartDetail(this.diaform.pid);
          setTimeout(() => {
            this.editDepart(scope.row.pid, isable == 0 ? 1 : 0);
          }, 500);
        })
        .catch(() => {});
    },
    // 删除数据
    deleteData(scope) {
      this.$confirm("确定要删除当前数据吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteDepart(scope.row.pid);
        })
        .catch(() => {});
    },
    // 提交数据
    submit() {
      if (this.diaform.name == "") {
        this.$message({
          message: "请选择医共体",
          type: "error",
          showClose: true
        });
        return;
      }
      if (this.diaform.medName == "") {
        this.$message({
          message: "请选择医疗机构",
          type: "error",
          showClose: true
        });
        return;
      }
      if (this.diaform.departName == "") {
        this.$message({
          message: "请输入科室名称",
          type: "error",
          showClose: true
        });
        return;
      }

      if (this.type == "add") {
        this.addDepart();
      } else {
        this.editDepart(this.diaform.pid);
      }
    },
    // 添加数据
    addData() {
      this.showDialog = true;
      this.type = "add";
      this.diaform = {
        name: "",
        medName: "",
        departName: "",
        fDepart: "",
        pid: ""
      };
    },
    // 编辑数据
    edidData(scope) {
      this.type = "edit";

      this.getDepartDetail(scope.row.pid);
      this.showDialog = true;
    },
    clearFilter() {
      this.form = {
        name: "",
        departName: "",
        medName: ""
      };
      this.getDepartList();
      this.getMedSelect("", 0);
    },
    search() {
      this.page = 1;
      this.getDepartList();
    },
    sizeC(e) {
      // console.log(e, '1')
      this.page = e;
      this.getDepartList();
    },
    // 获取科室列表
    async getDepartList() {
      let params = {
        organizationidSelsource: this.form.name,
        name: this.form.departName,
        institutionidSelsource: this.form.medName,
        page: this.page,
        limit: this.limit
      };
      let res = await getDepartList(params);
      if (res.ResultCode == 0) {
        this.tableData = res.Data.Items;
        this.total = res.Data.TotalRecordCount;
        this.tableData.filter((item, index) => {
          if (this.page > 1) {
            if (index == 9) {
              return (item.id = this.page + String(0));
            }
            return (item.id = this.page - 1 + String(index + 1));
          } else {
            return (item.id = index + 1);
          }
        });

        this.showEdit = res.Data.ButtonRight.indexOf("edit") != -1;
        this.showDel = res.Data.ButtonRight.indexOf("del") != -1;
        this.showState = res.Data.ButtonRight.indexOf("setState") != -1;
      }
    },
    // 获取医共体选择器数据
    async getDoctorBodySelect() {
      let res = await getDoctorBodySelect();
      if (res.ResultCode == 0) {
        this.doctorBodyList = res.Data;
      }
    },
    // 获取医疗机构选择器
    async getMedSelect(id, type) {
      let params = {
        organizationId: id
      };
      let res = await getMedSelect(params);
      if (res.ResultCode == 0) {
        if (type == 0) {
          this.medSelectList = res.Data;
        } else {
          this.medSelectList1 = res.Data;

          this.doctorBodyList.forEach(item => {
            if (item.pid == id) {
              this.diaform.code = item.code;
            }
          });
        }
      }
    },
    // 获取医疗机构下的科室
    async getDepartSelect(id) {
      let res = await getDepartSelect({
        institutionId: id
      });
      if (res.ResultCode == 0) {
        this.departList = res.Data;
      }
    },
    // 添加 科室
    async addDepart() {
      let params = {
        institutionidSelsource: this.diaform.medName,
        name: this.diaform.departName,
        organizationidSelsource: this.diaform.name,
        parentdepartmentidSelsource: this.diaform.fDepart,
        isable: 0
      };
      const loading = this.$loading({
        lock: true,
        text: "正在提交",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await addDepart(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();
        this.$message({
          type: "success",
          message: "提交成功",
          showClose: true
        });
        this.showDialog = false;

        this.page = 1;
        this.getDepartList();
      }
    },
    // 获取科室信息
    async getDepartDetail(id) {
      let res = await getDepartDetail(id);
      if (res.ResultCode == 0) {
        this.diaform = {
          name: res.Data.organizationidSelsource,
          medName: res.Data.institutionidSelsource,
          departName: res.Data.name,
          fDepart: res.Data.parentdepartmentidSelsource,
          pid: id
        };

        this.getMedSelect(res.Data.organizationidSelsource, 1);
        this.getDepartSelect(res.Data.institutionidSelsource);
      }
    },
    // 修改科室
    async editDepart(id, isable = "") {
      let params = {
        institutionidSelsource: this.diaform.medName,
        name: this.diaform.departName,
        organizationidSelsource: this.diaform.name,
        parentdepartmentidSelsource: this.diaform.fDepart,
        isable: isable,
        pid: id
      };
      const loading = this.$loading({
        lock: true,
        text: "正在提交",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await editDepart(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();
        this.$message({
          type: "success",
          message: "提交成功",
          showClose: true
        });
        this.showDialog = false;

        this.getDepartList();
      }
    },
    //删除科室数据
    async deleteDepart(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 deleteDepart(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();

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

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

    // 获取 科室权限按钮
    async getDepartBR() {
      let res = await getDepartBR();
      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;
      width: 120px;
      text-align: right;
    }

    .input {
      width: 260px;
    }

    .input1 {
      width: 84px;
    }
  }
}

.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;
      }
    }
  }
}

.table::v-deep .el-table .cell {
  text-align: center;
}

.input ::v-deep .el-select {
  width: 100%;
}

.input1::v-deep .el-select .el-input__inner {
  padding: 0 20px 0 10px;
}
</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>