<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="deptId" v-show="deptShow">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择部门" clearable>
          <el-option
            v-for="dict in allDeptList"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="队伍名称" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入队伍名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="队伍级别" prop="teamLevel">
        <el-input
          v-model="queryParams.teamLevel"
          placeholder="请输入队伍级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="dutyer">
        <el-input
          v-model="queryParams.dutyer"
          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="station">
        <el-input
          v-model="queryParams.station"
          placeholder="请输入驻地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="队伍属性" prop="teamProperty">
        <el-select v-model="queryParams.teamProperty" placeholder="请选择队伍属性" clearable>
          <el-option
            v-for="dict in dict.type.team_property"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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="['ehs:ehsFloodDroughtTeam: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="['ehs:ehsFloodDroughtTeam: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="['ehs:ehsFloodDroughtTeam: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="['ehs:ehsFloodDroughtTeam:export']"
        >导出</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="ehsFloodDroughtTeamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ Number(scope.$index) + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="队伍名称" align="center" prop="teamName" />
      <el-table-column label="队伍级别" align="center" prop="teamLevel" />
      <el-table-column label="负责人" align="center" prop="dutyer" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="所属单位" align="center" prop="unit" />
      <el-table-column label="驻地" align="center" prop="station" />
      <el-table-column label="队伍属性" align="center" prop="teamProperty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.team_property" :value="scope.row.teamProperty"/>
        </template>
      </el-table-column>
      <el-table-column label="救援人员数量" align="center" prop="rescueWorkerNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <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="['ehs:ehsFloodDroughtTeam:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehs:ehsFloodDroughtTeam:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getWorker(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="队伍名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入队伍名称" />
        </el-form-item>
        <el-form-item label="队伍级别" prop="teamLevel">
          <el-input v-model="form.teamLevel" placeholder="请输入队伍级别" />
        </el-form-item>
        <el-form-item label="负责人" prop="dutyer">
          <el-input v-model="form.dutyer" 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="unit">
          <el-select v-model="form.unit" filterable placeholder="请选择所属单位" clearable>
            <el-option
              v-for="dict in allDeptList"
              :key="dict.deptName"
              :label="dict.deptName"
              :value="dict.deptName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="驻地" prop="station">
          <el-input v-model="form.station" placeholder="请输入驻地" />
        </el-form-item>
        <el-form-item label="队伍属性" prop="teamProperty">
          <el-select v-model="form.teamProperty" placeholder="请选择队伍属性">
            <el-option
              v-for="dict in dict.type.team_property"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </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>
    <!-- 人员信息 -->
    <el-drawer
      title="防汛抗旱队伍人员信息"
      :visible.sync="drawer1"
      size="70%"
      v-if="drawer1"
    >
      <tdh :id="id" @flood="flood"></tdh>
    </el-drawer>
    <!-- 导入 -->
    <importUpload
      ref="uploadRef"
      @ok="upLoadOk"
      downUrl="/ehs/ehsFloodDroughtTeam/importTemplate"
      temName="防汛抗旱队伍导入模板"
    ></importUpload>
  </div>
</template>

<script>
import { listEhsFloodDroughtTeam, getEhsFloodDroughtTeam, delEhsFloodDroughtTeam, addEhsFloodDroughtTeam, updateEhsFloodDroughtTeam } from "@/api/ehs/ehsFloodDroughtTeam";
import importUpload from "@/components/upload/upload";
import tdh from "../ehsFloodDroughtWorker/index.vue";
import {listAllDept } from "@/api/system/dept";
export default {
  name: "EhsFloodDroughtTeam",
  dicts: ['team_property'],
  components: {
    importUpload,
    tdh,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //是否显示部门列表查询条件
      deptShow: false,
      curDeptId:-1,//当前部门
      //所有部门列表字典
      allDeptList: [],
      // 总条数
      total: 0,
      // 防汛抗旱队伍管理表格数据
      ehsFloodDroughtTeamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamName: null,
        teamLevel: null,
        deptId: null,
        teamProperty: null,
        dutyer: null,
        phone: null,
        unit: null,
        station: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teamName: [
          { required: true, message: "队伍名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          /*{ pattern:/^((0\d{2,3}-\d{7,8})|(1[345678]\d{9}))$/, message: "联系方式必须是手机或固话(区号-固话)", trigger: "change" }*/
        ],
      },
      // teamId
      id: "",
      drawer1: false,
    };
  },
  created() {
    this.getList();
    this.getListAllDept();
    this.deptShow = this.$store.state.user.parentId==0 ?true :false;
    this.curDeptId = this.$store.state.user.deptId;
  },
  methods: {
    flood(){
      this.resetQuery()
    },
    /** 查询防汛抗旱队伍管理列表 */
    getList() {
      this.loading = true;
      listEhsFloodDroughtTeam(this.queryParams).then(response => {
        this.ehsFloodDroughtTeamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 单位信息列表 */
    getListAllDept() {
      console.log(this.$store.state.user.deptId);
      this.loading = true;
      listAllDept().then(response => {
        this.allDeptList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamName: null,
        teamLevel: null,
        teamProperty: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        rescueWorkerNum: null,
        remark: null,
        deptId: null,
        createUserId: null,
        updateUserId: null,
        dutyer: null,
        phone: null,
        unit: null,
        station: 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 = "添加防汛抗旱队伍管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEhsFloodDroughtTeam(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) {
            updateEhsFloodDroughtTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEhsFloodDroughtTeam(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 delEhsFloodDroughtTeam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehs/ehsFloodDroughtTeam/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.url =
        process.env.VUE_APP_BASE_API + "/ehs/ehsFloodDroughtTeam/importData";
    },
    /** 导入成功 */
    upLoadOk() {
      // console.log("导入成功")
      this.getList();
    },
    /** 人员信息 */
    getWorker(row) {
      this.id = row.id;// 即teamId
      console.log(this.id);
      this.drawer1 = true;
    },

  }
};
</script>
