<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" />
      </el-form-item>
      <el-form-item label="部门名称" prop="orgName">
        <el-input v-model="queryParams.orgName" />
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="所属部门" align="center" prop="orgName" :show-overflow-tooltip="true" width="240">
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" :show-overflow-tooltip="true" />
      <el-table-column label="请假开始时间" align="center" prop="leaveStartDate" :show-overflow-tooltip="true" >

      </el-table-column>
      <el-table-column label="请假结束时间" align="center" prop="leaveEndDate" :show-overflow-tooltip="true" >

      </el-table-column>
      <el-table-column label="请假时长(天)" align="center" prop="leaveDuration" :show-overflow-tooltip="true" width="180"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <!-- <el-button
            type="primary"
            @click="handleUpdate(scope.row)"
          >审核</el-button> -->
          <el-button
            type="primary"
            @click="handleView(scope.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-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假人员：" prop="userName">
              <el-input v-model="form.userName" :readonly="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门:" prop="orgName">
              <el-input v-model="form.orgName" :readonly="true" />
              <!-- <el-select v-model="form.orgId" disabled style="width: 100%;">
                <el-option
                  v-for="item in orgList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请时间:" prop="createTime">
              <el-input v-model="form.createTime" :readonly="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假类型:" prop="leaveType">
              <el-select v-model="form.leaveType" disabled style="width: 100%;">
                <el-option
                  v-for="item in leaveTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假开始时间：" prop="leaveStartDate">
              <el-input v-model="form.leaveStartDate" :readonly="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假结束时间:" prop="leaveEndDate">
              <el-input v-model="form.leaveEndDate" :readonly="true"/>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假时长(天)：" prop="leaveDuration">
              <el-input v-model="form.leaveDuration" :readonly="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="请假事由：" prop="leaveReason">
              <el-input type="textarea" v-model="form.leaveReason" maxlength="100" :readonly="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">

        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
 import { listHoliday,getHoliday,exportHoliday } from "@/api/process/rest";
import Treeselect from "@riophae/vue-treeselect";
import { getToken } from "@/utils/auth";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "rules",
  dicts: ['apply_status'],
  components: { Treeselect },
  data() {
    return {
      userInfo:this.$store.state.user,
      orgList:this.$store.state.user.orgList,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isView: false,
      leaveTypeList:[{id:'1',name:"病假"},{id:'2',name:"年休假"},{id:'3',name:"探亲假"},{id:'4',name:"事假"},{id:'5',name:"产假"},{id:'6',name:"婚假"},{id:'7',name:"丧假"}],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        leaveType:"假",
        type:"请假",
        userName: undefined,
        orgName: undefined,
      },
      personOptions:[],
      typeList:[{value:'1',label:'固定班制'},{value:'2',label:'自由工时'}],
      attDayList:[{id:0,label:'周一'},{id:1,label:'周二'},{id:2,label:'周三'},{id:3,label:'周四'},{id:4,label:'周五'},{id:5,label:'周六'},{id:6,label:'周日'}],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
        leaveStartDate: [
          { required: true, message: "请假开始时间不能为空", trigger: "blur" }
        ],
        leaveEndDate: [
          { required: true, message: "请假结束时间不能为空", trigger: "blur" }
        ],
        leaveDuration: [
          { required: true, message: "请假时长不能为空", trigger: "blur" }
        ],
        leaveReason: [
          { required: true, message: "请假事由不能为空", trigger: "blur" }
        ]
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/import/excelUpload"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listHoliday(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId:this.userInfo.id,
        userName:this.userInfo.name,
        orgId: undefined,
        leaveStartDate: undefined,
        leaveStartInterval:'1',
        leaveEndDate: undefined,
        leaveEndInterval:'1',
        leaveDuration:undefined,
        leaveReason:undefined,
        type:"请假",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.groupId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    handleView(row) {
      this.reset();
      const groupId = row.id
      getHoliday(groupId).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.open = true;
        this.isView = true;
        this.title = "请假单";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      console.log(this.form)
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
          // let arr=[0,0,0,0,0,0,0]
          // this.form.attDays.forEach(item=>{arr[item]=1})

          // let arr2 = []
          // this.form.checkList.forEach(item=>{
          //   let obj={
          //     orgId:item.split('|')[1],
          //     userId:item.split('|')[0]
          //   }
          //   arr2.push(obj)
          // })
          // this.form.checkOrgList=arr2;
          // console.log(this.form)
          // if (this.form.groupId != undefined) {
          //   this.form.checkOrgList.forEach(item=>{
          //     item.groupId=this.form.groupId
          //   })
          //   updateAttGroup(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   addAttGroup(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
      //   }
      // });
    },

    /** 导出按钮操作 */
    handleExport() {
        this.queryParams.exportType="查询";
        exportHoliday(this.queryParams).then(res => {
          this.downloadFile({ fileName: `请假单_${new Date().getTime()}.xlsx`, fileData: res, fileType: 'application/vnd.ms-excel;charset=utf-8' })
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/system/download/holidayFile', {}, `请假数据模板_${new Date().getTime()}.xlsx`)
      // this.download('system/user/importTemplate', {
      // }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }

  }
};
</script>
