<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="postId">
        <el-select
          v-model="queryParams.postId"
          clearable
          placeholder="请选择岗位"
        >
          <el-option
            v-for="item in postOptions"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
            :disabled="item.status == 1"
          ></el-option>
        </el-select>
      </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="['app:staff:add']"
          >新增</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="['app:staff:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="staffList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" width="80" /> -->
      <el-table-column label="姓名" align="center" prop="realname" width="80"  />
      <el-table-column label="岗位" align="center" prop="posts" min-width="140">
        <template slot-scope="scope">
          <el-tag style="margin: 0 5px;" v-for="(item,i) in scope.row.posts" :key="i">{{ item.postName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" width="120"  />
      <el-table-column label="技术职称" align="center" prop="technicalTitle" width="100"  />
      <el-table-column
        label="证书"
        align="center"
        prop="technicalCert"
        min-width="160"
      >
        <template slot-scope="scope">
          <div class="flex-div">
            <div v-for="pic in scope.row.technicalCert" :key="pic">
              <img
                :src="getFullAddr(pic)"
                object-fit="cover"
                class="img-row"
              />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="健康证明"
        align="center"
        prop="healthCert"
        min-width="160"
      >
        <template slot-scope="scope">
          <div class="flex-div">
            <div v-for="pic in scope.row.healthCert" :key="pic">
              <img
                :src="getFullAddr(pic)"
                object-fit="cover"
                class="img-row"
              />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:staff:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:staff: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="isView" label-width="80px">
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="岗位" prop="postIds">
          <el-select v-model="form.postIds" multiple clearable placeholder="请选择岗位">
            <el-option
              v-for="item in postOptions"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="技术职称" prop="technicalTitle">
          <el-input
            v-model="form.technicalTitle"
            placeholder="请输入技术职称"
          />
        </el-form-item>
        <el-form-item label="证书" prop="technicalCert">
          <ImageUpload v-model="form.technicalCert" />
        </el-form-item>
        <el-form-item label="健康证明" prop="healthCert">
          <ImageUpload v-model="form.healthCert" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStaff,
  getStaff,
  delStaff,
  addStaff,
  updateStaff,
} from "@/api/app/staff";
import { getAllPost } from "@/api/system/post";
import ImageUpload from "@/components/ImageUpload";
import { isExternal } from "@/utils/validate";
export default {
  name: "Staff",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      //是否查看
      isView:false,
      // 是否显示弹出层
      open: false,
      // 岗位选项
      postOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        realname: null,
        postId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realname: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 1, max: 10, message: '姓名输入超出最大长度限制（10位）', trigger: 'blur' }
        ],
        postIds: [{ required: true, message: "岗位不能为空", trigger: "change" }],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {
              pattern: /^1[3-9]\d{9}$/,
              message: '联系电话格式输入错误',
              trigger: 'blur'
          },
        ],
        technicalTitle: [
          { min: 0, max: 10, message: '技术职称输入超出最大长度限制（10位）', trigger: 'blur' }
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getPostSelect();
  },
  methods: {
    //拼接完整地址
    getFullAddr(url) {
      if (url.indexOf(this.baseUrl) === -1 && !isExternal(url)) {
        url = this.baseUrl + url;
      }
      return url;
    },
    //获取岗位选择器
    getPostSelect() {
      getAllPost().then((response) => {
        this.postOptions = response.data;
      });
    },
    /** 查询人员列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then((response) => {
        this.staffList = response.rows;
        this.staffList.map((item) => {
          item.healthCert = item.healthCert
            ? item.healthCert.split(",")
            : [];
          item.technicalCert = item.technicalCert
            ? item.technicalCert.split(",")
            : [];
          return item;
        });
        console.log(this.staffList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        realname: null,
        postIds: null,
        phone: null,
        technicalTitle: null,
        technicalCert: null,
        healthCert: 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.isView = false;
      this.title = "添加人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStaff(id).then((response) => {
        this.form = response.data;
        this.$set(this.form, "postIds", response.postIds);
        this.open = true;
        this.isView = false;
        this.title = "修改人员";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getStaff(id).then((response) => {
        this.form = response.data;
        this.$set(this.form, "postIds", response.postIds);
        this.open = true;
        this.isView = true;
        this.title = "查看人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStaff(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStaff(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 delStaff(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "app/staff/export",
        {
          ...this.queryParams,
        },
        `staff_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.flex-div {
  display: flex;
  justify-content: center;
}
.img-row {
  width: 50px;
  height: 50px;
  margin: 0 10px;
}
</style>
