<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="certificateName">
        <el-input
          v-model="queryParams.certificateName"
          placeholder="请输入证书名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书编号" prop="certificateCode">
        <el-input
          v-model="queryParams.certificateCode"
          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:certificate: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:certificate: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:certificate: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="['system:certificate:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="certificateList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="证书名称" align="center" prop="certificateName" />
      <el-table-column label="证书编号" align="center" prop="certificateCode" />
      <el-table-column
        label="证书显示顺序"
        align="center"
        prop="certificateSort"
      />
      <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:certificate:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:certificate: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="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <el-form-item label="所属人员" prop="hospitalPersonId">
          <el-button
            type=""
            v-if="form.personName == '请选择所属人员'"
            @click="clickinnerVisible()"
            style="
              width: 250px;
              text-align: left;
              height: 36px;
              color: #c0c4cc;
              overflow: hidden;
            "
            >{{ form.personName }}</el-button
          >
          <el-button
            @click="clickinnerVisible()"
            type=""
            v-else
            style="
              width: 250px;
              text-align: left;
              height: 36px;
              padding-left: -10px;
              overflow: hidden;
            "
            >{{ form.personName }}</el-button
          >
        </el-form-item>
        <el-form-item label="证书名称" prop="certificateName">
          <el-input
            v-model="form.certificateName"
            style="width: 250px"
            placeholder="请输入证书名称"
          />
        </el-form-item>
        <el-form-item label="证书显示顺序" prop="certificateSort">
          <el-input
            v-model.number="form.certificateSort"
            style="width: 250px"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxLength="5"
            placeholder="请输入证书显示顺序"
          />
        </el-form-item>
        <div
          v-for="(item, index) in form.certificateUrlList"
          :key="index"
          style="margin-top: 20px; border-bottom: 1px solid #f3f3f3"
        >
          <el-form-item
            label="证书图片"
            :rules="rules.certificateUrlList.certificateUrl"
            :prop="`certificateUrlList.${index}.certificateUrl`"
          >
            <stationAcatar
              @imgUrl="imgUrl"
              @item="imgclassifyItem"
              :img="item.certificateUrl"
              :item="item"
              :type="'certificateUrl'"
            />
          </el-form-item>
          <el-button
            style="margin-top: 50px;"
            type="primary"
            circle
            plain
            icon="el-icon-circle-plus-outline"
            @click="adddisease"
            v-if="index == 0"
          ></el-button>
          <el-button
            style="margin-top: 50px; "
            type="danger"
            icon="el-icon-delete"
            circle
            plain
            @click="deldisease(index)"
            v-if="index != 0"
          ></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="选择人员"
      :visible.sync="innerVisible"
      width="1000px"
      append-to-body
      :before-close="innerVisiblecancel"
    >
      <el-form
        ref="queryForm"
        :model="hospitalqueryParams"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <el-form-item label="人员编码" prop="personCode" label-width="120">
          <el-input
            v-model="hospitalqueryParams.personCode"
            placeholder="请输入人员编码"
            clearable
          />
        </el-form-item>
        <el-form-item label="人员名称" prop="personName" label-width="120">
          <el-input
            v-model="hospitalqueryParams.personName"
            placeholder="请输入人员名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="info"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="addresetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        :data="hospitalPersonList"
        @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.hospitalPersonId == 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="personCode" label="人员编码" align="center">
        </el-table-column>
        <el-table-column
          property="personName"
          label="人员名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>

        <el-table-column
          property="personPhone"
          label="人员联系电话"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="personAddress"
          label="人员地址"
          align="center"
        >
        </el-table-column>
        <el-table-column property="cardNo" label="身份证号" align="center">
        </el-table-column>
      </el-table>
      <pagination
        v-show="total2 > 0"
        :total="total2"
        :page.sync="hospitalqueryParams.pageNum"
        :limit.sync="hospitalqueryParams.pageSize"
        @pagination="info"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listCertificate,
  getCertificate,
  delCertificate,
  addCertificate,
  listHospitalPerson,
  updateCertificate,
} from "@/api/system/certificate";
import stationAcatar from "../stationAvatar/index.vue";
import { updatePicture } from "@/api/system/station";
export default {
  components: { stationAcatar },
  name: "Certificate",
  data() {
    return {
      idd: 0,
      hospitalqueryParams: {
        pageNum: 1,
        pageSize: 10,
        personCode: "",
        personName: "",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健康咨询-科室人员资质证书信息表格数据
      certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deletNewImgs:[],
      objitem:[],
      imgsurl: { pictureUrlList: [] },
      innerVisible: false,
      hospitalPersonList: [],
      total2: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitalPersonId: null,
        certificateName: null,
        certificateCode: null,
        certificateUrl: null,
        certificateSort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        certificateUrlList: {
          certificateUrl: [
            {
              required: true,
              message: "请上传图片",
              trigger: "blur",
            },
          ],
        },
        certificateSort: [
          {
            required: true,
            message: "请输入顺序",
            trigger: "blur",
          },
        ],
        certificateName: [
          {
            required: true,
            message: "请输入证书名称",
            trigger: "blur",
          },
        ],
        hospitalPersonId: [
          {
            required: true,
            message: "请选择所属人员",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //删除deldisease
    deldisease(index) {
      this.form.certificateUrlList.splice(index, 1);
    },
    adddisease() {
      this.idd++;
      var obj = {
        certificateUrl: "",
        idd: this.idd
      };
      if (this.form.certificateUrlList.length == 5) {
        this.$message.error("最多只能5条");
      } else {
        this.form.certificateUrlList.push(obj);
      }
    },
      //接收图片所属的item  
      imgclassifyItem(item) {
        let imgUrlData = JSON.parse(item);
      // 存贮新上传的图片数组
      this.deletNewImgs.push(imgUrlData.certificateUrl);
      this.objitem.push(imgUrlData.certificateUrl);
      let items = JSON.parse(item);
      if (items.idd && !items.id) {
        this.form.certificateUrlList.forEach(e => {
          if (e.idd == items.idd) {
            e.certificateUrl = items.certificateUrl;
          }
        });
      } else if (!items.idd && items.id) {
        this.form.certificateUrl = items.certificateUrl;
      }
    },
    imgUrl(imgUrl) {
     
    },
    innerVisiblecancel() {
      this.innerVisible = false;
    },
    nurseclick(row) {
      this.form.personName = row.personName;
      this.form.hospitalPersonId = row.id;
      this.innerVisible = false;
    },
    /** 查询健康咨询-科室人员信息列表 */
    info() {
      this.loading = true;
      listHospitalPerson(this.hospitalqueryParams).then((response) => {
        this.hospitalPersonList = response.rows;
        this.total2 = response.total;
        this.loading = false;
      });
    },
    // 重置
    addresetQuery() {
      this.hospitalqueryParams = {
        pageNum: 1,
        pageSize: 10,
        personCode: "",
        personName: "",
      };
      this.info();
    },
    clickinnerVisible() {
      this.info();
      this.innerVisible = true;
    },
    /** 查询健康咨询-科室人员资质证书信息列表 */
    getList() {
      this.loading = true;
      listCertificate(this.queryParams).then((response) => {
        this.certificateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      var obj = { pictureUrlList: [] };
        if (this.form.certificateUrlList) {
          this.form.certificateUrlList.forEach((e) => {
            obj.pictureUrlList.push(e.certificateUrl);
          });
        }
        updatePicture({ pictureUrlList: this.deletNewImgs }).then(res => {
          this.open = false;
        });
       
        this.imgsurl = { pictureUrlList: [] };
 
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        certificateUrlList: [
          {
            certificateUrl: null,
          },
        ],
        id: null,
        hospitalPersonId: null,
        certificateName: null,
        certificateCode: null,
        certificateUrl: null,
        certificateSort: null,
        createBy: null,
        personName: "请选择所属人员",
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.deletNewImgs=[];
      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 = {
        certificateUrlList: [
          {
            certificateUrl:"",
            idd: this.idd
          }
        ]
      };
      this.reset();
      this.open = true;
      this.title = "添加健康咨询-科室人员资质证书信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      const hospitalPersonId = row.hospitalPersonId || this.hospitalPersonIds;
      getCertificate(hospitalPersonId).then((response) => {
        this.form = response.data;
        this.title = "修改健康咨询-科室人员资质证书信息";
      });
      this.reset();
    
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificate(this.form).then((response) => {
              if (response.code == 200) {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              }
            });
          } else {
            addCertificate(this.form).then((response) => {
              if (response.code == 200) {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hospitalPersonIds = row.hospitalPersonId || this.hospitalPersonIds; 
      this.$modal
        .confirm(
          '是否确认删除人员id为"' +
          hospitalPersonIds +
            '"的数据项？'
        )
        .then(function () {
          return delCertificate(hospitalPersonIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/certificate/export",
        {
          ...this.queryParams,
        },
        `certificate_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
