<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="classifyCode">
        <el-input
          v-model="queryParams.classifyCode"
          placeholder="请输入分类编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="classifyName">
        <el-input
          v-model="queryParams.classifyName"
          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:classifyItem: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:classifyItem: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:classifyItem: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="['classify:info:export']"
          >导出</el-button
        >
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类编码" align="center" prop="classifyCode" />
      <el-table-column label="分类名称" align="center" prop="classifyName" />
      <!-- <el-table-column label="分类类型" align="center" prop="classifyType">
        <template slot-scope="scope"
          >{{ scope.row.classifyType == "NURSE_AGNECY" ? "护理机构分类" : "" }}
          {{ scope.row.classifyType == "NURSE_ITEM" ? "护理项目分类" : "" }}
        </template>
      </el-table-column>-->
      <el-table-column label="分类排序" align="center" prop="classifySort" />
      <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:classifyItem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:classifyItem: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
      :before-close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="true"
        :before-close="cancel"
      >
        <div
          v-for="(item, index) in form.nurseClassifyInfoList"
          :key="index"
          style="margin-bottom: 30px"
        >
          <el-form-item
            label="分类名称"
            :rules="rules.nurseClassifyInfoList.classifyName"
            :prop="`nurseClassifyInfoList.${index}.classifyName`"
          >
            <el-input
              maxlength="30"
              style="width: 200px"
              v-model="item.classifyName"
              placeholder="请输入分类名称"
            />
          </el-form-item>
          <el-form-item
            label="分类排序"
            :rules="rules.nurseClassifyInfoList.classifySort"
            :prop="`nurseClassifyInfoList.${index}.classifySort`"
          >
            <el-input
              style="width: 200px"
              v-model.number="item.classifySort"
              placeholder="请输入分类排序"
              oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="5"
            />
          </el-form-item>
          <el-form-item
            label="分类图标"
            :rules="rules.nurseClassifyInfoList.classifyPictureUrl"
            :prop="`nurseClassifyInfoList.${index}.classifyPictureUrl`"
          >
            <stationAcatar
              style="margin-top: -25px"
              @imgUrl="imgclassifyPictureUrl"
              @item="imgclassifyPictureitem"
              :img="item.classifyPictureUrl"
              :type="'nurseItemClassifyUrl'"
              :item="item"
            />
          </el-form-item>
          <el-button
            type="primary"
            plain
            icon="el-icon-circle-plus-outline"
            style="margin-top: 2px; margin-left: 50px"
            v-if="index == 0"
            @click="addnurseClassifyitem"
          ></el-button>
          <!-- circle -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            plain
            style="margin-top: 2px; margin-left: 50px"
            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="editopen"
      width="500px"
      append-to-body
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="classifyName">
          <el-input v-model="form.classifyName" placeholder="请输入分类名称" maxlength="30" />
        </el-form-item>
        <el-form-item label="分类图标" prop="classifyPictureUrl">
          <stationAcatar
            @imgUrl="imgclassifyPictureUrl"
            @item="imgclassifyPictureitem"
            :img="form.classifyPictureUrl"
            :type="'nurseItemClassifyUrl'"
            :item="form"
          />
        </el-form-item>
        <el-form-item label="分类排序" prop="classifySort">
          <el-input
            v-model.number="form.classifySort"
            placeholder="请输入分类排序"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxlength="5"
          />
        </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 stationAcatar from "../stationAvatar/index.vue";
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo
} from "@/api/system/classifyItem.js";
import { updatePicture } from "@/api/system/station";
export default {
  name: "classify",
  components: { stationAcatar },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      imgone: "",
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 护理机构和护理项目分类信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      editopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        classifyCode: null,
        classifyName: null,

        classifyLevel: null,
        classifyType: null,
        classifyPictureUrl: null,
        classifySort: null
      },
      // 表单参数
      form: {
        nurseClassifyInfoList: []
      },
      idd: 0,
      classifyPictureUrl: null,
      imgsurl: { pictureUrlList: [] },
      // 表单校验
      rules: {
        nurseClassifyInfoList: {
          classifyName: [
            {
              required: true,
              message: "请输入分类名称",
              trigger: "blur"
            }
          ],
          classifyPictureUrl: [
            {
              required: true,
              message: "请选择分类图标",
              trigger: "blur"
            }
          ],
          classifySort: [
            {
              required: true,
              message: "请输入分类排序",
              trigger: "blur"
            }
          ]
        },
        classifyName: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur"
          }
        ],
        classifyPictureUrl: [
          {
            required: true,
            message: "请选择分类图标",
            trigger: "blur"
          }
        ],
        classifySort: [
          {
            required: true,
            message: "请输入分类排序",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //添加时删除item
    delnurseClassifyitem(index) {
      this.form.nurseClassifyInfoList.splice(index, 1);
    },
    //添加时新增item
    addnurseClassifyitem() {
      this.idd++;
      var obj = {
        classifyName: "",
        classifyPictureUrl: "",
        classifySort: null,
        idd: this.idd
      };
      if (this.form.nurseClassifyInfoList.length == 5) {
        this.$message.error("最多批量添加5条");
      } else {
        this.form.nurseClassifyInfoList.push(obj);
      }
    },
    //接收图片
    imgclassifyPictureUrl(e) {},
    //接收图片所属的item
    imgclassifyPictureitem(item) {
      let items = JSON.parse(item);
      if (items.idd && !items.id) {
        this.form.nurseClassifyInfoList.forEach(e => {
          if (e.idd == items.idd) {
            e.classifyPictureUrl = items.classifyPictureUrl;
          }
        });
      } else if (!items.idd && items.id) {
        this.form.classifyPictureUrl = items.classifyPictureUrl;
      }
    },
    /** 查询护理机构和护理项目分类信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      if (this.editopen) {
        var obj = { pictureUrlList: [] };
        if (this.form.classifyPictureUrl != this.imgone) {
          obj.pictureUrlList.push(this.form.classifyPictureUrl);
        }
        updatePicture(obj).then(res => {
          this.editopen = false;
        });
      }
      if (this.open) {
        var obj = { pictureUrlList: [] };
        if (this.form.nurseClassifyInfoList) {
          this.form.nurseClassifyInfoList.forEach(e => {
            obj.pictureUrlList.push(e.classifyPictureUrl);
          });
        }
        updatePicture(obj).then(res => {
          this.open = false;
        });
      }
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        classifyCode: null,
        classifyName: null,
        classifyLevel: null,
        classifyType: null,
        classifyPictureUrl: null,
        classifySort: 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.form = {
        nurseClassifyInfoList: [
          {
            classifyName: "",
            classifyPictureUrl: "",
            classifySort: null,
            idd: this.idd
          }
        ]
      };
      this.open = true;
      this.title = "添加护理项目分类信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then(response => {
        this.form = response.data;
        this.imgone = this.form.classifyPictureUrl;
        this.classifyPictureUrl = response.data.classifyPictureUrl;
        this.editopen = true;
        this.title = "修改护理项目分类信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.classifySort = Number(this.form.classifySort);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              if (this.form.classifyPictureUrl != this.imgone) {
                var obj = { pictureUrlList: [] };
                obj.pictureUrlList.push(this.imgone);
                updatePicture(obj).then(res => {});
              }
              this.editopen = false;
              this.getList();
            });
          } else {
         
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log(row);
      this.$modal
        .confirm("是否确认删除？")
        .then(function() {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "classify/info/export",
        {
          ...this.queryParams
        },
        `info_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
