<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="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入姓名"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="部门" prop="orgId">
            <treeselect v-model="queryParams.orgId" :options="deptOptions" :normalizer="normalizer" placeholder="选择部门" style="width: 240px"/>
        </el-form-item>
        <el-form-item label="状态" prop="attStatus">
          <el-select
            v-model="queryParams.attStatus"
            placeholder="状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.att_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </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-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
          <el-button type="primary" size="mini" @click="openDialog">批量修改</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <!-- <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-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <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="userName" />
        <el-table-column label="所属部门" align="center" prop="orgName" :show-overflow-tooltip="true" />
        <el-table-column label="考勤日期" align="center"  prop="attCurrentDay" width="180" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ formatDate(scope.row.attCurrentDay) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="上班打卡时间" align="center" prop="toWorkAttCurrentTime" width="180"></el-table-column>
        <el-table-column label="上班状态" align="center" prop="toWorkAttStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.toWorkAttStatus==0">未打卡</div>
            <div v-if="scope.row.toWorkAttStatus==1">正常</div>
            <div style="color: #F1A4AD" v-if="scope.row.toWorkAttStatus==2">迟到</div>
            <div style="color: #29C9C9" v-if="scope.row.toWorkAttStatus==3">旷工</div>
            <div v-if="scope.row.toWorkAttStatus==4">早退</div>
            <div v-if="scope.row.toWorkAttStatus==5">轮休</div>
            <div style="color: #14ACF0" v-if="scope.row.toWorkAttStatus==6">请假</div>
            <div v-if="scope.row.toWorkAttStatus==7">临时外出</div>
            <div style="color: #DD1E36" v-if="scope.row.toWorkAttStatus==8">出入异常</div>
            <div v-if="scope.row.toWorkAttStatus==9">打卡地异常</div>
            <div v-if="scope.row.toWorkAttStatus==10">出差</div>
            <div v-if="scope.row.toWorkAttStatus==11">法定节假日</div>
          </template>
        </el-table-column>
        <el-table-column label="打卡地址" align="center" prop="toWorkAttAddress" :show-overflow-tooltip="true" />
        <el-table-column label="异常备注" align="center" prop="toWorkErrorRemake" :show-overflow-tooltip="true" />
        <el-table-column label="下班打卡时间" align="center" prop="offWorkAttCurrentTime" width="180"></el-table-column>
        <el-table-column label="下班状态" align="center" prop="offWorkAttStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.offWorkAttStatus==0">未打卡</div>
            <div v-if="scope.row.offWorkAttStatus==1">正常</div>
            <div style="color: #F1A4AD" v-if="scope.row.offWorkAttStatus==2">迟到</div>
            <div style="color: #29C9C9" v-if="scope.row.offWorkAttStatus==3">旷工</div>
            <div v-if="scope.row.offWorkAttStatus==4">早退</div>
            <div v-if="scope.row.offWorkAttStatus==5">轮休</div>
            <div style="color: #14ACF0" v-if="scope.row.offWorkAttStatus==6">请假</div>
            <div v-if="scope.row.offWorkAttStatus==7">临时外出</div>
            <div style="color: #DD1E36" v-if="scope.row.offWorkAttStatus==8">出入异常</div>
            <div v-if="scope.row.offWorkAttStatus==9">打卡地异常</div>
            <div v-if="scope.row.offWorkAttStatus==10">出差</div>
            <div v-if="scope.row.offWorkAttStatus==11">法定节假日</div>
          </template>
        </el-table-column>
        <el-table-column label="打卡地址" align="center" prop="offWorkAttAddress" :show-overflow-tooltip="true" />
        <el-table-column label="异常备注" align="center" prop="offWorkErrorRemake" :show-overflow-tooltip="true" />
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    <el-dialog title="批量修改" :visible.sync="open" width="1200px" append-to-body> 
        <el-table v-loading="loading" :data="dialogList" width="100%" height="500px">
            <el-table-column label="序号" type="index" width="55" align="center" fixed/>
            <el-table-column label="姓名" align="center" prop="userName" fixed/>
            <el-table-column label="所属部门" align="center" prop="orgName" :show-overflow-tooltip="true" width="150"  fixed/>
            <el-table-column label="考勤日期" align="center"  prop="attCurrentDay" :show-overflow-tooltip="true" width="180" fixed>
              <template slot-scope="scope">
                <span>{{ formatDate(scope.row.attCurrentDay) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="修改上班时间" align="center" prop="toWorkAttCurrentTime" width="240">
              <template slot-scope="scope">
                  <el-date-picker 
                      v-model="scope.row.toWorkAttCurrentTime" :clearable="false"
                      type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width: 95%"
                      placeholder="选择上班时间">
                  </el-date-picker>
              </template>
            </el-table-column>
            <el-table-column label="修改上班状态" align="center" prop="toWorkAttStatus" width="180">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.toWorkAttStatus"
                  placeholder="修改后上班状态" style="width: 95%">
                  <el-option
                    v-for="dict in dict.type.att_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="打卡地址" align="center" prop="toWorkAttAddress" :show-overflow-tooltip="true" width="180"/>
            <el-table-column label="原因说明" align="center" prop="toErrorRemake" width="200">
              <template slot-scope="scope">
                <el-input type="textarea" v-model="scope.row.toErrorRemake" maxlength="100"></el-input>
              </template>
            </el-table-column>

            <el-table-column label="修改下班时间" align="center" prop="offWorkAttCurrentTime" width="240">
              <template slot-scope="scope">
                  <el-date-picker 
                      v-model="scope.row.offWorkAttCurrentTime" :clearable="false"
                      type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width: 95%"
                      placeholder="选择下班时间">
                  </el-date-picker>
              </template>
            </el-table-column>
            <el-table-column label="修改下班状态" align="center" prop="offWorkAttStatus" width="180">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.offWorkAttStatus"
                  placeholder="修改后下班状态" style="width: 95%">
                  <el-option
                    v-for="dict in dict.type.att_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="打卡地址" align="center" prop="offWorkAttAddress" :show-overflow-tooltip="true" width="180"></el-table-column>
            <el-table-column label="原因说明" align="center" prop="offErrorRemake" width="200">
              <template slot-scope="scope">
                <el-input type="textarea" v-model="scope.row.offErrorRemake" maxlength="100"></el-input>
              </template>
            </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitEdit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  
    
    </div>
  </template>
  
  <script>
  import { getDetailsList,updateAttDetails } from "@/api/report/attReport";
  import { listDept } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";   
  export default {
    name: "Dict",
    dicts: ['att_status'],
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 字典表格数据
        typeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        dialogList:[],
        // 日期范围
        dateRange: [],
        deptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          orgId: undefined,
          attStatus: undefined
        },
      };
    },
    created() {
      this.getWeekDates();
      this.getDeptList();
      this.getList();
    },
    methods: {
        getWeekDates() {
          const now = new Date();
          const dayOfWeek = now.getDay();
          const dayOffset = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
      
          const monday = new Date(now);
          monday.setDate(monday.getDate() + dayOffset);
      
          const sunday = new Date(monday);
          sunday.setDate(sunday.getDate() + 6);
          this.dateRange[0]=monday.toISOString().split('T')[0];
          this.dateRange[1]=sunday.toISOString().split('T')[0];
        },
        formatDate(dateString){
          const date = new Date(dateString); // 创建日期对象
          const year = date.getFullYear(); // 获取年份
          const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份（注意：月份从0开始）
          const day = String(date.getDate()).padStart(2, '0'); // 获取日期
          const weekdays = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; // 星期几数组
          const weekday = weekdays[date.getDay()]; // 获取星期几

          return `${year}-${month}-${day} ${weekday}`; // 组合成所需格式
        },
        getDeptList(){
            listDept().then(response => {
                this.deptOptions = this.handleTree(response.data, "id");
            });
        },
        /** 转换部门数据结构 */
        normalizer(node) {
            if (node.children && !node.children.length) {
                delete node.children;
            }
            return {
                id: node.id,
                label: node.orgName,
                children: node.children
            };
        },
         /** 查询字典类型列表 */
        getList() {
            this.loading = true;
            console.log(this.dateRange)
            if(this.dateRange&&this.dateRange.length>0){
                this.queryParams.startDate=this.dateRange[0]
                this.queryParams.endDate=this.dateRange[1]
            }else{
                this.queryParams.startDate=undefined
                this.queryParams.endDate=undefined
            }
            getDetailsList(this.queryParams).then(response => {
                this.typeList = response.rows;
                this.total = response.total;
                this.loading = false;
            }
            );
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.getWeekDates();
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
          this.dialogList = selection;
        },
        openDialog(){
          console.log(this.dialogList)
          if(this.dialogList.length>0){
            this.open=true;
          }else{
            this.$message({ message: '请先勾选数据！', type: 'warning' })
          }
        },
        submitEdit(){ 
          let paramList = []
          var index = this.dialogList.findIndex(item => {return !item.toErrorRemake||item.toErrorRemake==""||!item.offErrorRemake||item.offErrorRemake==""})
          console.log(index)
          this.$modal.msgSuccess("修改申请提交成功,耐心等待审批，请勿重复提交！");
          if(index>-1){
            // this.$modal.msgSuccess("原因说明不可为空！");
            this.$message({ message: '原因说明不可为空！', type: 'warning' })
          }else{
            this.dialogList.forEach(item=>{
              let obj = {
                userId:item.userId,
                orgId:item.orgId,
                attCurrentDay:item.attCurrentDay,
                toWorkAttCurrentTime:item.toWorkAttCurrentTime,
                toWorkAttStatus:item.toWorkAttStatus,
                toErrorRemake:item.toErrorRemake,
                offWorkAttCurrentTime:item.offWorkAttCurrentTime,
                offWorkAttStatus:item.offWorkAttStatus,
                offErrorRemake:item.offErrorRemake,
              }
              paramList.push(obj)
            })  
            // console.log(paramList)
            updateAttDetails(paramList).then(response => {
              if(response.code==200){
                this.$modal.msgSuccess("修改申请提交成功,耐心等待审批，请勿重复提交！");
                this.dialogList=[]
                this.open = false;
                this.getList();
              }else{
                this.$message({ message: '修改申请提交失败！', type: 'warning' })
              }
            });
          }
        },
        cancel(){
            this.open=false;
        },
        
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/dict/type/export', {
            ...this.queryParams
            }, `type_${new Date().getTime()}.xlsx`)
        },
 
    }
  };
  </script>