<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="shiftName">
        <el-input v-model="queryParams.shiftName" placeholder="请输入排班名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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="handleAdd"
          v-hasPermi="['attendance:scheduling-manage:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['attendance:scheduling-manage:delete']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
          v-hasPermi="['attendance:scheduling-manage:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schedulingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"align="center"/>
      <el-table-column label="排班名称" align="center" prop="shiftName" />
      <el-table-column label="时间安排" align="center">
        <template v-slot="scope">
          <span>{{ scope.row.startTime + '~' + scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['attendance:scheduling-manage:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['attendance:scheduling-manage:delete']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-detail" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排班名称" prop="shiftName">
          <el-input v-model="form.shiftName" placeholder="请输入排班名称" />
        </el-form-item>
        <el-form-item label="上班时间" prop="startTime">
          <el-time-picker
            value-format="HH:mm"
            format="HH:mm"
            v-model="form.startTime"
            :picker-options="{
              selectableRange: '00:00:00 - 23:59:00'
            }"
            placeholder="上班时间" style="width: 100%;">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="下班时间" prop="endTime">
          <el-time-picker
            value-format="HH:mm"
            format="HH:mm"
            v-model="form.endTime"
            :picker-options="{
              selectableRange: '00:00:00 - 23:59:00'
            }"
            placeholder="下班时间" style="width: 100%;">
          </el-time-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="800px" append-to-body>
      <div class="widget-wrapper">
        <div class="widget-list" v-loading="loading">
          <div class="fx-field x-grid-col-12">
            <div class="field-label">
              <div class="field-name">排班名称</div>
            </div>
            <div class="field-component">
              <span>{{ form.shiftName }}</span>
            </div>
          </div>
          <div class="fx-field x-grid-col-12">
            <div class="field-label">
              <div class="field-name">上班时间</div>
            </div>
            <div class="field-component">
              <span>{{ form.startTime }}</span>
            </div>
          </div>
          <div class="fx-field x-grid-col-12">
            <div class="field-label">
              <div class="field-name">下班时间</div>
            </div>
            <div class="field-component">
              <span>{{ form.endTime }}</span>
            </div>
          </div>

        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScheduling, getScheduling, delScheduling, addScheduling, updateScheduling, exportScheduling } from "@/api/personnel/attendance/scheduling/index";
export default {
  name: "SystemPost",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排班表格数据
      schedulingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        shiftName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shiftName: [
          { required: true, message: "排班名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: '请选择上班时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择下班时间', trigger: 'change' }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排班列表 */
    getList() {
      this.loading = true;
      listScheduling(this.queryParams).then(response => {
        this.schedulingList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.files = [],
      this.form = {
        name: undefined,
        startTime: undefined,
        endTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排班";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id;
      getScheduling(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getScheduling(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排班";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateScheduling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScheduling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据项?').then(function () {
        return delScheduling(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      // 执行导出
      this.$modal.confirm('是否确认导出所有据项?').then(() => {
        this.exportLoading = true;
        return exportScheduling(params);
      }).then(response => {
        this.$download.excel(response, '排班信息.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
<style lang="scss" scoped>
.widget-wrapper>.widget-list {
  display: flex;
  flex-wrap: wrap;
}

.x-grid-col-6 {
  width: 50%;
}

.x-grid-col-12 {
  width: 100%;
}

.fx-field {
  line-height: 20px;
  padding: 7px 12px 12px;
  position: relative;
}

.fx-field .field-label {
  word-wrap: break-word;
  color: #141e31;
  display: flex;
  font-weight: 600;
  line-height: 20px;
  padding: 5px 0;
  position: relative;
  word-break: break-word;
}

.fx-field .field-label .field-required {
  color: #eb5050;
  margin-left: -6px;
  position: relative;
}

.fx-field .field-label .field-name {
  overflow: hidden;
}

.field-component {
  word-wrap: break-word;
  background: #f5f6f8;
  border-radius: 2px;
  color: #141e31;
  font-size: 14px;
  line-height: 20px;
  min-height: 32px;
  padding: 6px 8px;
  white-space: pre-wrap;
  word-break: break-word;
}

ul {
  padding-left: 0px !important;
}

.el-tag+.el-tag {
  margin-left: 10px;
}
</style>