<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >

      <el-form-item label="医院名称" prop="hospitalName">
        <el-input
          v-model="queryParams.hospitalName"
          placeholder="请输入医院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室编码" prop="departmentCode">
        <el-input
          v-model="queryParams.departmentCode"
          placeholder="请输入科室编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室名称" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入科室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:hospitalDepartment:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hospitalDepartment:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hospitalDepartment:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="hospitalDepartmentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="父级科室id" align="center" prop="parentId" />
      <el-table-column label="医院表id" align="center" prop="hospitalId" /> -->
      <el-table-column label="医院名称" align="center" prop="hospitalName" />
      <el-table-column label="科室编码" align="center" prop="departmentCode" />
      <el-table-column label="科室名称" align="center" prop="departmentName" />
      <el-table-column
        label="科室联系电话"
        align="center"
        prop="departmentPhone"
      />
      <el-table-column
        label="科室负责人"
        align="center"
        prop="departmentPerson"
      />
      <!-- <el-table-column
        label="科室地址"
        align="center"
        prop="departmentAddress"
      /> -->
      <!-- <el-table-column label="科室显示级别" align="center" prop="departmentLevel" /> -->
      <!-- <el-table-column
        label="科室显示顺序"
        align="center"
        prop="departmentSort"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:hospitalDepartment:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:hospitalDepartment:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改科室信息管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addopen"
      width="1200px"
      append-to-body
      :before-close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <div
          v-for="(item, index) in form.hospitalDepartmentInfoList"
          :key="index"
          style="margin-top: 20px; border-bottom: 1px solid #f3f3f3"
        >
          <el-form-item
            label="所属医院"
            :rules="rules.hospitalDepartmentInfoList.hospitalId"
            :prop="`hospitalDepartmentInfoList.${index}.hospitalId`"
          >
            <el-button
              type=""
              v-if="item.hospitalName == '请选择所属医院'"
              @click="clickinnerVisible(item, index)"
              style="
                width: 250px;
                text-align: left;
                height: 36px;
                color: #c0c4cc;
                overflow: hidden;
              "
              >{{ item.hospitalName }}</el-button
            >
            <el-button
              @click="clickinnerVisible(item, index)"
              type=""
              v-else
              style="
                width: 250px;
                text-align: left;
                height: 36px;
                padding-left: -10px;
                overflow: hidden;
              "
              >{{ item.hospitalName }}</el-button
            >
          </el-form-item>

          <el-form-item
            label="科室名称"
            :rules="rules.hospitalDepartmentInfoList.departmentName"
            :prop="`hospitalDepartmentInfoList.${index}.departmentName`"
          >
            <el-input
              style="width: 250px"
              v-model="item.departmentName"
              placeholder="请输入科室名称"
            />
          </el-form-item>
          <el-form-item
            label="科室联系电话"
            :rules="rules.hospitalDepartmentInfoList.departmentPhone"
            :prop="`hospitalDepartmentInfoList.${index}.departmentPhone`"
          >
            <el-input
            maxlength="11"
              style="width: 250px"
              v-model="item.departmentPhone"
              placeholder="请输入科室联系电话"
            />
          </el-form-item>
          <el-form-item
            label="科室负责人"
            :rules="rules.hospitalDepartmentInfoList.departmentPerson"
            :prop="`hospitalDepartmentInfoList.${index}.departmentPerson`"
          >
            <el-input
              style="width: 250px"
              v-model="item.departmentPerson"
              placeholder="请输入科室负责人名称"
            />
          </el-form-item>
          <el-form-item
            label="科室地址"
            :rules="rules.hospitalDepartmentInfoList.departmentAddress"
            :prop="`hospitalDepartmentInfoList.${index}.departmentAddress`"
          >
            <el-input
              style="width: 250px"
              v-model="item.departmentAddress"
              placeholder="请输入科室地址"
            />
          </el-form-item>
          <el-form-item
            label="科室显示顺序"
            :rules="rules.hospitalDepartmentInfoList.departmentSort"
            :prop="`hospitalDepartmentInfoList.${index}.departmentSort`"
          >
            <el-input
            maxlength="5"
            oninput="value=value.replace(/[^\d]/g,'')"
              v-model.number="item.departmentSort"
              placeholder="请输入科室显示顺序"
              style="width:250px"
            />
          </el-form-item>
          <el-button
            type="primary"
            circle
            plain
            icon="el-icon-circle-plus-outline"
            @click="adddisease"
            v-if="index == 0"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            plain
            @click="deldisease(index)"
            v-if="index != 0"
          ></el-button></div
      ></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择医院 -->
    <el-dialog
      title="选择医院"
      :visible.sync="innerVisible"
      width="1000px"
      append-to-body
      :before-close="innerVisiblecancel"
    >
      <el-form
        ref="queryForm"
        :model="hospitalqueryParams"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <el-form-item label="医院编码" prop="hospitalCode" label-width="120">
          <el-input
            v-model="hospitalqueryParams.hospitalCode"
            placeholder="请输入科室编码"
            clearable
          />
        </el-form-item>
        <el-form-item label="医院名称" prop="hospitalName" label-width="120">
          <el-input
            v-model="hospitalqueryParams.hospitalName"
            placeholder="请输入医院名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="info"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="addresetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        :data="infolist"
        @cell-dblclick="nurseclick"
        v-loading="loading"
      >
        <el-table-column label="请选择" width="100" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="width: 15px; height: 15px"
              v-if="form.hospitalId == scope.row.id"
              circle
              @click="nurseclick(scope.row)"
            ></el-button>
            <el-button
              v-else
              style="width: 15px; height: 15px"
              circle
              @click="nurseclick(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column
          property="hospitalCode"
          label="医院编码"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="hospitalName"
          label="医院名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>

        <el-table-column
          property="hospitalAddress"
          label="医院地址"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column
          property="hospitalIntroduce"
          label="医院介绍"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <el-table-column
          property="phone"
          label="联系电话"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      </el-table>
      <pagination
        v-show="total2 > 0"
        :total="total2"
        :page.sync="hospitalqueryParams.pageNum"
        :limit.sync="hospitalqueryParams.pageSize"
        @pagination="info"
      />
    </el-dialog>
    <!-- 修改护理站部门信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" :inline="true">
        <el-form-item label="所属医院" prop="hospitalName">
          <el-input
            v-model="form.hospitalName"
            disabled
            placeholder="请输入所属医院"
            style="width: 250px"
            maxlength="15"
          />
        </el-form-item>
        <el-form-item label="科室名称" prop="departmentName">
          <el-input
            v-model="form.departmentName"
            placeholder="请输入科室名称"
            style="width: 250px"
            maxlength="15"
          />
        </el-form-item>
        <el-form-item label="科室负责人" prop="departmentPerson">
          <el-input
            v-model="form.departmentPerson"
            placeholder="请输入科室负责人"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="科室联系电话" prop="departmentPhone">
          <el-input
            v-model="form.departmentPhone"
            placeholder="请输入联系电话"
            maxlength="11"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="科室地址" prop="departmentAddress">
          <el-input
            v-model="form.departmentAddress"
            placeholder="请输入科室地址"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="科室显示顺序" prop="departmentSort">
          <el-input
          oninput="value=value.replace(/[^\d]/g,'')"
          maxlength="5"
            v-model.number="form.departmentSort"
            placeholder="请输入科室显示顺序"
            style="width: 250px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  listHospitalDepartment,
  getHospitalDepartment,
  delHospitalDepartment,
  addHospitalDepartment,
  updateHospitalDepartment,
} from "@/api/system/hospitalDepartment";
import { get } from "http";

export default {
  name: "HospitalDepartment",
  data() {
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57]|19[0-9])[0-9]{8}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      cb(new Error("请输入正确的联系电话"));
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      hospitalqueryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitalCode:"",
        hospitalName:"",
      },
      infolist: [],
      // 非单个禁用
      single: true,
      addopen: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      // 科室信息管理表格数据
      hospitalDepartmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      innerVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        hospitalId: null,
        hospitalName: null,
        hospitalCode: null,
        departmentCode: null,
        departmentName: null,
        departmentPhone: null,
        departmentPerson: null,
        departmentAddress: null,
        departmentLevel: null,
        departmentSort: null,
      },
      homenumber: null,

      // 表单参数
      form: {},
      //选择所属医院对应的下标
      nnerVisibleindex: "",
      // 表单校验

      rules: {
        hospitalDepartmentInfoList: {
          hospitalId: [
            {
              required: true,
              message: "请选择所属医院",
              trigger: "blur",
            },
          ],

          departmentSort: [
            {
              required: true,
              message: "请输入科室显示排序",
              trigger: "blur",
            },
          ],
          departmentName: [
            {
              required: true,
              message: "请输入科室名称",
              trigger: "blur",
            },
          ],
          departmentPerson: [
            {
              required: true,
              message: "请输入科室负责人",
              trigger: "blur",
            },
          ],
          departmentPhone: [
            {
              required: true,
              validator: checkMobile,
              trigger: "blur",
              message: "",
            },
          ],
          departmentAddress: [
            {
              required: true,
              message: "请输入科室地址",
              trigger: "blur",
            },
          ],
        },

        departmentName: [
          {
            required: true,
            message: "请输入科室名称",
            trigger: "blur",
          },
        ],
        departmentPerson: [
          {
            required: true,
            message: "请输入科室负责人",
            trigger: "blur",
          },
        ],
        departmentPhone: [
          {
            required: true,
            validator: checkMobile,
            trigger: "blur",
            message: "",
          },
        ],
        departmentAddress: [
          {
            required: true,
            message: "请输入科室地址",
            trigger: "blur",
            message: "",
          },
        ],
        departmentSort: [
          {
            required: true,
            message: "请输入科室显示顺序",
            trigger: "blur",
          },
        ],

      },
    };
  },
  created() {
    this.getList();
    this.info();
  },
  methods: {
    // 取消按钮
    cancel() {
      this.addopen = false;
      // this.reset();
    },

    innerVisiblecancel() {
      this.innerVisible = false;
    },
    // add

    nurseclick(row) {
      console.log(row);
      // if (this.homenumber) {
      //   console.log(this.homenumber);
      //   this.form.hospitalName = row.hospitalName;
      //   this.form.hospitalId = row.id;
      // } else {
        this.form.hospitalDepartmentInfoList[
          this.nnerVisibleindex
        ].hospitalName = row.hospitalName;
        this.form.hospitalDepartmentInfoList[this.nnerVisibleindex].hospitalId =
          row.id;
      // }
      this.innerVisible = false;
    },

    //删除deldisease
    deldisease(index) {
      this.form.hospitalDepartmentInfoList.splice(index, 1);
    },
    adddisease() {
      var obj = {
        hospitalId: "",
        departmentCode: "",
        departmentName: "",
        departmentPerson: "",
        departmentPhone: "",
        hospitalName: "请选择所属医院",
      };
      if (this.form.hospitalDepartmentInfoList.length == 5) {
        this.$message.error("最多只能5条");
      } else {
        this.form.hospitalDepartmentInfoList.push(obj);
      }
    },
    // 新增医院按钮
    clickinnerVisible(item, index) {
      this.addresetQuery();
      this.form.hospitalId = item.hospitalId;
      this.nnerVisibleindex = index;
      console.log(this.nnerVisibleindex);
      this.innerVisible = true;
      // this.homenumber = item;
    },
    //修改医院按钮
    clickitem() {
      this.addresetQuery();
      // this.form.hospitalId = item.hospitalId;
      this.innerVisible = true;
    },
    addresetQuery() {
      this.hospitalqueryParams = {
        pageNum: 1,
        pageSize: 10,
        hospitalCode:"",
        hospitalName:"",
      };
      this.info();
    },
    /** 查询科室信息管理列表 */
    getList() {
      this.loading = true;
      listHospitalDepartment(this.queryParams).then((response) => {
        this.hospitalDepartmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    info() {
      list(this.hospitalqueryParams,).then((response) => {
        this.infolist = response.rows;
        console.log(this.infolist)
        this.total2 = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.addopen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hospitalDepartmentInfoList: [
          {
            hospitalId: "",
            departmentCode: "",
            departmentName: "",
            departmentPerson: "",
            departmentPhone: "",
            departmentSort: "",
            departmentAddress: "",
            hospitalName: "请选择所属医院",
          },
        ],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        hospitalName:"",
        departmentCode:"",
        departmentName:"",
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addopen = true;
      this.title = "添加科室信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form.hospitalId = row.id;
      getHospitalDepartment(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改科室信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateHospitalDepartment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHospitalDepartment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.addopen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除此科室的信息？')
        .then(function () {
          return delHospitalDepartment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/hospitalDepartment/export",
        {
          ...this.queryParams,
        },
        `hospitalDepartment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
