<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="所属分组id" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入所属分组id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="分组名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入分组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="项目编码" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目别名" prop="projectAlias">
        <el-input
          v-model="queryParams.projectAlias"
          placeholder="请输入项目别名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="判断模式，定量：QUANTIFY，定性：QUALITATIVE" prop="judgeMode">
        <el-input
          v-model="queryParams.judgeMode"
          placeholder="请输入判断模式，定量：QUANTIFY，定性：QUALITATIVE"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目指标最大值" prop="maxValue">
        <el-input
          v-model="queryParams.maxValue"
          placeholder="请输入项目指标最大值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目指标最小值" prop="minValue">
        <el-input
          v-model="queryParams.minValue"
          placeholder="请输入项目指标最小值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目指标默认值" prop="defaultValue">
        <el-input
          v-model="queryParams.defaultValue"
          placeholder="请输入项目指标默认值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="LIS对照" prop="lisCompare">
        <el-input
          v-model="queryParams.lisCompare"
          placeholder="请输入LIS对照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="projectSort">
        <el-input
          v-model="queryParams.projectSort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注信息" prop="projectRemark">
        <el-input
          v-model="queryParams.projectRemark"
          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="['manage:project: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="['manage:project: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="['manage:project:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:project:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @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="所属分组" align="center" prop="groupName" />
      <!-- <el-table-column label="分组名称" align="center" prop="groupName" /> -->
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目编码" align="center" prop="projectCode" />
      <el-table-column label="项目别名" align="center" prop="projectAlias" />
      <el-table-column label="是否启用" align="center" prop="enableStatus">
        <template slot-scope="scope">
          {{ scope.row.enableStatus == "0" ? "否" : "" }}
          {{ scope.row.enableStatus == "1" ? "是" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="判断模式" align="center" prop="judgeMode">
        <template slot-scope="scope">
          {{ scope.row.judgeMode == "QUANTIFY" ? "定量" : "" }}
          {{ scope.row.judgeMode == "QUALITATIVE" ? "定性" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="项目指标最大值" align="center" prop="maxValue" />
      <el-table-column label="项目指标最小值" align="center" prop="minValue" />
      <el-table-column label="项目指标默认值" align="center" prop="defaultValue" />
      <el-table-column label="LIS对照" align="center" prop="lisCompare" />
      <el-table-column label="计量单位" align="center" prop="projectUnit" />
      <el-table-column label="排序" align="center" prop="projectSort" />
      <el-table-column label="备注信息" align="center" prop="projectRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:project:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:project: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="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
        <el-form-item label="所属分组" prop="groupId">
          <el-button type="" v-if="form.groupName == '请选择所属分组'" @click="clickinnerVisible()" style="
              width: 208px;
              text-align: left;
              height: 36px;
              color: #c0c4cc;
              overflow: hidden;
            ">{{ form.groupName }}</el-button>
          <el-button @click="clickinnerVisible()" type="" v-else style="
              width: 208px;
              text-align: left;
              height: 36px;
              padding-left: -10px;
              overflow: hidden;
            ">{{ form.groupName }}</el-button>
          <!-- <el-cascader
            :props="{ checkStrictly: true }"
            :options="treeOptions"
            @change="change($event)"
            style="width: 205px"
            v-model="form.parentId"
            :show-all-levels="false"

            clearable
            ref="cascader"
            :key="isResouceShow"
          ></el-cascader> -->
          <!-- <el-select
            v-model="form.parentId"
            placeholder="请选择所属分组"
            style="width: 208px"
          >
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >

            </el-option>
          </el-select> -->
          <!-- <el-input v-model="form.groupId" placeholder="请输入所属分组" /> -->
        </el-form-item>
        <!-- <el-form-item label="分组名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入分组名称" />
        </el-form-item> -->
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" maxlength="100" />
        </el-form-item>
        <el-form-item label="项目编码" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目编码" onKeyUp="value=value.replace(/[\W]/g,'')" />
        </el-form-item>
        <el-form-item label="项目别名" prop="projectAlias">
          <el-input v-model="form.projectAlias" placeholder="请输入项目别名" maxlength="100" />
        </el-form-item>
        <el-form-item label="判断模式" prop="judgeMode">
          <el-select v-model="form.judgeMode" placeholder="请选择" style="width: 208px">
            <el-option v-for="item in options" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目指标默认值" prop="defaultValue">
          <el-input v-model="form.defaultValue" placeholder="请输入项目指标默认值" />
        </el-form-item>
        <el-form-item label="项目指标最小值" prop="minValue">
          <el-input :change="checkPrice()" v-model="form.minValue" placeholder="请输入项目指标最小值" />
        </el-form-item>
        <el-form-item label="项目指标最大值" prop="maxValue">
          <el-input :change="checkPricemax()" v-model="form.maxValue" placeholder="请输入项目指标最大值" />
        </el-form-item>

        <!-- <el-form-item label="LIS对照" prop="lisCompare">
          <el-input v-model="form.lisCompare" placeholder="请输入LIS对照" />
        </el-form-item> -->
        <el-form-item label="计量单位" prop="projectUnit">
          <el-input v-model="form.projectUnit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="排序" prop="projectSort">
          <el-input-number v-model="form.projectSort" controls-position="right" :min="0" />
          <!-- <el-input v-model="form.projectSort" placeholder="请输入排序" /> -->
        </el-form-item>
        <el-form-item label="备注信息" prop="projectRemark">
          <el-input v-model="form.projectRemark" placeholder="请输入备注信息" maxlength="100" />
        </el-form-item>
        <el-form-item label="是否启用" prop="enableStatus">
          <el-radio-group v-model="form.enableStatus" size="small">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </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="groupName" label-width="120">
          <el-input v-model="informationqueryParams.groupName" 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.groupId == 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="groupName" 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 {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/manage/project";
import { getAgencytype } from "@/api/system/agency";
import { listProjectgroup } from "@/api/manage/projectgroup";
export default {
  name: "Project",
  data() {
    return {
      innerVisibleshow: false,
      options: [
        // {
        //   value: "QUANTIFY",
        //   label: "定量",
        // },
        // {
        //   value: "QUALITATIVE",
        //   label: "定性",
        // },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科室
      informationqueryParams: {
        pageNum: 1,
        pageSize: 10,
        departmentName: null,
      },
      // 检测项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        groupName: null,
        projectName: null,
        projectCode: null,
        projectAlias: null,
        enableStatus: null,
        judgeMode: null,
        maxValue: null,
        minValue: null,
        defaultValue: null,
        lisCompare: null,
        projectUnit: null,
        projectSort: null,
        projectRemark: null,
      },
      // 表单参数
      form: {},
      infolist: [],
      totaldepartment: 0,
      // 表单校验
      rules: {
        groupId: [
          {
            required: true,
            message: "所属分组不能为空",
            trigger: "blur",
          },
        ],
        projectName: [
          {
            required: true,
            message: "项目名称不能为空",
            trigger: "blur",
          },
        ],
        projectCode: [
          {
            required: true,
            message: "项目编码不能为空",
            trigger: "blur",
          },
        ],
      },
      isResouceShow: 1,
    };
  },
  created() {
    this.getList();
    this.info();
  },
  methods: {
    checkPricemax() {
      let checkPlan = "" + this.form.maxValue;
      checkPlan = checkPlan
        .replace(/[^\d.]/g, "") // 清除“数字”和“.”以外的字符
        .replace(/\.{2,}/g, ".") // 只保留第一个. 清除多余的
        .replace(/^\./g, "") // 保证第一个为数字而不是.
        .replace(".", "$#$")
        .replace(/\./g, "")
        .replace("$#$", ".");
      if (checkPlan.indexOf(".") < 0 && checkPlan !== "") {
        // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        checkPlan = parseFloat(checkPlan) + "";
      } else if (checkPlan.indexOf(".") >= 0) {
        checkPlan = checkPlan.replace(/^()*(\d+)\.(\d\d).*$/, "$1$2.$3"); // 只能输入两个小数
      }
      this.form.maxValue = checkPlan;
    },
    /**只能输入数字且有小数点最多保留两位*/
    checkPrice() {
      let checkPlan = "" + this.form.minValue;
      checkPlan = checkPlan
        .replace(/[^\d.]/g, "") // 清除“数字”和“.”以外的字符
        .replace(/\.{2,}/g, ".") // 只保留第一个. 清除多余的
        .replace(/^\./g, "") // 保证第一个为数字而不是.
        .replace(".", "$#$")
        .replace(/\./g, "")
        .replace("$#$", ".");
      if (checkPlan.indexOf(".") < 0 && checkPlan !== "") {
        // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        checkPlan = parseFloat(checkPlan) + "";
      } else if (checkPlan.indexOf(".") >= 0) {
        checkPlan = checkPlan.replace(/^()*(\d+)\.(\d\d).*$/, "$1$2.$3"); // 只能输入两个小数
      }
      this.form.minValue = checkPlan;
    },
    info() {
      var dictType = "project_judge_mode";
      getAgencytype(dictType).then((res) => {
        this.options = res.data;
      });
    },
    // 分组名称重置
    addresetQuerylist() {
      this.informationqueryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.informationInfoinfo();
    },
    innerVisiblecancel() {
      this.innerVisibleshow = false;
    },
    // 分组名称圆点按钮
    nurseclick(row) {
      console.log(row);
      this.form.groupId = row.id;
      this.form.groupName = row.groupName;
      // console.log(this.departmentName, "0000");
      this.innerVisibleshow = false;
    },
    // 分组列表
    informationInfoinfo() {
      listProjectgroup(this.informationqueryParams).then((response) => {
        this.infolist = response.rows;
        this.totaldepartment = response.total;
        this.loading = false;
      });
    },
    // 点击分组
    clickinnerVisible() {
      this.innerVisibleshow = true;
      this.informationInfoinfo();
    },

    /** 查询检测项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        groupId: null,
        groupName: null,
        projectName: null,
        projectCode: null,
        projectAlias: null,
        enableStatus: 1,
        judgeMode: null,
        maxValue: null,
        minValue: null,
        defaultValue: null,
        lisCompare: null,
        projectUnit: null,
        projectSort: undefined,
        projectRemark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.groupName = "请选择所属分组";
      this.title = "添加检测项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProject(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检测项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (
            this.form.minValue &&
            this.form.maxValue &&
            Number(this.form.minValue) >= this.form.maxValue
          ) {
            this.$message.error("项目指标最小值必须小于项目指标最大值");
          } else {
            if (this.form.id != null) {
              updateProject(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProject(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除检测项目编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "manage/project/export",
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
