<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      @submit.native.prevent
    >
      <el-form-item label="手术名称" prop="operationName">
        <el-input
          v-model="queryParams.operationName"
          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="['operationInfo:operationInfo:add']"
          >新增手术</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="5">
        <div class="left" ref="box">
          <div class="name">科室名称</div>
          <div>
            <el-input
              v-model="name"
              placeholder="请输入科室名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </div>
          <div class="listitem">
            <div
              :class="itemname == null ? 'allactive' : 'all'"
              @click="itemdata()"
            >
              全部
            </div>
            <span class="count">{{ count }}</span>
          </div>
          <div
            class="listitem"
            v-for="(item, index) in DepartmentoList"
            :key="index"
            @click="itemdata(item)"
          >
            <div :class="itemname == item.id ? 'allactive' : 'all'">
              {{ item.departmentName }}
            </div>
            <span class="count">{{ item.countNum }}</span>
          </div>
        </div>
      </el-col>
      <el-col :span="19">
        <el-table
          v-loading="loading"
          :data="operationInfoList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="序号"
            align="center"
            prop="sort"
            type="index"
          />

          <el-table-column
            label="手术名称"
            align="center"
            prop="operationName"
          />

          <el-table-column
            label="术式信息"
            align="center"
            prop="operationInfo"
          />

          <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="['operationInfo:operationInfo:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['operationInfo:operationInfo: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-col>
    </el-row>

    <!-- 添加或修改手术信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="科室名称" prop="departmentId">
          <el-button
            type=""
            v-if="departmentName == '请选择科室'"
            @click="clickinnerVisible()"
            style="
              width: 379px;
              text-align: left;
              height: 36px;
              color: #c0c4cc;
              overflow: hidden;
            "
            >{{ departmentName }}</el-button
          >
          <el-button
            @click="clickinnerVisible()"
            type=""
            v-else
            style="
              width: 379px;
              text-align: left;
              height: 36px;
              padding-left: -10px;
              overflow: hidden;
            "
            >{{ departmentName }}</el-button
          >
        </el-form-item>
        <!-- <el-form-item label="所属科室" prop="departmentName">
          <el-input
            v-model="form.departmentName"
            placeholder="请输入所属科室"
            disabled
          />
        </el-form-item> -->
        <el-form-item label="手术名称" prop="operationName">
          <el-input
            v-model="form.operationName"
            placeholder="请输入手术名称"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item label="手术排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
            placeholder="请输入手术排序"
            style="width: 380px"
          />
        </el-form-item>

        <el-form-item label="术式信息" prop="operationInfo">
          <el-input
            v-model="form.operationInfo"
            placeholder="请输入术式信息"
            type="textarea"
          />
        </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=""
      :visible.sync="innerVisibleshow"
      width="1000px"
      append-to-body
      :before-close="innerVisiblecancel"
    >
      <el-form
        ref="queryForm"
        :model="informationqueryParams"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <el-form-item label="科室名称" prop="departmentName" label-width="120">
          <el-input
            v-model="informationqueryParams.departmentName"
            placeholder="请输入科室名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="informationInfoinfo"
            >搜索</el-button
          >
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="addresetQuerylist"
            >重置</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.departmentId == 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="departmentName"
          label="科室名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      </el-table>
      <pagination
        v-show="totaldepartment > 0"
        :total="totaldepartment"
        :page.sync="informationqueryParams.pageNum"
        :limit.sync="informationqueryParams.pageSize"
        @pagination="informationInfoinfo"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listOperationInfo, getOperationInfo, delOperationInfo, addOperationInfo, updateOperationInfo, listOperationNum } from "@/api/operationInfo/operationInfo";
import { department, listDisease } from "@/api/manage/script";
export default {
  name: "OperationInfo",
  data() {
    return {
      obj: null,
      disabled: false,
      itemname: null,
      departmentName: null,
      diseaseTypeName: null,
      name: '',
      departmentId: null,
      count: '',


      innerVisibleshow: false, //科室弹框
      // 科室
      informationqueryParams: {
        pageNum: 1,
        pageSize: 10,
        departmentName: null,
      },
      infolist: [],
      totaldepartment: 0,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手术信息表格数据
      operationInfoList: [],
      DepartmentoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        departmentId: null,
        departmentName: null,
        operationName: null,
        operationCode: null,
        operationInfo: null,
        operationRemark: null,
        sort: null,
      },
      querydepartmen: {
        departmentName: "",
        // pageNum: 1,
        // pageSize: 10,

      },
      // 表单参数
      form: {
        id: null,
        departmentId: null,
        departmentName: null,
        operationName: null,
        sort: null,
        operationCode: null,
        operationInfo: null,
        operationRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      },
      // 表单校验
      rules: {
        departmentId: [
          { required: true, message: "所属科室名称不能为空", trigger: "blur" }


        ],
        operationName: [
          { required: true, message: "手术名称不能为空", trigger: "blur" }
        ],
        operationInfo: [
          { required: true, message: "术式信息不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "手术排序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.Departmentlist();
  },
  watch: {
    name(val) {
      this.querydepartmen.departmentName = val
      this.Departmentlist();
    },
    'form.sort'(val) {
      if (val) {
        var name = ''
        name = val.toString()
        this.obj = name.split('')
        // if (this.obj.length > 10) {
        //   this.$message({
        //     type: "error",
        //     message: "手术排序最大长度为十位，请正确输入！"
        //   });
        // }
      }
    }
  },
  mounted() {
    // this.$refs.box.addEventListener('scroll', this.lazyLoading) // 滚动到底部，再加载的处理事件
  },
  methods: {
    // 点击科室
    clickinnerVisible() {
      this.innerVisibleshow = true;
      if (this.itemname) {
        this.informationqueryParams.departmentId = this.itemname

      } else {
        this.informationqueryParams.departmentId = null
      }
      this.informationInfoinfo();
    },
    // 科室列表
    informationInfoinfo() {
      department(this.informationqueryParams).then((response) => {
        this.infolist = response.rows;
        this.totaldepartment = response.total;
        this.loading = false;
      });
      // this.informationqueryParams.page = 1;
    },
    // 科室名称重置
    addresetQuerylist() {
      this.informationqueryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.informationInfoinfo();
    },
    innerVisiblecancel() {
      this.innerVisibleshow = false;
    },
    // 科室名称圆点按钮
    nurseclick(row) {
      this.form.departmentId = row.id;
      this.departmentName = row.departmentName;
      this.form.diseaseTypeId = '';
      this.diseaseTypeName = '请选择病种';
      this.innerVisibleshow = false;
    },
    // 左侧科室
    itemdata(item) {
      if (item) {
        this.itemname = item.id
        this.departmentName = item.departmentName
        this.loading = true;
        this.queryParams.departmentId = item.id
        this.getList()
      } else {
        this.queryParams.departmentId = ''
        this.itemname = null
        this.getList()
      }
    },
    // 左侧科室
    Departmentlist() {
      this.loading = true;
      listOperationNum(this.querydepartmen).then(response => {
        this.DepartmentoList = response.data;
        let sum = 0;
        this.DepartmentoList.forEach((item) => {
          if (item.countNum != null) {
            sum += item.countNum;
          }
          this.count = sum;
        });
        this.loading = false;
      });

    },
    /** 查询手术信息列表 */
    getList() {
      this.loading = true;
      listOperationInfo(this.queryParams).then(response => {
        this.operationInfoList = response.rows;
        this.operationInfoList.forEach(e => {
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.form.operationName = '';
      this.form.operationInfo = '';
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        departmentId: null,
        departmentName: null,
        operationName: null,
        operationCode: null,
        operationInfo: null,
        operationRemark: null,
        sort: undefined,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.itemname) {
        this.queryParams.departmentId = this.itemname

      } else {
        this.queryParams.departmentId = null;
        this.itemname = null;

      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.departmentId = null;
      this.itemname = null;
      this.resetForm("queryForm");
      this.getList();
      // this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if (this.itemname) {
        this.form.departmentName = this.departmentName
        this.form.departmentId = this.itemname
      } else if (this.itemname == null) {
        this.departmentName = "请选择科室"
      }
      else {
        this.departmentName = "请选择科室"
      }

      this.open = true;
      this.title = "添加手术信息";

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.operationInfo = '';
      this.open = true;
      const id = row.id || this.ids
      getOperationInfo(id).then(response => {
        this.form = response.data;
        this.departmentName = response.data.departmentName;
        if (!this.form.sort) {
          this.form.sort = undefined
        }
        this.open = true;
        this.title = "修改手术信息";
        if (this.title == "修改手术信息") {
          // this.disabled = true
        }
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.form.departmentName = this.departmentName
      console.log(this.form, '0000')
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.obj.length > 10) {
            this.$message({
              type: "error",
              message: "手术排序最大长度为十位，请正确输入！"
            });
          } else {
            if (this.form.id != null) {
              updateOperationInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.Departmentlist();
              });
            } else {
              if (this.itemname) {
                this.form.departmentName = this.departmentName
                this.form.departmentId = this.itemname

              } else {
                this.form.departmentName = this.departmentName
              }
              addOperationInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.Departmentlist();
              });
            }
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确认要删除该手术？').then(function () {
        return delOperationInfo(ids);
      }).then(() => {
        this.getList();
        this.Departmentlist();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('operationInfo/operationInfo/export', {
        ...this.queryParams
      }, `operationInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}

.left {
  height: 530px;
  overflow: auto;
  .name {
    font-weight: 700;
    margin-bottom: 10px;
  }
  .listitem {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #dcdfe6;
    .count {
      display: inline-block;
      position: relative;
      // right: -172px;
      left: 210px;
      color: #a4a6aa;
      top: -35px;
      font-size: 13px;
    }
    .all {
      height: 50px;
      line-height: 50px;
      padding-left: 13px;
    }
    .allactive {
      background: #e8f4ff;
      height: 50px;
      line-height: 50px;
      padding-left: 13px;
      border-left: 3px solid #4d9de7;
    }
  }
}
.button {
  display: inline-block;
  position: relative;
  left: 20px;
  top: -11px;
}
</style>
