<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="上级分组id" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          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="groupCode">
        <el-input
          v-model="queryParams.groupCode"
          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:projectgroup: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:projectgroup: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:projectgroup: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:projectgroup:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="projectgroupList"
      @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="parentId" /> -->
      <el-table-column label="分组名称" align="center" prop="groupName" />
      <el-table-column label="分组编码" align="center" prop="groupCode" />
      <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="groupSort" />
      <el-table-column
        label="备注信息"
        align="center"
        prop="groupRemark"
        :show-overflow-tooltip="true"
      />
      <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="['manage:projectgroup:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:projectgroup: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="1000px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <div v-for="(aitem, index) in form.list" :key="index">
          <el-form-item
            label="分组名称"
            :rules="rules.list.groupName"
            :prop="`list.${index}.groupName`"
          >
            <el-input
              v-model="aitem.groupName"
              maxlength="100"
              placeholder="请输入分组名称"
              style="width: 250px"
            />
          </el-form-item>
          <el-form-item
            label="分组编码"
            :rules="rules.list.groupCode"
            :prop="`list.${index}.groupCode`"
          >
            <el-input
              onKeyUp="value=value.replace(/[\W]/g,'')"
              maxlength="100"
              v-model="aitem.groupCode"
              placeholder="请输入分组编码"
              style="width: 250px"
            />
          </el-form-item>
          <el-form-item
            label="是否启用"
            :rules="rules.list.enableStatus"
            :prop="`list.${index}.enableStatus`"
          >
            <el-radio-group v-model="aitem.enableStatus" size="small">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- <br /> -->
          <el-form-item
            label="排序"
            :rules="rules.list.groupSort"
            :prop="`list.${index}.groupSort`"
          >
            <el-input-number
              v-model="aitem.groupSort"
              controls-position="right"
              :min="0"
              style="width: 250px"
            />
          </el-form-item>
          <el-form-item
            label="备注信息"
            :rules="rules.list.groupRemark"
            :prop="`list.${index}.groupRemark`"
          >
            <el-input
              maxlength="100"
              v-model="aitem.groupRemark"
              placeholder="请输入备注信息"
              style="width: 250px"
            />
          </el-form-item>
          <!-- <br /> -->
          <el-button
            type="primary"
            plain
            icon="el-icon-circle-plus-outline"
            style="margin-top: 2px; margin-left: 6px"
            v-if="index == 0"
            @click="addnurseClassifyitem"
          ></el-button>
          <!-- circle -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            plain
            style="margin-top: 2px; margin-left: 6px"
            v-if="index != 0"
            @click="delnurseClassifyitem(index)"
          ></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="title"
      :visible.sync="openedit"
      width="1000px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <el-form-item label="分组名称" prop="groupName">
          <el-input
            maxlength="100"
            v-model="form.groupName"
            placeholder="请输入分组名称"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="分组编码" prop="groupCode">
          <el-input
            maxlength="100"
            v-model="form.groupCode"
            placeholder="请输入分组编码"
            style="width: 250px"
          />
        </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>
        <!-- <br /> -->
        <el-form-item label="排序" prop="groupSort">
          <el-input-number
            v-model="form.groupSort"
            controls-position="right"
            :min="0"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="备注信息" prop="groupRemark">
          <el-input
            maxlength="100"
            v-model="form.groupRemark"
            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="cancels">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { agencyList } from "@/api/system/agency";
import {
  listProjectgroup,
  getProjectgroup,
  delProjectgroup,
  addProjectgroup,
  updateProjectgroup,
} from "@/api/manage/projectgroup";

export default {
  name: "Projectgroup",
  data() {
    return {
      treeOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测项目分组表格数据
      projectgroupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openedit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        groupName: null,
        groupCode: null,
        enableStatus: null,
        groupSort: undefined,
        groupRemark: null,
      },
      // 表单参数
      form: {},
      // formedit:{},
      // 表单校验
      rules: {
        groupName: [
          {
            required: true,
            message: "请输入分组名称",
            trigger: "blur",
          },
        ],
        list: {
          groupName: [
            {
              required: true,
              message: "请输入分组名称",
              trigger: "blur",
            },
          ],
          groupCode: [
            {
              required: true,
              message: "请输入分组编码",
              trigger: "blur",
            },
          ],
        },
      },
      isResouceShow: 1,
    };
  },
  created() {
    this.gettreelist();

    this.getList();
  },
  methods: {
    //添加时删除item
    delnurseClassifyitem(index) {
      this.form.list.splice(index, 1);
    },
    //添加时新增item
    addnurseClassifyitem() {
      this.idd++;
      var obj = {
        groupName: null,
        id: null,
        // parentId: null,
        groupCode: null,
        enableStatus: null,
        groupSort: undefined,
        groupRemark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        serviceContent: null,
        idd: this.idd,
      };
      if (this.form.list.length == 5) {
        this.$message.error("最多批量添加5条");
      } else {
        this.form.list.push(obj);
        console.log(this.form);
      }
    },
    // 机构树
    gettreelist() {
      agencyList().then((res) => {
        // 接口请求
        // this.deptOptions = res.data;

        this.treeOptions = res.data.map((item) => {
          return { value: item.id, ...item };
        });
        // this.treeOption = {
        //   lazy: true,
        //   checkStrictly: true,
        //   lazyLoad: this.lazyLoad,
        // };
        console.log(res.data, "58", this.treeOptions);
      });
    },
    change(e) {
      console.log(e, "78");
      // console.log(this.$refs.cascader)
      // this.form.parentId = e;
      if (e) {
        this.form.parentId = e[e.length - 1];
      }
    },
    /** 查询检测项目分组列表 */
    getList() {
      this.loading = true;
      listProjectgroup(this.queryParams).then((response) => {
        this.projectgroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancels() {
      this.openedit = false;
      this.reset();
    },
    // enableStatus
    // 表单重置
    reset() {
      this.form = {
        groupName: null,
        id: null,
        // parentId: null,
        groupCode: null,
        enableStatus: 1,
        groupSort: undefined,
        groupRemark: null,
        // delFlag: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        list: [
          {
            groupName: null,
            id: null,
            // parentId: null,
            groupCode: null,
            enableStatus: null,
            groupSort: undefined,
            groupRemark: null,
            // delFlag: null,
            // createBy: null,
            // createTime: null,
            // updateBy: null,
            // updateTime: null,
            idd: this.idd,
          },
        ],
      };
      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.list.enableStatus = "1";
      this.title = "添加检测项目分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset();
      const id = row.id || this.ids;
      getProjectgroup(id).then((response) => {
        this.form = response.data;
        // if(response.data.)
        response.data.enableStatus;
        this.openedit = true;
        this.title = "修改检测项目分组";
      });
    },
    /** 提交按钮 */

    submitForm() {
      console.log(this.form);

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProjectgroup(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.openedit = false;
              this.getList();
            });
          } else {
            addProjectgroup(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 delProjectgroup(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "manage/projectgroup/export",
        {
          ...this.queryParams,
        },
        `projectgroup_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
