<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="rescueTeamId">
        <el-input
          v-model="queryParams.rescueTeamId"
          placeholder="请输入应急救援队伍id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="队伍名称" prop="rescueTeamName">
        <el-input
          v-model="queryParams.rescueTeamName"
          placeholder="请输入应急救援队伍名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="人员姓名" prop="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

<!--      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="队内职务" prop="teamPost">
        <el-input
          v-model="queryParams.teamPost"
          placeholder="请输入队内职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="工作年限" prop="workYear">
        <el-input
          v-model="queryParams.workYear"
          placeholder="请输入工作年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="救援技能特长" prop="speciality">
        <el-input
          v-model="queryParams.speciality"
          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">
        <!--v-hasPermi="['ehs:ehsRescueTeamWorker:add']"-->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <!--v-hasPermi="['ehs:ehsRescueTeamWorker:edit']"-->
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <!--v-hasPermi="['ehs:ehsRescueTeamWorker:remove']"-->
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <!--v-hasPermi="['ehs:ehsRescueTeamWorker:export']"-->
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >批量导入</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ehsRescueTeamWorkerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ Number(scope.$index) + 1 }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="应急救援队伍id" align="center" prop="rescueTeamId" />-->
      <el-table-column label="队伍名称" align="center" prop="rescueTeamName" />
      <el-table-column label="人员姓名" align="center" prop="workerName" />
      <el-table-column label="年龄" align="center" prop="workerAge" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>

      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="队内职务" align="center" prop="teamPost" />
      <el-table-column label="工作年限" align="center" prop="workYear" />
      <el-table-column label="救援技能特长" align="center" prop="speciality" />
      <el-table-column label="培训次数" align="center" prop="trainNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--v-hasPermi="['ehs:ehsRescueTeamWorker:edit']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <!--v-hasPermi="['ehs:ehsRescueTeamWorker:remove']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</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="80px">
<!--        <el-form-item label="应急救援队伍id" prop="rescueTeamId">
          <el-input v-model="form.rescueTeamId" placeholder="请输入应急救援队伍id" />
        </el-form-item>-->
        <el-form-item label="队伍名称" prop="rescueTeamName">
          <el-input v-model="form.rescueTeamName" :readonly="true" placeholder="请输入应急救援队伍名称" />
        </el-form-item>

<!--        <el-form label-width="80px" :model="form" :rules="rules">-->
          <el-form-item label="人员姓名" prop="workerName">
            <el-input v-model="form.workerName" placeholder="请输入人员姓名" />
          </el-form-item>
          <el-form-item label="年龄" prop="workerAge">
            <el-input v-model="form.workerAge" placeholder="请输入年龄" />
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-select v-model="form.gender" placeholder="请选择性别">
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="身份证号" prop="idCard">
            <el-input v-model="form.idCard" placeholder="请输入身份证号" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入联系方式" />
          </el-form-item>
          <el-form-item label="队内职务" prop="teamPost">
            <el-input v-model="form.teamPost" placeholder="请输入队内职务" />
          </el-form-item>
          <el-form-item label="工作年限" prop="workYear">
            <el-input v-model="form.workYear" placeholder="请输入工作年限" />
          </el-form-item>
          <el-form-item label="救援技能特长" prop="speciality">
            <el-input v-model="form.speciality" placeholder="请输入救援技能特长" />
          </el-form-item>
          <el-form-item label="培训次数" prop="trainNum">
            <el-input v-model="form.trainNum" placeholder="请输入培训次数" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
<!--        </el-form>-->
      </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>

    <!-- 导入 -->
    <importUpload
      ref="uploadRef"
      @ok="upLoadOk"
      downUrl="/ehs/ehsRescueTeamWorker/importTemplate"
      temName="应急救援队伍人员导入模板"
    ></importUpload>
  </div>
</template>

<script>
import {
  listEhsRescueTeamWorker,
  getEhsRescueTeamWorker,
  delEhsRescueTeamWorker,
  addEhsRescueTeamWorker,
  updateEhsRescueTeamWorker,
  getTeamInfo
} from '@/api/ehs/ehsRescueTeamWorker'
import importUpload from "@/components/upload/upload";

export default {
  name: "EhsRescueTeamWorker",
  components: {
    importUpload,
  },
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急救援队人员信息表格数据
      ehsRescueTeamWorkerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rescueTeamId: null,
        rescueTeamName: null,
        workerName: null,
        gender: null,
        idCard: null,
        phone: null,
        workYear: null,
        speciality: null,
        workerAge: null,
        teamPost: null,
        trainNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workerName: [
          { required: true, message: "人员姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { pattern:/^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/, message: "联系方式必须是手机或固话(区号-固话)", trigger: "change" }
        ],
      }
    };
  },
  props: {
    // rescueTeamId
    id: {
      type: Number
    }
  },
  created() {
    this.queryParams.rescueTeamId =this.id;
    this.form.rescueTeamId = this.id;
    this.getList();
  },
  methods: {
    /** 查询应急救援队人员信息列表 */
    getList() {
      this.loading = true;
      listEhsRescueTeamWorker(this.queryParams).then(response => {
        this.ehsRescueTeamWorkerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rescueTeamId: null,
        rescueTeamName: null,
        workerName: null,
        gender: null,
        idCard: null,
        phone: null,
        workYear: null,
        speciality: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        createUserId: null,
        updateUserId: null,
        workerAge: null,
        teamPost: null,
        trainNum: 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.title = "添加应急救援队人员信息";
      const rescueTeamId = this.id;
      getTeamInfo(rescueTeamId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "新增应急救援队人员信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEhsRescueTeamWorker(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急救援队人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEhsRescueTeamWorker(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEhsRescueTeamWorker(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      this.$emit('rescue');
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应急救援队人员信息编号为"' + ids + '"的数据项？').then(function() {
        return delEhsRescueTeamWorker(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
        this.$emit('rescue');
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehs/ehsRescueTeamWorker/export', {
        ...this.queryParams
      }, `应急救援人员信息_${new Date().getTime()}.xlsx`)
    },
    /** 批量导入 */
    handleImport() {
      console.log(this.$refs.uploadRef.upload);
      this.$refs.uploadRef.upload.open = true;
      this.$refs.uploadRef.upload.title = "应急救援队伍人员导入";
      this.$refs.uploadRef.upload.params = this.id;// 这个id是rescueTeamId
      this.$refs.uploadRef.upload.url =
        process.env.VUE_APP_BASE_API + "/ehs/ehsRescueTeamWorker/importData";
    },
    /** 导入成功 */
    upLoadOk() {
      // console.log("导入成功")
      this.getList();
    },
  }
};
</script>
