<template>
  <div class="app-container">
    <el-row v-if="optType !='view'" :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shiftList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="orderNum" width="50"/>
      <el-table-column label="班次名称" align="center" prop="shiftName" />
      <el-table-column label="工作开始时间" align="center" prop="startTime" width="100">
      </el-table-column>
      <el-table-column label="工作结束时间" align="center" prop="endTime" width="100">
      </el-table-column>
      <!-- <el-table-column label="休息开始时间" align="center" prop="breakStartTime" width="100">
      </el-table-column>
      <el-table-column label="休息结束时间" align="center" prop="breakEndTime" width="100">
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="70" v-if="optType !='view'" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, scope.$index)"
            v-hasPermi="['mes:cal:calplan:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改计划班次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
            <!-- <el-col :span="12">
                <el-form-item label="序号" prop="orderNum">
                    <el-input-number :min="1" v-model="form.orderNum" placeholder="请输入序号" />
                </el-form-item>
            </el-col> -->
            <el-col :span="12">
                <el-form-item label="班次名称" prop="shiftName">
                  <el-select
                    v-model="form.shiftName"
                    clearable
                    placeholder="请选择班次"
                  >
                    <el-option
                      v-for="dict in dict.type.SHIFT_TYPE"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.label"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="工作开始时间" prop="startTime">
                <el-time-picker
                v-model="form.startTime"
                placeholder="请选择工作开始时间"
                format ="HH:mm"
                value-format="HH:mm"
                >
                </el-time-picker>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="工作结束时间" prop="endTime">
                <el-time-picker
                v-model="form.endTime"
                placeholder="请选择工作结束时间"
                format ="HH:mm"
                value-format="HH:mm"
                >
                </el-time-picker>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
                </el-form-item>
            </el-col>
          </el-row>
          <div class="box">
            <el-row class="row" v-for="(item, index) in breakTimeList" :key="index">
              <el-col class="td" :span="22">
                <el-col :span="11">
                  <el-form-item label="休息开始时间" prop="breakStartTime">
                  <el-time-picker
                  v-model="item.breakStartTime"
                  placeholder="请选择休息开始时间"
                  format ="HH:mm"
                  value-format="HH:mm"
                  >
                  </el-time-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="休息结束时间" prop="breakEndTime">
                    <el-time-picker
                    v-model="item.breakEndTime"
                    placeholder="请选择休息结束时间"
                    format ="HH:mm"
                    value-format="HH:mm"
                    >
                    </el-time-picker>
                    </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="1">
                <i class="el-icon-plus" @click="addlist" />
                <i class="el-icon-minus" @click="removelist(index)" />
              </el-col>
            </el-row>
          </div>
      </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>
  </div>
</template>

<script>
import { listShift, listEditShift } from "@/api/mes/cal/shift";

export default {
  name: "Shift",
  dicts: ['SHIFT_TYPE'],
  props: {
      shiftType: null,
      planId: null,
      optType: null
  },
  watch: {
    // 根据名称筛选分类树
    shiftType: {
      handler (newName, oldName) {
        console.log(newName, 'shiftType')
        if (newName) {
          if (this.planId&&!this.editShiftType) {
            this.getEditList();
          } else if ((this.optType === 'view' || this.optType === 'edit')&&(this.editShiftType && this.editShiftType === newName)) {
            this.getEditList();
          } else {
              this.getList();
          }
        }
      },
      immediate: true
    },
    planId: {
      handler (newName, oldName) {
        if (newName) {
          this.editShiftType = this.shiftType
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      editShiftType: null,
      currentIndex: null,
      currentRow: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 计划班次表格数据
      shiftList: [],
      // 休息时间list
      breakTimeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        shiftType: this.shiftType,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shiftName: [
          { required: true, message: "班次名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {

  },
  methods: {
    removelist(index) {
      if(this.breakTimeList.length <= 1) {
        return this.$message.warning('最少需要一个休息时段')
      }
      this.breakTimeList.splice(index, 1)
    },
    addlist(){
      if(this.breakTimeList.length >= 2) {
        return this.$message.warning('最多只能增加两个休息时段')
      }
      this.breakTimeList.push({breakStartTime: null, breakEndTime: null})
    },
    /** 查询计划班次列表 */
    getList() {
      this.loading = true;
      this.queryParams.shiftType = this.shiftType
      listShift(this.queryParams).then(response => {
        this.shiftList = response.data;
        this.loading = false;
      });
    },
    /** 查询计划班次列表 */
    getEditList() {
      this.loading = true;
      listEditShift({planId: this.planId}).then(response => {
        this.shiftList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row, index) {
      this.reset();
      this.currentIndex = index
      this.currentRow = row
      this.form = Object.assign({}, row);
      // this.breakTimeList = row.breakTimeList
      this.breakTimeList = []
      this.breakTimeList.push({breakStartTime: row.breakStartTime, breakEndTime: row.breakEndTime})
      this.open = true;
      this.title = "修改计划班次";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // this.$modal.msgSuccess("修改成功");
          this.currentRow.shiftName = this.form.shiftName
          this.currentRow.startTime = this.form.startTime
          this.currentRow.endTime = this.form.endTime
          this.currentRow.breakEndTime = this.form.breakEndTime
          this.currentRow.breakStartTime = this.form.breakStartTime
          this.currentRow.remark = this.form.remark
          this.currentRow.breakTimeList = this.breakTimeList
          this.open = false;
          // this.$set(this.shiftList, this.currentIndex, this.form)
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.box {
  // height: 36px;
  width: 100%;
  padding-top: 10px;
  border: 1px solid #dcdfe6;
  position: relative;
  &::after {
    height: 100%;
    width: 1px;
    position: absolute;
    top: 0;
    right: 79px;
    display: inline-block;
    content: '';
    background: #dcdfe6;
  }
  .el-icon-plus, .el-icon-minus {
    line-height: 36px;
    width: 13px;
    display: inline-block;
    height: 36px;
    padding-left: 15px;
    cursor: pointer;
  }
  .el-icon-plus {
    padding-left: 0;
  }
}
</style>
