<template>
  <div class="tabs-container">
    <el-row :gutter="20">
      <!--机构数据-->
      <el-col :span="4" :xs="24">
        <org-tree
          v-model="queryParams.orgId"
          @defaultKey="getDefaultKey"
          @checkChange="changeCheckBox"
          @click="handleNodeClick"
        ></org-tree>
      </el-col>
      <!--table数据-->
      <el-col :span="20" :xs="24">
        <!--    搜索条件    -->
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="起止日期">
            <el-date-picker
              v-model="queryParams.range"
              style="width: 240px"
              value-format="yyyy-MM-dd hh:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="营业状态" prop="status">
            <el-select
              v-model="queryParams.isEnable"
              placeholder="请选择"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_business_type"
                :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="resetForm"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <!--    按纽    -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              size="mini"
              icon="el-icon-thumb"
              @click="clickAdd"
              v-hasPermi="['system:time:add']"
              >新增作息配置</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <!-- <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['system:time:export']">导出</el-button> -->
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            size="mini"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </el-row>

        <el-table
          height="600"
          size="small"
          v-loading="loading"
          style="width: auto"
          border
          :data="tableList"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="50"
          ></el-table-column>
          <el-table-column
            v-if="columns[0].visible"
            prop="orgName"
            label="机构"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column v-if="columns[1].visible" label="日期">
            <span slot-scope="scope">{{ scope.row.ymdDate }}</span>
          </el-table-column>
          <el-table-column v-if="columns[2].visible" label="营业状态">
            <template slot-scope="r">{{
              r.row.isEnable == "1" ? "营业" : "歇业"
            }}</template>
          </el-table-column>
          <!--          <el-table-column v-if="columns[3].visible" label="上下班时间段">-->
          <!--            <template slot-scope="r">-->
          <!--              <span>{{r.row.workTime}}-{{r.row.workOffTime}}</span>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column v-if="columns[4].visible" label="营业时间段">
            <template slot-scope="r">
              <span>{{ r.row.openTime }}-{{ r.row.closeTime }}</span>
            </template>
          </el-table-column>
          <!--          <el-table-column v-if="columns[5].visible" label="午休时间段">-->
          <!--            <template slot-scope="scope">-->
          <!--              <span>{{scope.row.noonbreakStart}}-{{scope.row.noonbreakEnd}}</span>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column
            v-if="columns[6].visible"
            prop="modifiedName"
            label="更新人"
          ></el-table-column>
          <el-table-column v-if="columns[7].visible" label="更新时间">
            <span slot-scope="scope">{{ scope.row.updateTime }}</span>
          </el-table-column>
          <el-table-column
            v-if="columns[8].visible"
            label="操作列表"
            width="100"
          >
            <template slot-scope="r">
              <el-button
                type="text"
                v-if="hideEdit(r.row)"
                v-hasPermi="['system:time:edit']"
                @click="onEditTime(r.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          class="pages-box"
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!--  编辑作息  -->
    <dialog-edit ref="modalEdit" @ok="getList"></dialog-edit>
    <!--  指定日期配置  -->
    <dialog-add ref="modalAdd" @ok="getList"></dialog-add>
  </div>
</template>

<script>
import dayjs from "dayjs";
import OrgTree from "@/components/orgTree";
import DialogEdit from "./dialog.edit";
import DialogAdd from "./dialog.add";

/** 引入节点树接口*/
import { deptTreeSelect } from "@/api/system/public";
import { tableList } from "./api";
import { formatTime } from "@/utils/ruoyi";
//初始化查询日期
let timea = new Date();
timea.setHours(0, 0, 0, 0);
let timeb = new Date(timea.getTime() - 3600 * 1000 * 24 * 30);
timeb.setHours(0, 0, 0, 0);
export default {
  /**  得先引入字典才能使用 */
  dicts: ["sys_business_type"],
  /** 引入基础minxins*/
  components: { OrgTree, DialogEdit, DialogAdd },
  data() {
    return {
      nowTime: null,
      // 遮罩层
      loading: false,
      //选中的行
      selectRow: null,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      tableList: null,
      // 机构树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 通过机构名称查询tree
      deptName: undefined,
      //是否关联下级
      checked: false,
      //自定义机构树数据
      defaultProps: {
        children: "children",
        label: "name",
      },
      // 查询参数
      queryParams: {
        checkSub: true,
        orgId: null,
        isEnable: "1",
        pageNum: 1,
        pageSize: 10,
        range: [formatTime(timeb), formatTime(timea)],
      },
      total: 0,
      // 列信息
      columns: [
        { key: 0, label: `机构`, visible: true },
        { key: 1, label: `日期`, visible: true },
        { key: 2, label: `营业状态`, visible: true },
        { key: 3, label: `上下班时间段`, visible: true },
        { key: 4, label: `营业时间段`, visible: true },
        { key: 5, label: `午休时间段`, visible: true },
        { key: 6, label: `更新人`, visible: true },
        { key: 7, label: `更新时间`, visible: true },
        { key: 8, label: `操作`, visible: true },
      ],
      //默认选中节点
      defaultKeys: [],
      pickerOptions: {
        start: "00:00",
        end: "24:00",
        step: "00:10",
      },
    };
  },
  created() {
    this.getNowTime();
  },
  methods: {
    getNowTime() {
      const dateObj = new Date(); // 获取当前时间对象
      const year = dateObj.getFullYear(); // 获取年份
      const month = String(dateObj.getMonth() + 1).padStart(2, "0"); // 获取月份，并补齐两位数
      const day = String(dateObj.getDate()).padStart(2, "0"); // 获取日期，并补齐两位数
      this.nowTime = `${year}-${month}-${day}`;
    },
    hideEdit(row) {
      const startDate = new Date(row.ymdDate);
      const endDate = new Date(this.nowTime);
      return startDate >= endDate;
    },
    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    /** 下穿状态改变*/
    changeCheckBox() {
      this.queryParams.checkSub = !this.queryParams.checkSub;
      this.getList();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams, "pages");
      tableList(this.queryParams)
        .then((response) => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    /** 查询机构树 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
        this.defaultKeys.push(response.data[0].id);
        this.queryParams.orgId = response.data[0].id;
        //this.$refs.tree.setCurrentKey(response.data[0].id);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.orgId = data.id;
      this.handleQuery();
    },
    //添加指定日期作息
    clickAdd() {
      this.$refs.modalAdd.add();
    },
    /** 编辑按钮操作 */
    onEditTime(row) {
      this.selectRow = row;
      this.$refs.modalEdit.edit(row);
    },

    /** 提交按钮 */
    submitForm: function () {},
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/time/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
    /* 重置搜索 */
    resetForm(name) {
      this.queryParams.isEnable = "1";
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.range = [formatTime(timeb), formatTime(timea)];
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
};
</script>
<style lang="scss"></style>
