<template>
  <div>
    <el-row :gutter="10">
      <!--机构数据-->
      <!-- <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="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.dateRange"
                  key="daterange"
                  type="daterange"
                  :editable="false"
                  :clearable="false"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </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>
            <!--    按纽    -->
            <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"
                  icon="el-icon-plus"
                  size="mini"
                  @click="addTemplate"
                  v-hasPermi="['system:time:add']"
                >新增作息模板</el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </div>
          <el-table
            v-loading="loading"
            border
            height="597"
            size="small"
            :data="tableList">
            <el-table-column label="序号" type="index" align="center" width="70"></el-table-column>
            <el-table-column prop="orgName" align="center" label="机构" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="effectiveDate" align="center" label="生效日期" width="160"></el-table-column>
            <el-table-column prop="workDay" align="center" label="工作日" width="300"></el-table-column>
            <el-table-column prop="modifiedName" align="center" label="更新人" width="160" show-overflow-tooltip></el-table-column>
            <el-table-column prop="updateTime" align="center" label="更新时间" width="180"></el-table-column>
            <el-table-column label="操作" align="center"  width="140" >
              <template slot-scope="r">
                  <el-button
                    size="mini"
                    type="text"
                    v-hasPermi="['system:time:edit']"
                    v-if="r.row.timeCheck===0||r.row.timeCheck===3"
                    v-show="ifOrg(r.row)"
                    @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="lookItem(r.row)">查看</el-button>-->
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-document"
                    @click="lookItem(r.row)">
                    查看
                  </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>


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

  </div>
</template>

<script>
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";
import DialogTemplate from './dialog.template'
/** 引入节点树接口*/
import { deptTreeSelect} from "@/api/system/public";
import {weekTableList} from './api'
import { mapGetters } from "vuex";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
export default {
  /**  得先引入字典才能使用 */
  dicts: ['sys_business_type','sys_org_type'],
  components: {DataRangePicker, 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,
        orgType:null,
        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: {
    ...mapGetters(["orgId"]),
    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: {
    ifOrg(row){
      console.log(row.orgId,this.orgId,'row')
      return true
    },
    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);
    },
    onEdit(row){
      this.selectRow = row;
      this.$refs.modalTemplate.edit(row);
    },
    addTemplate(){
      this.$refs.modalTemplate.add();
    },
    /** 下穿状态改变*/
    changeCheckBox(){
      this.queryParams.checkSub = !this.queryParams.checkSub;
      this.getList();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      weekTableList(this.queryParams).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.isEnable = false;
      this.queryParams.pageNum= 1;
      this.queryParams.pageSize=10;
      this.queryParams.dateRange = ['',''];
      this.queryParams.orgId=this.orgId;
      this.queryParams.orgType = null;
      this.queryParams.checkSub = true;
      this.$refs["orgTree"].setCheckSub(this.queryParams.checkSub)
      this.getList();
    },
  }
};
</script>
