<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item  prop="proName">
      <el-input
          v-model="queryParams.proName"
          placeholder="工程名称"
          clearable
          @keyup.enter.native="handleQuery"
          maxlength="20"
        />   
      </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="['machinery:type:add']"
                            >新增</el-button
                        >
                    </el-col> 
                     <el-col :span="1.5">
                        <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        >导出
                        </el-button>
                    </el-col> 
                    <right-toolbar
                        :showSearch.sync="showSearch"
                        @queryTable="getList"
                    ></right-toolbar>
    </el-row>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bracelet:construction:export']"
        >导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="constructionList" ref="multipleTable" row-key="teamId" @selection-change="handleSelectionChange" @select="handlerSelect" @select-all="handlerSelectAll">
      <!-- <el-table-column type="selection" width="55" align="center" :reserve-selection="true" /> -->
      <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工程名称" align="center" prop="proName" sortable/>
      <el-table-column label="项目部名称" align="center" prop="departName" sortable/>
      <el-table-column label="施工班组" align="center" prop="teamName" sortable/>
      <el-table-column label="班组负责人" align="center" prop="teamLeader" sortable>
        <template slot-scope="scope">
         <div>
               <span style="color: green;">{{ scope.row.teamLeader }} \ </span><span style="color: #02a7f0;">{{ scope.row.teamPhone }}</span>
        </div>
      </template>
      </el-table-column>
      <el-table-column label="班组施工人数" align="center" prop="teamNum" sortable>
        <template slot-scope="scope">
            <div @click="openTeam(scope.row)" style="color: #02a7f0; cursor: pointer">
                {{ scope.row.teamNum}}
            </div>
        </template>
      </el-table-column>
      <el-table-column label="临时人数" align="center" prop="tempNum" sortable>
        <template slot-scope="scope">
            <div @click="openTeamTemp(scope.row)" style="color: #02a7f0; cursor: pointer">
                {{ scope.row.tempNum}}
            </div>
        </template>
      </el-table-column>
      <el-table-column label="施工时间" align="center" prop="lyTime" sortable/>
      <el-table-column label="施工预警次数" align="center" prop="warnNum" sortable>
        <template slot-scope="scope">
            <div @click="openWarn(scope.row)" style="color: #02a7f0; cursor: pointer">
                {{ scope.row.warnNum}}
            </div>
        </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="showTeam" width="1000px" height="1000px" append-to-body>
          <el-form :model="queryTeam" ref="queryFormTeam" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryTeam.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQueryTeam"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="queryTeam.sex" 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>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryTeam">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryTeam">重置</el-button>
        </el-form-item>
        </el-form>

      <el-table v-loading="loadingTwo" :data="teamList" width="600px" height = "600px" >
        <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryTeam.pageNum - 1) * queryTeam.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
        <el-table-column label="人员id" align="center" prop="id" v-if="false" />
        <el-table-column label="姓名" align="center" prop="name" sortable/>
        <el-table-column label="性别" align="center" prop="sex" sortable>
          <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
            </template>
        </el-table-column>
        <el-table-column label="身份证号" align="center" prop="idCard" width="160px" sortable/>
        <el-table-column label="电话" align="center" prop="phone" sortable/>
        <el-table-column label="安全帽编号" align="center" prop="aqmCode" />
        <el-table-column label="马甲编号" align="center" prop="mjCode" />
        <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="openImg(scope.row.base64Url)"
              v-hasPermi="['basic:constructionRecord:edit']"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="totalTwo>0"
        :total="totalTwo"
        :page.sync="queryTeam.pageNum"
        :limit.sync="queryTeam.pageSize"
        @pagination="getListTeam"
      />

      </el-dialog>

          <!-- 查看临时弹窗 -->
          <el-dialog :title="title" :visible.sync="showTeamTemp" width="1000px" height="1000px" append-to-body>
          <el-form :model="queryTeamTemp" ref="queryFormTeamTemp" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryTeamTemp.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQueryTeamTemp"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="queryTeamTemp.sex" 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>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryTeamTemp">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryTeamTemp">重置</el-button>
        </el-form-item>
        </el-form>

      <el-table v-loading="loadingThree" :data="teamTempList" width="600px" height = "600px" >
        <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryTeamTemp.pageNum - 1) * queryTeamTemp.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
        <el-table-column label="人员id" align="center" prop="id" v-if="false" />
        <el-table-column label="姓名" align="center" prop="name" sortable/>
        <el-table-column label="性别" align="center" prop="sex" sortable>
          <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
            </template>
        </el-table-column>
        <el-table-column label="身份证号" align="center" prop="idCard" sortable/>
        <el-table-column label="电话" align="center" prop="phone" sortable/>
        <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="openImg(scope.row.base64Url)"
              v-hasPermi="['basic:constructionRecord:edit']"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="totalTwo>0"
        :total="totalTwo"
        :page.sync="queryTeamTemp.pageNum"
        :limit.sync="queryTeamTemp.pageSize"
        @pagination="getListTeamTemp"
      />

      </el-dialog>

      <!-- 查看施工预警信息弹窗 -->
      <el-dialog :title="title" :visible.sync="showWarn" width="1000px" height="1000px" append-to-body>
      <el-table v-loading="loadingTwo" :data="warnList" width="600px" height = "600px" >
        <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryWarn.pageNum - 1) * queryWarn.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
        <el-table-column label="预警设备" align="center" prop="devTypeName" sortable/>
        <el-table-column label="设备名称" align="center" prop="devName" sortable/>
        <el-table-column label="预警时间" align="center" prop="warnTime" sortable/>
        <el-table-column label="预警内容" align="center" prop="warnContent" sortable/>
      </el-table>
  
      <pagination
        v-show="totalTwo>0"
        :total="totalTwo"
        :page.sync="queryWarn.pageNum"
        :limit.sync="queryWarn.pageSize"
        @pagination="getListWarn"
      />

      </el-dialog>
      <!-- 图片查看弹窗 -->
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" height="650px" :src="dialogImageUrl" alt />
      </el-dialog>
  </div>
</template> 

<script>
// import { listConstruction,listTeam,listTeamTemp,listWarn,exportConstructionRecord,exportConstructionRecordSome } from "@/api/comprehensive/constructionRecord";
import { downloadFile } from '@/utils/download'
export default {
  name: "Post",
  dicts: ['sys_normal_disable','sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,

      loadingTwo: false,

      loadingThree: false,

      // 是否显示项目部Id
      showName: false,

      // 是否显示施工预警信息
      showWarn: false,

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      //图片查看弹窗
      dialogImageUrl: '',
      dialogVisible: false, 

      // 显示搜索条件
      showSearch: true,
      showTeam: false,
      showTeamTemp: false,
      // 总条数
      total: 0,
      totalTwo : 0,
      // 施工记录表格数据
      constructionList: [],
      maxLength:100,//已选列表上限，防止数据过多请求报错
      currentSelection:[],//工程已选列表
      // 班组人员表格数据
      teamList: [],
      teamTempList: [],
      warnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: undefined,
        departName: undefined,
        teamName: undefined,
        lyTime: undefined,
      },
      // 查询参数
      queryTeam: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          sex: undefined,
          teamId: undefined,
        },
      queryTeamTemp: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          sex: undefined,
          teamId: undefined,
        },
      queryWarn: {
          pageNum: 1,
          pageSize: 10,
          teamId: undefined,
        },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
  },
  methods: {
         //页面单选操作-跨页
         handlerSelect(val, row) {
          this.currentSelection.indexOf(row.teamId) === -1
            ? this.currentSelection.push(row.teamId)
            : this.currentSelection.splice(this.currentSelection.indexOf(row.teamId), 1)
        },
        //工程页面全选操作-跨页
        handlerSelectAll(val) {
          if (val.length) {
            // 进来此处说明:
            // 1 当前页的全选 2 其他页有数据然后当前页的取消全选
            // 比较全选或者取消全选与当前页的数据，得到差集
            // 如果constructionList中的数据在val中不存在，则说明是取消全选，需要从currentSelection中移除
            // 如果constructionList中所有的数据都在val中存在，则说明是全选，需要将差集添加到currentSelection中
            const isAllSelect = this.constructionList.every(item =>
            val.some(valItem => valItem.teamId === item.teamId)
            )
            if (isAllSelect) {
              // 全选中新增的差集
              const diff = val.filter(
              item => !this.currentSelection.some(teamId => teamId === item.teamId)
            )
            if (this.currentSelection.length + diff.length > this.maxLength) {
                this.currentSelection = this.currentSelection.concat(
                  diff.splice(0, this.maxLength - this.currentSelection.length)
                      .map(item => item.teamId))
                diff.forEach(item => this.$refs.multipleTable.toggleRowSelection(item, false))
            } else {
                this.currentSelection = this.currentSelection.concat(diff.map(item => item.teamId))
            }
            } else {
                this.currentSelection = this.currentSelection.filter(
                  teamId => !this.constructionList.some(item => item.teamId === teamId)
                )
              }
          } else {
            // 进来此处说明:
            // 其他页并无勾选数据，且当前页取消勾选
            this.currentSelection = []
          }
        },
    //列表-图片查看
    openImg(url) {
        this.dialogImageUrl = url
        this.dialogVisible = true
        },

    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listConstruction(this.queryParams).then(response => {
        this.constructionList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (this.currentSelection.length) {
                this.$nextTick(() => {
                  this.currentSelection.forEach(row => {
                    this.constructionList.forEach(item => {
                      if (item.teamId === row) {
                        this.$refs.multipleTable?.toggleRowSelection(item, true)
                      }
                    })
                  })
                })
              } else {
                this.$refs.multipleTable?.clearSelection()
              }
      });
    },

    //打开班组施工页面
      openTeam(row){
          this.teamId = row.teamId;
          this.title = "查看人员"
          this.showTeam = true;
          this.getListTeam();
        },

      //打开临时施工页面
      openTeamTemp(row){
          this.teamId = row.teamId;
          this.title = "查看临时人员"
          this.showTeamTemp = true;
          this.getListTeamTemp();
        },

      handleQueryTeam() {
            this.queryTeam.pageNum = 1;
            this.queryTeam.teamId = this.teamId;
            console.log(this.queryTeam);
            this.getListTeam();
        },

      handleQueryTeamTemp() {
            this.queryTeamTemp.pageNum = 1;
            this.queryTeamTemp.teamId = this.teamId;
            console.log(this.queryTeamTemp);
            this.getListTeamTemp();
        },

      /** 查询班组人员列表 */
      getListTeam() {
          this.loadingTwo = true;
          this.queryTeam.teamId = this.teamId;
          listTeam(this.queryTeam).then(response => {
          this.teamList = response.rows;
          this.totalTwo = response.total;
          this.loadingTwo = false;
          });
       },  
       
       /** 查询临时班组人员列表 */
      getListTeamTemp() {
          this.loadingThree = true;
          this.queryTeamTemp.teamId = this.teamId;
          listTeamTemp(this.queryTeamTemp).then(response => {
          this.teamTempList = response.rows;
          this.totalTwo = response.total;
          this.loadingThree = false;
          });
       },  

        /** 查询施工预警信息列表 */
      getListWarn() {
          this.loadingTwo = true;
          this.queryWarn.teamId = this.teamId;
          listWarn(this.queryWarn).then(response => {
          this.warnList = response.rows;
          this.totalTwo = response.total;
          this.loadingTwo = false;
          });
      },  

        //打开施工预警信息页面
        openWarn(row){
          this.teamId = row.teamId;
          this.title = "查询施工预警信息"
          this.showWarn = true;
          this.getListWarn();
        },

        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.teamId)
            this.single = selection.length!=1
            this.multiple = !selection.length
        },
    // 取消按钮
    // cancel() {
    //   this.open = false;
    //   this.reset();
    // },
    // 表单重置
    // reset() {
    //   this.form = {
    //     appnum: undefined,
    //     projectDepartName: undefined,
    //     projectHeadName: undefined,
    //     contactInformation: undefined,
    //     projectHeadName: undefined,
    //     remarks: undefined
    //   };
    //   this.resetForm("form");
    // },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.currentSelection = [];
      this.handleQuery();
    },

    /** 查看班组人员重置按钮操作 */
    resetQueryTeam() {
      this.resetForm("queryFormTeam");
      this.handleQueryTeam();
    },

    /** 查看临时人员重置按钮操作 */
    resetQueryTeamTemp() {
      this.resetForm("queryFormTeamTemp");
      this.handleQueryTeamTemp();
    },
 
    /** 导出按钮操作 */
    handleExport() {
      if(this.ids.length == 0){
        exportConstructionRecord(this.queryParams).then(res => {
            downloadFile({ fileName: `施工记录_${new Date().getTime()}.xlsx`, fileData: res, fileType: 'application/vnd.ms-excel;charset=utf-8' })
          })
      }else{
        const ids = this.ids.join(',')
        exportConstructionRecordSome({ids:ids}).then(res => {
            downloadFile({ fileName: `施工记录_${new Date().getTime()}.xlsx`, fileData: res, fileType: 'application/vnd.ms-excel;charset=utf-8' })
          })
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.uploadImg {
  padding-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.deviceCode {
    margin-top: 10px;
    padding-bottom: 20px;
    font-size: 18px;
}
::v-deep.el-table .fixed-width .el-button--mini {
    width: 60px !important;
    margin-bottom: 10px;
}
//隐藏图片上传框的css
::v-deep.disabled {
    .el-upload--picture-card {
        display: none;
    }
}
</style>