<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="changeDays">
        <el-input
          v-model="queryParams.changeDays"
          placeholder="请输入倒班日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期天数" prop="cycleDays">
        <el-input
          v-model="queryParams.cycleDays"
          placeholder="请输入周期天数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班次A" prop="shiftA">
        <el-input
          v-model="queryParams.shiftA"
          placeholder="请输入班次A"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班次B" prop="shiftB">
        <el-input
          v-model="queryParams.shiftB"
          placeholder="请输入班次B"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceShiftRuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="ruleId" v-if="true"/>
      <el-table-column label="规则类型" align="center" prop="ruleType" />
      <el-table-column label="倒班日期" align="center" prop="changeDays" />
      <el-table-column label="周期天数" align="center" prop="cycleDays" />
      <el-table-column label="班次A" align="center" prop="shiftA">
        <template slot-scope="scope">
          {{ getShiftName(scope.row.shiftA) }}
        </template>
      </el-table-column>
      <el-table-column label="班次B" align="center" prop="shiftB">
        <template slot-scope="scope">
          {{ getShiftName(scope.row.shiftB) }}
        </template>
      </el-table-column>
      <el-table-column label="倒班日班次(白转夜)" align="center" prop="changeShiftBId">
        <template slot-scope="scope">
          {{ getShiftName(scope.row.changeShiftBId) }}
        </template>
      </el-table-column>
      <el-table-column label="倒班日班次(夜转白)" align="center" prop="changeShiftAId">
        <template slot-scope="scope">
          {{ getShiftName(scope.row.changeShiftAId) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="倒班日期" prop="changeDays">
          <el-input v-model="form.changeDays" placeholder="请输入倒班日期" />
        </el-form-item>
        <el-form-item label="周期天数" prop="cycleDays">
          <el-input v-model="form.cycleDays" placeholder="请输入周期天数" />
        </el-form-item>
        <el-form-item label="班次A" prop="shiftA">
          <el-select v-model="form.shiftA" placeholder="请选择班次A" style="width: 100%;">
            <el-option
              v-for="item in shiftList"
              :key="item.shiftId"
              :label="item.shiftName"
              :value="item.shiftId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班次B" prop="shiftB">
          <el-select v-model="form.shiftB" placeholder="请选择班次B" style="width: 100%;">
            <el-option
              v-for="item in shiftList"
              :key="item.shiftId"
              :label="item.shiftName"
              :value="item.shiftId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="倒班日班次(白转夜)" prop="changeShiftBId">
          <el-select v-model="form.changeShiftBId" placeholder="请选择倒班日班次(白转夜)" style="width: 100%;">
            <el-option
              v-for="item in shiftList"
              :key="item.shiftId"
              :label="item.shiftName"
              :value="item.shiftId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="倒班日班次(夜转白)" prop="changeShiftAId">
          <el-select v-model="form.changeShiftAId" placeholder="请选择倒班日班次(夜转白)" style="width: 100%;">
            <el-option
              v-for="item in shiftList"
              :key="item.shiftId"
              :label="item.shiftName"
              :value="item.shiftId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttendanceShiftRule, getAttendanceShiftRule, delAttendanceShiftRule, addAttendanceShiftRule, updateAttendanceShiftRule } from "@/api/wms/attendanceShiftRule";
import { listShift } from "@/api/wms/attendanceShift";

export default {
  name: "AttendanceShiftRule",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 倒班规则表格数据
      attendanceShiftRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 班次列表
      shiftList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruleType: undefined,
        changeDays: undefined,
        cycleDays: undefined,
        shiftA: undefined,
        shiftB: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shiftA: [
          { required: true, message: "班次A不能为空", trigger: "change" }
        ],
        shiftB: [
          { required: true, message: "班次B不能为空", trigger: "change" }
        ],
        changeShiftBId: [
          { required: true, message: "倒班日班次(白转夜)不能为空", trigger: "change" }
        ],
        changeShiftAId: [
          { required: true, message: "倒班日班次(夜转白)不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getShiftList();
  },
  methods: {
    /** 查询倒班规则列表 */
    getList() {
      this.loading = true;
      listAttendanceShiftRule(this.queryParams).then(response => {
        this.attendanceShiftRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取班次列表 */
    getShiftList() {
      listShift().then(response => {
        this.shiftList = response.rows || [];
      });
    },
    /** 获取班次名称和理论打卡时间 */
    getShiftName(shiftId) {
      if (!shiftId) return '';
      const shift = this.shiftList.find(item => item.shiftId === shiftId);
      if (!shift) return shiftId;
      let timeStr = '';
      if (shift.startTime && shift.endTime) {
        timeStr = `(${shift.startTime.slice(0, 5)}-${shift.endTime.slice(0, 5)}`;
        if (shift.startTime2 && shift.endTime2) {
          timeStr += `, ${shift.startTime2.slice(0, 5)}-${shift.endTime2.slice(0, 5)}`;
        }
        timeStr += ')';
      }
      return `${shift.shiftName} ${timeStr}`;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ruleId: undefined,
        ruleType: undefined,
        changeDays: undefined,
        cycleDays: undefined,
        shiftA: undefined,
        shiftB: undefined,
        changeShiftBId: undefined,
        changeShiftAId: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ruleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加倒班规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const ruleId = row.ruleId || this.ids
      getAttendanceShiftRule(ruleId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改倒班规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.ruleId != null) {
            updateAttendanceShiftRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addAttendanceShiftRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruleIds = row.ruleId || this.ids;
      this.$modal.confirm('是否确认删除倒班规则编号为"' + ruleIds + '"的数据项？').then(() => {
        this.loading = true;
        return delAttendanceShiftRule(ruleIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/attendanceShiftRule/export', {
        ...this.queryParams
      }, `attendanceShiftRule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
