<template>
  <div class="app-container">

    <div class="company" v-if="!$store.state.user.tenantId">
      <el-input
        style="width: 200px"
        placeholder="公司名称"
        size="small"
        prefix-icon="el-icon-search"
        v-model="gsInfo.tenantName"
        @keyup.enter.native="getGsInfoList">
      </el-input>
      <div class="cp-content" style="width: 200px">
        <div :class="gsInfo.active_id===item.id?'cp-item active':'cp-item'" @click="changCompany(item.id)" v-for="item in gsInfo.list">{{ item.name }}</div>
      </div>
    </div>

    <div :class="$store.state.user.tenantId?'content-all':'content'">

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="部门" prop="deptId">
          <treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" style="width: 220px"/>
        </el-form-item>
        <el-form-item label="员工" prop="empId">
          <el-select v-model="queryParams.empId" filterable clearable placeholder="请选择">
            <el-option
              v-for="item in employeeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日工日期" prop="date">
          <el-date-picker
            v-model="queryParams.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日工日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="月份" prop="dwYearMonth">
          <el-date-picker
            v-model="queryParams.dwYearMonth"
            type="month"
            value-format="yyyy-MM"
            placeholder="请选择月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="业务员" prop="salesman">
          <el-select v-model="queryParams.salesman" filterable placeholder="请选择">
            <el-option
              v-for="item in employeeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </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="['pay:dayWorkHours: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="['pay:dayWorkHours: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="['pay:dayWorkHours: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="['pay:dayWorkHours:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="dayWorkHoursList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="部门名称" align="center" prop="deptName" />
        <el-table-column label="员工姓名" align="center" prop="empName" />
        <el-table-column label="日工日期" align="center" prop="date" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="日工年月" align="center" prop="dwYearMonth" />
        <el-table-column label="备注" align="center" prop="note" />
        <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="['pay:dayWorkHours:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleDetails(scope.row)"
              v-hasPermi="['pay:dayWorkHours:query']"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['pay:dayWorkHours: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"
      />

    </div>

    <!-- 添加或修改日工工时对话框 -->
    <el-dialog class="my-dialog" :title="title" :visible.sync="open" width="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="日工日期" prop="date">
              <el-date-picker clearable
                              v-model="form.date"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择日工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(elctment, index) in form.empInfos" :key="elctment.key">
          <el-col :span="8">
            <el-form-item
              label="员工姓名"
              :prop="'empInfos.' + index + '.empName'"
              :rules="{required: true, message: '员工姓名不能为空', trigger: ['blur', 'change']}">
              <el-input placeholder="请选择员工姓名" readonly v-model="elctment.empName" @focus="changeEmp(index,1)">
                <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="部门名称"
              :prop="'empInfos.' + index + '.deptId'"
              :rules="{required: true, message: '员工姓名不能为空', trigger: ['blur', 'change']}">
              <treeselect v-model="elctment.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" style="width: 200px" @select="changeDept"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="my-delete">
              <el-button size="mini" @click.prevent="removeElet(elctment)">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-button size="mini" @click="addElet">新增员工</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(domain, index) in form.dwHoursContrasts" :key="domain.key">
          <el-col :span="1.5">
            <el-form-item
              label="开始时间"
              :prop="'dwHoursContrasts.' + index + '.beginTime'"
              :rules="{required: true, message: '开始时间不能为空', trigger: 'blur'}">
              <el-time-picker v-model="domain.beginTime" value-format="HH:mm" format="HH:mm" placeholder="开始时间" style="width: 145px;"></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="结束时间"
              :prop="'dwHoursContrasts.' + index + '.fnishTime'"
              :rules="{required: true, message: '结束时间不能为空', trigger: 'blur'}">
              <el-time-picker v-model="domain.fnishTime" value-format="HH:mm" format="HH:mm" placeholder="结束时间" style="width: 145px;"></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="业务员"
              :prop="'dwHoursContrasts.' + index + '.salesman'">
              <el-input placeholder="请选择业务员" readonly v-model="domain.salesman" @focus="changeEmp(index,2)">
                <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="是否帮工" :prop="'dwHoursContrasts.' + index + '.isHelper'"
              :rules="{required: true, message: '请选择是否帮工', trigger: 'change'}">
              <el-radio-group v-model="domain.isHelper" @change="getHelpers">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="部门名称" :prop="'dwHoursContrasts.' + index + '.deptId'" :rules="{required: true, message: '部门不能为空', trigger: 'change'}"  v-if="domain.isHelper === 1">
              <treeselect v-model="domain.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" @select="changeDept1($event,domain)" style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item class="my-delete">
              <el-button size="mini" @click.prevent="removeDomain(domain)">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-button size="mini" @click="addDomain">新增时间段</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </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-dialog class="my-dialog" :title="title" :visible.sync="opena" width="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="title !== '日工工时详情'">
        <el-row>
          <el-col :span="8">
            <el-form-item label="员工姓名" prop="empName">
              <el-input placeholder="请选择员工姓名" readonly v-model="form.empName" @focus="changeEmp(index,1)">
                <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部门名称" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" style="width: 200px" @select="changeDept"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日工日期" prop="date">
              <el-date-picker clearable
                              v-model="form.date"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择日工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(domain, index) in form.dwHoursContrasts" :key="domain.key">
          <el-col :span="1.5">
            <el-form-item
              label="开始时间"
              :prop="'dwHoursContrasts.' + index + '.beginTime'"
              :rules="{required: true, message: '开始时间不能为空', trigger: 'blur'}">
              <el-time-picker v-model="domain.beginTime" value-format="HH:mm" format="HH:mm" placeholder="开始时间" style="width: 145px;"></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="结束时间"
              :prop="'dwHoursContrasts.' + index + '.fnishTime'"
              :rules="{required: true, message: '结束时间不能为空', trigger: 'blur'}">
              <el-time-picker v-model="domain.fnishTime" value-format="HH:mm" format="HH:mm" placeholder="结束时间" style="width: 145px;"></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="业务员"
              :prop="'dwHoursContrasts.' + index + '.salesman'">
              <el-input placeholder="请选择业务员" readonly v-model="domain.salesman" @focus="changeEmp(index,2)">
                <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="是否帮工" :prop="'dwHoursContrasts.' + index + '.isHelper'"
              :rules="{required: true, message: '请选择是否帮工', trigger: 'change'}">
              <el-radio-group v-model="domain.isHelper" @change="getHelpers">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="部门名称" :prop="'dwHoursContrasts.' + index + '.deptId'"
                          :rules="{required: true, message: '部门不能为空', trigger: 'change'}"
                          v-if="domain.isHelper === 1">
              <treeselect v-model="domain.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" @select="changeDept1($event,domain)" style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item class="my-delete">
              <el-button size="mini" @click.prevent="removeDomain(domain)">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-button size="mini" @click="addDomain">新增时间段</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="title === '日工工时详情'">
        <el-row>
          <el-col :span="8">
            <el-form-item label="员工姓名" prop="empName">
              <el-select v-model="form.empName" filterable clearable placeholder="请选择" disabled>
                <el-option
                  v-for="item in form.employeeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部门名称" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" style="width: 200px" @select="changeDept" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日工日期" prop="date">
              <el-date-picker clearable
                              v-model="form.date"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择日工日期" disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-for="(domain, index) in form.dwHoursContrasts" :key="domain.key">
          <el-col :span="1.5">
            <el-form-item
              label="开始时间"
              :prop="'dwHoursContrasts.' + index + '.beginTime'"
              :rules="{required: true, message: '开始时间不能为空', trigger: 'blur'}">
              <el-time-picker v-model="domain.beginTime" value-format="HH:mm" format="HH:mm" placeholder="开始时间" disabled style="width: 145px;"></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="结束时间"
              :prop="'dwHoursContrasts.' + index + '.fnishTime'"
              :rules="{required: true, message: '结束时间不能为空', trigger: 'blur'}">
              <el-time-picker v-model="domain.fnishTime" value-format="HH:mm" format="HH:mm" placeholder="结束时间" disabled style="width: 145px;"></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="业务员"
              :prop="'dwHoursContrasts.' + index + '.salesman'">
              <el-input placeholder="请选择业务员" readonly v-model="domain.salesman" @focus="changeEmp(index,2)">
                <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="是否帮工" :prop="'dwHoursContrasts.' + index + '.isHelper'"
              :rules="{required: true, message: '请选择是否帮工', trigger: 'change'}">
              <el-radio-group v-model="domain.isHelper" @change="getHelpers" disabled>
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item label="部门名称" :prop="'dwHoursContrasts.' + index + '.deptId'" :rules="{required: true, message: '部门不能为空', trigger: 'change'}" v-if="domain.isHelper === 1">
              <treeselect v-model="domain.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" disabled @select="changeDept1($event,domain)" style="width: 200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="title !== '日工工时详情'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择员工信息 -->
    <el-dialog title="双击选择员工" :visible.sync="emp.open" width="900px" append-to-body top="6vh">
      <div>
        <el-form :model="emp.queryParams" :inline="true" label-width="70px">
          <el-form-item label="员工名称" prop="name" style="margin-bottom: 10px">
            <el-input
              v-model="emp.queryParams.name"
              placeholder="请输入员工名称"
              clearablesize="small"
              @keyup.enter.native="empQuery"
            />
          </el-form-item>
          <el-form-item style="margin-bottom: 10px">
            <el-button type="primary" icon="el-icon-search" size="small" @click="empQuery">搜索</el-button>
          </el-form-item>
        </el-form>

        <el-table border v-loading="emp.loading" :data="emp.list" @row-dblclick="empRowChange">
          <el-table-column label="员工姓名" align="center" prop="name" :show-overflow-tooltip="true" min-width="120"/>
          <el-table-column label="所属部门" align="center" prop="deptName" :show-overflow-tooltip="true" min-width="120"/>
          <el-table-column label="状态" align="center" prop="deleted">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pay_del_status" :value="scope.row.deleted"/>
            </template>
          </el-table-column>
          <el-table-column label="性别" align="center" prop="sex">
            <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="age" />
          <el-table-column label="手机号" align="center" prop="phone" min-width="100"/>
          <el-table-column label="身份证号" align="center" prop="idcard" min-width="100" />
        </el-table>

        <pagination
          v-show="emp.total>0"
          :total="emp.total"
          :page.sync="emp.queryParams.pageNum"
          :limit.sync="emp.queryParams.pageSize"
          @pagination="empQuery"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="empCancel">取 消</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listDayWorkHours, getDayWorkHours, delDayWorkHours, addDayWorkHours, updateDayWorkHours } from "@/api/pay/dayWorkHours";
import { listTenant } from "@/api/pay/tenant";
import { listEmployeeInfo } from "@/api/pay/employeeInfo";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "DayWorkHours",
  dicts: ['pay_workshop', 'sys_user_sex', 'pay_del_status'],
  components: { Treeselect },
  data() {
    return {
      // 公司信息
      gsInfo:{
        tenantName:'',
        active_id:this.$store.state.user.tenantId,
        list:[]
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日工工时表格数据
      dayWorkHoursList: [],
      // 部门树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opena: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: undefined,
        deptId: undefined,
        deptName: undefined,
        employeeId: undefined,
        empName: undefined,
        date: undefined,
        dwYearMonth: undefined,
        note: undefined
      },
      // 表单参数
      form: {},
      // 选择员工
      employeeList: [],
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        tenantId: [
          { required: true, message: "租户id不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "员工部门id不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "员工部门名称不能为空", trigger: "blur" }
        ],
        employeeId: [
          { required: true, message: "员工id不能为空", trigger: "blur" }
        ],
        empName: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日工日期不能为空", trigger: "blur" }
        ],
        dwYearMonth: [
          { required: true, message: "日工年月不能为空", trigger: "blur" }
        ],
        note: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      },
      /** 选择员工*/
      emp: {
        // 1 员工选择  2业务员选择
        type: null,
        // 是否显示弹出层
        open: false,
        // 列表遮罩层
        loading: true,
        // 列表查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined
        },
        // 表格数据
        list: [],
        // 总条数
        total: 0,
        index: 0
      },
    };
  },
  created() {
    if (!this.$store.state.user.tenantId) {
      this.getGsInfoList()
    }else {
      this.gsInfo.active_id = this.$store.state.user.tenantId
      this.getList();
      this.getDeptTree();
      this.getlistEmployee();
    }
  },
  methods: {
    // 获取左侧公司列表
    getGsInfoList(){
      listTenant({ name: this.gsInfo.tenantName }).then(response => {
        if(response.rows.length>0){
          this.gsInfo.list = response.rows;
          this.gsInfo.active_id = this.gsInfo.list[0].id
          this.getList();
          this.getDeptTree();
          this.getlistEmployee();
        }else{
          this.$alert(`没有查到相关公司，暂无数据！`, `提示`, {
            type: 'warning'
          });
        }
      });
    },
    // 选择公司
    changCompany(id){
      this.gsInfo.active_id = id
      this.getList()
    },
    /** 查询时员工 */
    getlistEmployee() {
      listEmployeeInfo({ pageSize: 1000000000, tenantId: this.gsInfo.active_id }).then(response => {
        this.employeeList = response.rows
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect({tenantId: this.gsInfo.active_id}).then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询日工工时列表 */
    getList() {
      this.loading = true;
      // this.queryParams.tenantId = this.gsInfo.active_id;
      listDayWorkHours(this.queryParams).then(response => {
        this.dayWorkHoursList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.employeeId = null
      this.queryParams.deptId = null
      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 = "添加日工工时";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除日工工时编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delDayWorkHours(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pay/dayWorkHours/export', {
        ...this.queryParams
      }, `dayWorkHours_${new Date().getTime()}.xlsx`)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDayWorkHours(id).then(response => {
        this.form = response.data;
        this.form.dwHoursContrasts.forEach(item => {
          // this.$set(item,'departOptions',response.data)
          var sth = new Date(item.startTime).getHours();
          sth = sth < 10 ?  sth : sth;
          var stm = new Date(item.startTime).getMinutes();
          stm = stm < 10 ? '0' + stm : stm;
          var eth = new Date(item.endTime).getHours();
          eth = eth < 10 ?  eth : eth;
          var etm = new Date(item.endTime).getMinutes();
          etm = etm < 10 ? '0' + etm : etm;
          this.$set(item,"beginTime",sth+":"+stm);
          this.$set(item,"fnishTime",eth+":"+etm);
        })
        this.opena = true;
        this.title = "修改日工工时";
      });
    },
    /** 查询时选择部门 */
    changeDept(dept) {
      this.form.deptName = dept.label
    },
    /** 查询时选择部门 */
    changeDept1(dept,item) {
      item.deptName = dept.label
    },
    /** 查询时选择员工 */
    getEmploy(val) {
      this.queryParams.employeeList.forEach(employ => {
        if (employ.id == val) {
          this.queryParams.employeeId = employ.id
          this.queryParams.empName = employ.name
        }
      })
    },
    getHelpers() {
      this.form.dwHoursContrasts.forEach(item => {
        if(0 == item.isHelper){
          item.deptId = null
          item.deptName = null
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opena = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        deptName: null,
        employeeId: null,
        empName: null,
        employeeList: [],
        date: null,
        empInfos: [{
          deptId: null,
          deptName: null,
          employeeId: null,
          empName: null
        }],
        dwHoursContrasts: [{
          startTime: null,
          beginTime: null,
          endTime: null,
          fnishTime: null,
          isHelper: 0,
          departOptions: [],
          deptId: null,
          deptName: null,
          salesman: null
        }]
      };
      this.resetForm("form");
    },
    // 新增员工
    addElet() {
      this.form.empInfos.push({
        deptId: null,
        deptName: null,
        employeeId: null,
        empName: null
      });
    },
    // 删除员工
    removeElet(elp) {
      var index = this.form.empInfos.indexOf(elp)
      if (index !== -1) {
        this.form.empInfos.splice(index, 1)
      }
    },
    /** 新增时间段 */
    addDomain() {
      this.form.dwHoursContrasts.push({
        startTime: null,
        beginTime: null,
        endTime: null,
        fnishTime: null,
        isHelper: 0,
        departOptions: [],
        deptId: null,
        deptName: null,
        salesman: null
      });
    },
    /** 删除时间段 */
    removeDomain(item) {
      var index = this.form.dwHoursContrasts.indexOf(item)
      if (index !== -1) {
        this.form.dwHoursContrasts.splice(index, 1)
      }
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      this.reset();
      const id = row.id || this.ids
      getDayWorkHours(id).then(response => {
        this.form = response.data;
        this.form.dwHoursContrasts.forEach(item => {
          var sth = new Date(item.startTime).getHours();
          sth = sth < 10 ?  sth : sth;
          var stm = new Date(item.startTime).getMinutes();
          stm = stm < 10 ? '0' + stm : stm;
          var eth = new Date(item.endTime).getHours();
          eth = eth < 10 ?  eth : eth;
          var etm = new Date(item.endTime).getMinutes();
          etm = etm < 10 ? '0' + etm : etm;
          this.$set(item,"beginTime",sth+":"+stm);
          this.$set(item,"fnishTime",eth+":"+etm);
        })
        this.opena = true;
        this.title = "日工工时详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tenantId = this.gsInfo.active_id;
          this.form.dwHoursContrasts.forEach(a => {
            a.startTime = this.form.date + ' ' + a.beginTime + ':00';
            a.endTime = this.form.date + ' ' + a.fnishTime + ':00';
          })
          var isHour = true
          if(this.form.dwHoursContrasts.length > 0){
            for(var i=0; i<this.form.dwHoursContrasts.length; i++){
              if(new Date(this.form.dwHoursContrasts[i].endTime).getTime() <= new Date(this.form.dwHoursContrasts[i].startTime).getTime()){
                isHour = false
                this.$alert('结束时间应大于开始时间', '信息', {
                  confirmButtonText: '确定',
                  customClass:'message_box_alert'
                }).catch((err) => {
                  console.log(err)
                })
                break;
              }
            }
          }
          if (isHour) {
            if (this.form.id != null) {
              updateDayWorkHours(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.opena = false;
                this.getList();
              });
            } else {
              addDayWorkHours(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    // 添加包活派工单 选择员工
    changeEmp(index,type){
      this.emp.open = true;
      this.emp.index = index;
      this.emp.type = type;
      this.emp.queryParams.pageNum = 1;
      this.empQuery();
    },
    /** 员工列表查询 */
    empQuery() {
      this.emp.loading = true;
      this.emp.queryParams.tenantId = this.gsInfo.active_id
      listEmployeeInfo(this.emp.queryParams).then(response => {
        this.emp.list = response.rows;
        this.emp.total = response.total;
        this.emp.loading = false;
      });
    },
    /** 员工选择弹出框关闭*/
    empCancel() {
      this.emp.open = false
    },
    // 员工选择 选中数据
    empRowChange(val) {
      console.log(this.emp.type,val);
      if( this.emp.type===1 ){
        this.form.empInfos[this.emp.index].empName = val.name;
        this.form.empInfos[this.emp.index].employeeId = val.id;
        this.form.empInfos[this.emp.index].deptId = val.deptId;
        this.form.empInfos[this.emp.index].deptName = val.deptName;
      }
      if( this.emp.type===2 ){
        this.form.dwHoursContrasts[this.emp.index].salesman = val.name;
      }
      this.emp.open = false;
    },
  }
};
</script>

<style scoped lang="scss">
.app-container {
  display: flex;
  .company {
    border-radius: 5px;
    width: 230px;
    height: calc(100vh - 115px);
    overflow-y: auto;
    .cp-content {
      margin-top: 20px;
      .cp-item {
        cursor: pointer;
        padding: 6px 10px 6px 5px;
        color: #424242;
        font-size: 14px;
        &:hover {
          background: #f6f6f6;
        }
      }
      .active{
        background: #edf6ff;
      }
    }
  }
  .content{
    padding-left: 15px;
    width: calc(100% - 250px);
    .el-button--medium {
      padding: 2px 0px;
    }
  }
  .content-all{
    width: 100%;
    .el-button--medium {
      padding: 2px 0px;
    }
  }
}
</style>
