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

    <el-row :gutter="20">
      <!--机构数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入机构名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="tree-container">
          <div style="margin-bottom: 10px;">
            <el-checkbox v-model="queryParams.checkSub" @change="changeCheckBox">是否关联下级机构</el-checkbox>
          </div>
          <el-tree
            :data="orgTree"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            :default-expanded-keys="defaultKeys"
            :default-checked-keys="defaultKeys"
            @node-click="handleNodeClick"
          />
        </div>
      </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.effectiveDate"
              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="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="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;" size="nimi" border :data="tableList">
          <el-table-column v-if="columns[0].visible" prop="orgName" label="机构"></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="pages.total >0"
          :total="pages.total"
          :page.sync="pages.page"
          :limit.sync="pages.size"
          @pagination="getList"
        />
      </el-col>
    </el-row>


    <dialog-template ref="modalTemplate" ></dialog-template>

  </div>
</template>

<script>
import tableListMixins  from '@/mixins/tableList'
import DialogTemplate from './dialog.template'
import {json} from'./json'
/** 引入节点树接口*/
import { deptTreeSelect} from "@/api/system/public";
import {weekTableList} from './api'

export default {
  /**  得先引入字典才能使用 */
  dicts: ['sys_business_type'],
  mixins:[tableListMixins],
  components: {DialogTemplate},
  data() {
    return {
      //选中的行
      selectRow:null,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      tableList: null,
      // 树选项
      orgTree: null,
      // 是否显示弹出层
      open: false,
      // 通过机构名称查询tree
      deptName: null,
      //是否关联下级
      checked: false,
      //自定义机构树数据
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 查询参数
      queryParams: {
        checkSub: false,
        orgId: null,
        isEnable:false,
        effectiveDate: null,
      },
      // 列信息
      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",
      };
    },
  },
  watch: {
    // 根据名称筛选机构树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getDeptTree();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },
  methods: {
    /** 查看 */
    lookItem(row) {
      this.selectRow = row;
      this.$refs.modalTemplate.show(row);
    },

    addTemplate(){
      this.$refs.modalTemplate.add();
    },
    /** 下穿状态改变*/
    changeCheckBox(){
      this.getList();
    },
    /** 查询列表 */
    getList() {
      let data = {
        ...this.queryParams,
        ...this.pages
      }
      weekTableList(data).then(response => {
          this.tableList = response.data.records;
          this.pages.total = response.data.total;
          this.loading = false;
        }
      ).catch(err=>{
        this.loading = false;
      })
    },
    /** 查询机构树 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.orgTree = response.data;
        console.log( this.deptOptions,' this.deptOptions')
        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`)
    },
  }
};
</script>
