<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="hospitalId">
        <el-select
          v-model="queryParams.hospitalId"
          placeholder="请选择医院名称"
          clearable
          @change="hospitalNamechange"
        >
          <el-option
            v-for="item in hospitalNamelist"
            :key="item.id"
            :label="item.hospitalName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室名称" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择科室名称"
          clearable
          @change="departmentNameadd"
        >
          <el-option
            v-for="item in hospitalDepartmentist"
            :key="item.id"
            :label="item.departmentName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="医生姓名" prop="doctorId">
        <el-select
          v-model="queryParams.doctorId"
          placeholder="请选择医生姓名"
          clearable
          @change="doctorIdadd"
        >
          <el-option
            v-for="item in personNamelist"
            :key="item.id"
            :label="item.personName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </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"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          >修改</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="hospitalList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医院名称" align="center" prop="hospitalName" />
      <el-table-column label="科室名称" align="center" prop="departmentName" />
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="班次名称" align="center" prop="scheduleName" />
      <el-table-column
        label="排版开始日期"
        align="center"
        prop="scheduleStartDate"
      />
      <el-table-column
        label="排版结束日期"
        align="center"
        prop="scheduleEndDate"
      />
      <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-zoom-in"
            @click="handleUpdate(scope.row)"
            >查看明细</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</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="850px"
      append-to-body
      :before-close="cancel"
    >
      <el-form
        :inline="true"
        :rules="rules"
        label-width="120px"
        :model="form"
        ref="form"
      >
        <el-form-item label="医院名称" prop="hospitalId">
          <el-select
            v-model="form.hospitalId"
            placeholder="请选择医院名称"
            clearable
            @change="hospitalNamechangeadd"
          >
            <el-option
              v-for="item in hospitalNamelist"
              :key="item.id"
              :label="item.hospitalName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班次名称" prop="scheduleId">
          <el-select
            v-model="form.scheduleId"
            placeholder="请选择班次名称"
            clearable
            @change="scheduleIdadd"
          >
            <el-option
              v-for="item in schedulelist"
              :key="item.id"
              :label="item.scheduleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室名称" prop="departmentId">
          <el-select
            v-model="form.departmentId"
            placeholder="请选择科室名称"
            clearable
            @change="departmentNameadd"
          >
            <el-option
              v-for="item in hospitalDepartmentist"
              :key="item.id"
              :label="item.departmentName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医生姓名" prop="doctorId">
          <el-select
            v-model="form.doctorId"
            placeholder="请选择医生姓名"
            clearable
            @change="doctorIdadd"
          >
            <el-option
              v-for="item in personNamelist"
              :key="item.id"
              :label="item.personName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排班开始日期" prop="scheduleStartDate">
          <el-date-picker
            v-model="form.scheduleStartDate"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="排班开始日期"
            style="width: 220px"
            :picker-options="pickerStartTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="排班结束日期" prop="scheduleEndDate">
          <el-date-picker
            v-model="form.scheduleEndDate"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="排班结束日期"
            style="width: 220px"
            :picker-options="pickerEndTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="每人预约分钟数" prop="minutesPerPatient">
          <el-input-number
            style="width: 220px"
            v-model="form.minutesPerPatient"
            label="请输入每人预约分钟数"
            :min="1"
            :precision="0"
          ></el-input-number>
          <!-- <el-input placeholder="请输入每人预约分钟数" style="width: 220px"  v-model="form.minutesPerPatient"/> -->
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in state"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出诊地点" prop="scheduleAddress">
          <el-input
            v-model="form.scheduleAddress"
            placeholder="请输入出诊地点"
            type="textarea"
            style="width: 570px"
          />
        </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>

    <!-- 查看明细对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-table
        v-loading="loading"
        :data="hospitalDepartmentList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="医院姓名" align="center" prop="doctorName" />
        <el-table-column label="排班日期" align="center" prop="scheduleDate" />
        <el-table-column
          label="开始时间"
          align="center"
          prop="scheduleStartTime"
        />
        <el-table-column
          label="结束时间"
          align="center"
          prop="scheduleEndTime"
        />
        <el-table-column label="预约状态" align="center" prop="applyState">
          <template slot-scope="scope">
            {{ scope.row.applyState == "0" ? "未预约" : "" }}
            {{ scope.row.applyState == "1" ? "已预约" : "" }}
          </template>
        </el-table-column>
        <el-table-column label="可预约状态" align="center" prop="status">
          <template slot-scope="scope">
            {{ scope.row.status == "0" ? "可预约" : "" }}
            {{ scope.row.status == "1" ? "不可预约" : "" }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getList, hospitalDepartment, plan, add, schedule, hospitalPerson, plandetail } from "@/api/system/Scheduling";
export default {
  name: "Scheduling",
  data() {
    return {
      /* start 开始时间小于今天,结束时间不能大于开始时间 */
      endDisabled: {
        disabledDate: time => {
          if (this.form.scheduleStartDate) {
            return (
              time.getTime() < new Date(this.form.scheduleStartDate).getTime()
            );
          } else {
            return time.getTime() >= Date.now();
          }
        }
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      hospitalList: [],// 查询医院信息
      hospitalNamelist: [],//医院名称
      hospitalDepartmentist: [],//科室
      schedulelist: [],//班次
      personNamelist: [],//医生
      hospitalId: '',//所属医院id
      departmentId: '',//部门id
      state: [{
        value: '0',
        label: '启用'

      },
      {
        value: '1',
        label: '未启用'

      }],
      // 非单个禁用
      single: true,
      addopen: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科室信息管理表格数据
      hospitalDepartmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        departmentId: "",
        departmentName: "",
        doctorId: "",
        doctorName: "",
        hospitalId: "",
        hospitalName: "",
      },
      // 表单参数
      form: {
        departmentId: "",
        departmentName: "",
        doctorId: "",
        doctorName: "",
        hospitalId: "",
        hospitalName: "",
        minutesPerPatient: "",
        scheduleAddress: "",
        scheduleEndDate: "",
        scheduleId: "",
        scheduleName: "",
        scheduleStartDate: "",
        status: "0",
      },
      // 表单校验
      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.hospitalName();
  },
  computed: {
    pickerStartTime() {
      return {
        disabledDate: time => {
          if (this.form.scheduleEndDate) {
            let edtTime = this.form.scheduleEndDate.replace(/-/g, '/');
            return time.getTime() > new Date(edtTime)

          }
        }
      }
    },
    pickerEndTime() {
      return {
        disabledDate: time => {
          if (this.form.scheduleStartDate) {
            let startTime = this.form.scheduleStartDate.replace(/-/g, '/');
            return time.getTime() < new Date(startTime)
          }

        }

      }

    }

  },
  methods: {
    // 取消按钮
    cancel() {
      this.addopen = false;
    },
    /** 查询医生排班管理列表 */
    getList() {
      this.loading = true;
      plan(this.queryParams).then((response) => {
        this.hospitalList = response.rows;
        if (response.total) {
          this.total = response.total;
        }
        this.loading = false;
      });

    },
    // 医院名称点击事件
    hospitalNamechange(value) {
      this.hospitalId = value
      // this.form.hospitalName = this.hospitalNamelist.find((f) => f.id == this.hospitalId)?.hospitalName;
      this.queryParams.departmentId = "",
        this.queryParams.doctorId = "",
        this.personNamelist=[],
        hospitalDepartment(this.hospitalId).then(response => {
          this.hospitalDepartmentist = response.data;
        });
    },
    // 科室名称
    hospitalName() {
      getList().then(response => {
        this.hospitalNamelist = response.data;
      });

    },

    // 新增医院名称点击事件
    hospitalNamechangeadd(value) {
      this.hospitalId = value
      this.form.hospitalName = this.hospitalNamelist.find((f) => f.id == this.hospitalId)?.hospitalName;
      this.form.departmentId = "",
        this.form.doctorId = "",
        this.form.scheduleId = "",
        this.personNamelist=[],
        // this.$refs.departmentId.clear()
        // 科室名称
        hospitalDepartment(this.hospitalId).then(response => {
          this.hospitalDepartmentist = response.data;
        });
      //班次名称
      schedule(this.hospitalId).then(response => {
        this.schedulelist = response.data;
      });
    },
    // 新增科室名称点击事件
    departmentNameadd(value) {
      this.departmentId = value
      this.form.departmentName = this.hospitalDepartmentist.find((f) => f.id == this.departmentId)?.departmentName;
      this.form.doctorId = "",
      this.queryParams.doctorId = "",
      hospitalPerson(this.departmentId, this.hospitalId).then(response => {
        this.personNamelist = response.data;
      });
    },
    // 医生姓名
    doctorIdadd(value) {
      this.form.doctorName = this.personNamelist.find((f) => f.id == value)?.personName;
    },
    // 班次
    scheduleIdadd(value) {
      this.form.scheduleName = this.schedulelist.find((f) => f.id == value)?.scheduleName;
    },
    // 取消按钮
    cancel() {
      this.addopen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        departmentId: "",
        departmentName: "",
        doctorId: "",
        doctorName: "",
        hospitalId: "",
        hospitalName: "",
        minutesPerPatient: "",
        scheduleAddress: "",
        scheduleEndDate: "",
        scheduleId: "",
        scheduleName: "",
        scheduleStartDate: "",
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        departmentId: "",
        departmentName: "",
        doctorId: "",
        doctorName: "",
        hospitalId: "",
        hospitalName: "",
      };
      this.personNamelist=[],
      this.hospitalDepartmentist=[],
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.hospitalName();
      this.addopen = true;
      this.title = "添加医生排班";
    },
    /** mi按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "查看明细";
      const schedulePlanId = row.id || this.ids;
      plandetail(schedulePlanId).then((response) => {
        this.hospitalDepartmentList = response.data;
        this.open = true;
        this.title = "查看明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate((valid) => {
        if (valid) {
          add(this.form).then((response) => {
            if (response.code == 200) {
              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>
