<template>
  <div class="tabs-container">
    <el-row :gutter="10">
      <!--机构数据-->
      <!-- <el-col :span="showOrgTree?4:0" :xs="24">
        <org-tree
          v-model="queryParams.orgId"
          @defaultKey="getDefaultKey"
          @checkChange="changeCheckBox"
          @click="handleNodeClick"
        ></org-tree>
      </el-col> -->
      <!--table数据-->
      <el-col :span="24" :xs="24">
        <div class="main-right-box">
          <!--    搜索条件    -->
          <div class="main-search-box">
            <el-form
              :model="queryParams"
              ref="queryForm"
              size="small"
              :inline="true"
              v-show="showSearch"
            >
              <el-form-item label="机构名称">
                <org-tree
                  v-model="queryParams.orgId"
                  @defaultKey="getDefaultKey"
                  @checkChange="changeCheckBox"
                  @click="handleNodeClick"
                  ref="orgTree"
                ></org-tree>
              </el-form-item>
              <el-form-item label="机构类型" prop="orgType">
                <el-select v-model="queryParams.orgType" placeholder="请选择机构类型">
                  <el-option
                    v-for="dict in dict.type.sys_org_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="`${dict.value}`">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="日期范围">
                <DataRangePicker
                  style="width: 240px"
                  v-model="queryParams.range"
                  key="daterange"
                  type="daterange"
                  :clearable="false"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </el-form-item>
              <el-form-item label="营业状态" prop="status">
                <el-select
                  v-model="queryParams.isEnable"
                  placeholder="请选择"
                  clearable
                >
                  <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>
            <!--    按纽    -->
            <el-row :gutter="10">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                  >搜索</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetForm"
                  >重置</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  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"
              ></right-toolbar>
            </el-row>
          </div>
          <el-table
            height="597"
            size="small"
            v-loading="loading"
            border
            :data="tableList"
          >
            <el-table-column
              label="序号"
              type="index"
              align="center"
              width="70"
            ></el-table-column>
            <el-table-column
              v-if="columns[0].visible"
              prop="orgName"
              align="center"
              label="机构"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column align="center" label="日期">
              <span slot-scope="scope">{{ scope.row.ymdDate }}</span>
            </el-table-column>
            <el-table-column align="center" label="营业状态">
              <template slot-scope="r">{{
                r.row.isEnable == "1" ? "营业" : "歇业"
              }}</template>
            </el-table-column>
            <el-table-column align="center" label="值班打卡">
              <template slot-scope="r">{{
                r.row.isDuty == "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 align="center" 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
              align="center"
              prop="modifiedName"
              label="更新人"
            ></el-table-column>
            <el-table-column align="center" label="更新时间" width="180">
              <span slot-scope="scope">{{ scope.row.updateTime }}</span>
            </el-table-column>
            <el-table-column
              label="操作"
              header-align="center"
              align="left"
              width="160"
            >
              <template slot-scope="r">
                <el-button
                  size="mini"
                  icon="el-icon-document"
                  type="text"
                  @click="showDetail(r.row)"
                >查看</el-button>
                <el-button
                  size="mini"
                  icon="el-icon-edit-outline"
                  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"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>
    <!--  查看详情  -->
    <dialog-detail ref="modalDetail" @ok="getList"></dialog-detail>
    <!--  编辑作息  -->
    <dialog-edit ref="modalEdit" @ok="getList"></dialog-edit>
    <!--  指定日期配置  -->
    <dialog-add ref="modalAdd" @ok="getList"></dialog-add>
  </div>
</template>

<script>
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";
import DialogEdit from "./dialog.edit";
import DialogAdd from "./dialog.add";
import DialogDetail from "./dialog.detail.vue"
import { mapGetters } from "vuex";
/** 引入节点树接口*/
import { deptTreeSelect } from "@/api/system/public";
import { tableList } from "./api";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
//初始化查询日期
let sDate = new Date();
let eDate = new Date();
export default {
  /**  得先引入字典才能使用 */
  dicts: ["sys_business_type",'sys_org_type'],
  /** 引入基础minxins*/
  components: { DataRangePicker, OrgTree, DialogEdit, DialogAdd,DialogDetail },
  data() {
    return {
      nowTime: null,
      // 遮罩层
      loading: false,
      //选中的行
      selectRow: null,
      //显示右边机构树
      showOrgTree: true,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      tableList: null,
      // 机构树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 通过机构名称查询tree
      deptName: undefined,
      //是否关联下级
      checked: false,
      //自定义机构树数据
      defaultProps: {
        children: "children",
        label: "name",
      },
      // 查询参数
      queryParams: {
        checkSub: true,
        orgId: null,
        isEnable: null,
        pageNum: 1,
        pageSize: 10,
        orgType:null,
        range: [sDate, eDate],
      },
      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",
      },
    };
  },
  computed: {
    ...mapGetters(["orgId"]),
  },
  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();
    },
    showDetail(row){
      this.selectRow = row;
      this.$refs.modalDetail.show(row);
    },
    /** 编辑按钮操作 */
    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 = null;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.orgId = this.orgId;
      this.queryParams.orgType = null;
      this.queryParams.checkSub = true;
      this.$refs["orgTree"].setCheckSub(this.queryParams.checkSub)
      this.queryParams.range = [sDate, eDate];
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
};
</script>
<style lang="scss"></style>
