<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="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd hh:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="仅显示当前生效" label-width="120px">
            <el-switch
              v-model="queryParams.isEnable"
              inactive-color="#ccc">
            </el-switch>
          </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
              icon="el-icon-plus"
              size="mini"
              @click="addTemplate"
              v-hasPermi="['system:user:add']"
            >新增作息模板</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          style="width: auto;"
          border
          height="600"
          size="small"
          :data="tableList">
          <el-table-column fixed 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" prop="effectiveDate" label="生效日期" ></el-table-column>
          <el-table-column v-if="columns[2].visible" prop="workDay" label="工作日"></el-table-column>
          <el-table-column v-if="columns[3].visible" prop="modifiedName" label="更新人"></el-table-column>
          <el-table-column v-if="columns[4].visible"  prop="updateTime" label="更新时间" ></el-table-column>
          <el-table-column v-if="columns[5].visible" label="操作" width="100" >
            <template slot-scope="r">
<!--                <el-button type="text" v-if="r.row.timeCheck===0||r.row.timeCheck===3" @click="onEdit(r.row)">编辑</el-button>-->
<!--                <el-button type="text" v-if="r.row.timeCheck===0" @click="onDelete(r.row.id)">删除</el-button>-->
<!--                <el-button type="text" v-if="r.row.timeCheck===1||r.row.timeCheck===2" @click="info(r.row)">查看</el-button>-->
                <el-button type="text"  v-hasPermi="['system:user:details']" @click="lookItem(r.row)">查看</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"
        />
      </el-col>
    </el-row>


    <dialog-template ref="modalTemplate" @ok="getList" ></dialog-template>

  </div>
</template>

<script>
import OrgTree from "@/components/orgTree";
import DialogTemplate from './dialog.template'
/** 引入节点树接口*/
import { deptTreeSelect} from "@/api/system/public";
import {weekTableList} from './api'
import {formatTime} from "@/utils/ruoyi";
export default {
  /**  得先引入字典才能使用 */
  dicts: ['sys_business_type'],
  components: {OrgTree,DialogTemplate},
  data() {
    return {
      loading:false,
      //选中的行
      selectRow:null,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      tableList: null,
      // 树选项
      orgTree: null,
      // 是否显示弹出层
      open: false,
      // 通过机构名称查询tree
      deptName: null,
      //是否关联下级
      checked: false,
      //自定义机构树数据
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkSub: true,
        orgId: null,
        isEnable:false,
      },
      dateRange:[],
      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:[],
    };
  },
  computed: {
    pickerOptions() {
      return {
        start: "00:00",
        end: "24:00",
        step: "00:10",
      };
    },
  },
  created() {
    // this.getDeptTree();
    // this.getConfigKey("sys.user.initPassword").then(response => {
    //   this.initPassword = response.msg;
    // });
  },
  methods: {
    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查看 */
    lookItem(row) {
      this.selectRow = row;
      this.$refs.modalTemplate.show(row);
    },

    addTemplate(){
      this.$refs.modalTemplate.add();
    },
    /** 下穿状态改变*/
    changeCheckBox(){
      this.getList();
    },
    /** 查询列表 */
    getList() {
      weekTableList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.tableList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      ).catch(err=>{
        this.loading = false;
      })
    },
    /** 查询机构树 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.orgTree = response.data;
        this.defaultKeys.push(response.data[0].id);
        this.queryParams.orgId = response.data[0].id;
        this.handleQuery();
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.orgId = data.id;
      this.handleQuery();
    },

    /** 提交按钮 */
    submitForm() {

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/time/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /* 重置搜索 */
    resetForm(name){
      this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          orgId: null,
          isEnable:false,
      }
      this.dateRange = [];
      this.getList();
    },
  }
};
</script>
