<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="dutyName">
        <el-input v-model="queryParams.dutyName" placeholder="请输入标题" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="类型" prop="dutyType">
        <el-select v-model="queryParams.dutyType" style="width: 100%" placeholder="请选择类型">
          <el-option
            v-for="dict in dict.type.dutytype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="节日" prop="holidayName">
        <el-input v-model="queryParams.holidayName" placeholder="请输入节假日名称" 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-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
      v-hasPermi="['SchoolDuty:duty:add']"
    >新增
    </el-button>
    <el-table v-loading="loading" :data="dutyList">
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="标题" align="center" min-width="150px" show-overflow-tooltip prop="dutyName"/>
      <el-table-column label="类型" align="center" prop="dutyType">
        <template slot-scope="scope">
          {{ getDutyType(scope.row.dutyType) }}
        </template>
      </el-table-column>
      <el-table-column label="节日" align="center" prop="holidayName"/>
      <el-table-column label="说明" min-width="100px" align="center" prop="remarks"/>
      <el-table-column label="操作" align="center" min-width="100px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.state!=1"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['SchoolDuty:duty:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit-outline" v-if="scope.row.state!=1"
                     @click="dutyArrange(scope.row)"
                     v-hasPermi="['SchoolDuty:duty:edit']">值班安排
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-s-promotion" v-if="scope.row.state!=1"
                     @click="publish(scope.row)"
                     v-hasPermi="['SchoolDuty:duty:edit']">发布
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" v-if="scope.row.state!=1"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['SchoolDuty:duty:remove']">删除
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-tickets" v-if="scope.row.state==1"
                     @click="dutySchedule(scope.row)"
                     v-hasPermi="['SchoolDuty:duty:edit']">值班表
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-refresh-left" v-if="scope.row.state==1"
                     @click="withdraw(scope.row)"
                     v-hasPermi="['SchoolDuty:duty:edit']">撤回
          </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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="dutyName">
          <el-input v-model="form.dutyName" placeholder="请输入标题"/>
        </el-form-item>
        <el-row>
          <el-col :span="10">
            <el-form-item label="假日名称" prop="holidayName">
              <el-input v-model="form.holidayName" placeholder="请输入假日名称"/>
            </el-form-item>
            <el-form-item label="开始时间" prop="startDate">
              <el-date-picker style="width: 100%" clearable v-model="form.startDate" type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择值班开始时间"
                              :picker-options="limitStart"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="值班类型" prop="dutyType">
              <!--<el-radio v-model="form.dutyType" label="0">节日</el-radio>-->
              <!--<el-radio v-model="form.dutyType" label="1">寒暑假</el-radio>-->
              <el-radio-group v-model="form.dutyType">
                <el-radio
                  v-for="dict in dict.type.dutytype"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="结束时间" prop="endDate">
              <el-date-picker style="width: 100%" clearable v-model="form.endDate" type="date" value-format="yyyy-MM-dd"
                              placeholder="请选择值班结束时间"
                              :picker-options="limitEnd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="说明" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" :autosize="{ minRows: 3, maxRows: 5}"
                    placeholder="请输入内容"/>
        </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>
  </div>
</template>

<script>
import {
  listDuty,
  getDuty,
  delDuty,
  addDuty,
  updateDuty,
  infoPublish,
  infoWithdraw
} from '@/api/smartSchool/schoolManage/dutyManage/index'

export default {
  name: 'Duty',
  dicts: ['dutytype'],
  data() {
    return {
      //值班类型
      // dutyTypeDictList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSchoolDutyDetails: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 值班管理表格数据
      dutyList: [],
      // 值班管理明细表格数据
      schoolDutyDetailsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dutyName: null,
        dutyType: null,
        holidayName: null,
        startDate: null,
        endDate: null,
        remarks: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        holidayName: [{required: true, message: '假日名称不能为空', trigger: 'blur'}],
        startDate: [{required: true, message: '开始时间不能为空', trigger: 'blur'}],
        endDate: [{required: true, message: '结束时间不能为空', trigger: 'blur'}],
        dutyName: [
          {required: true, message: "标题不能为空", trigger: "blur"},
        ],
        dutyType: [
          {required: true, message: "类型不能为空", trigger: "blur"},
        ]
      },
      //时间范围限制 开始时间不能晚于结束时间
      limitStart: {
        disabledDate: time => Date.parse(time) < Date.parse(new Date()) - 8.64e7 || Date.parse(time) > Date.parse(this.form.endDate)
      },
      limitEnd: {
        disabledDate: time => Date.parse(time) < Date.parse(this.form.startDate) || Date.parse(time) < Date.parse(new Date())
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //值班安排
    dutyArrange(row) {
      this.$router.push({
        // path: '/schoolManage/schoolMomentsDetail/' + row.id,
        path: '/schoolManage/dutyManage/dutyArrange',
        query: {
          dutyId: row.id,
          dutyType: row.dutyType,
          limitStartDate: row.startDate,
          limitEndDate: row.endDate
        }
      })
    },
    //值班表
    dutySchedule(row) {
      this.$router.push({
        path: '/schoolManage/dutyManage/dutySchedule',
        query: {
          dutyId: row.id,
          dutyName: row.dutyName,
          holidayName: row.holidayName,
          dutyType: row.dutyType
        }
      })
    },
    //获取数据字典列表
    getDutyType(dutyType) {
      if (this.dict.type.dutytype.length != 0) {
        for (let i = 0; i < this.dict.type.dutytype.length; i++) {
          let item = this.dict.type.dutytype[i]
          if (dutyType == item.value) return item.label
        }
      }
    },
    /** 查询值班管理列表 */
    getList() {
      this.loading = true
      listDuty(this.queryParams).then(response => {
        this.dutyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dutyName: null,
        dutyType: null,
        holidayName: null,
        startDate: null,
        endDate: null,
        remarks: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      }
      this.schoolDutyDetailsList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加值班管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDuty(id).then(response => {
        this.form = response.data
        this.schoolDutyDetailsList = response.data.schoolDutyDetailsList
        this.open = true
        this.title = '修改值班管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.schoolDutyDetailsList = this.schoolDutyDetailsList
          if (this.form.id != null) {
            updateDuty(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDuty(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 delDuty(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 值班管理明细序号 */
    rowSchoolDutyDetailsIndex({row, rowIndex}) {
      row.index = rowIndex + 1
    },
    /** 值班管理明细添加按钮操作 */
    handleAddSchoolDutyDetails() {
      let obj = {}
      obj.dutyId = ''
      obj.dutyDeDate = ''
      obj.dutyDeWeek = ''
      obj.dutyDeType = ''
      obj.dutyDeUser = ''
      obj.dutyDeUsername = ''
      obj.dutyDePhone = ''
      obj.dutyDeTel = ''
      obj.remarks = ''
      this.schoolDutyDetailsList.push(obj)
    },
    /** 值班管理明细删除按钮操作 */
    handleDeleteSchoolDutyDetails() {
      if (this.checkedSchoolDutyDetails.length == 0) {
        this.$modal.msgError('请先选择要删除的值班管理明细数据')
      } else {
        const schoolDutyDetailsList = this.schoolDutyDetailsList
        const checkedSchoolDutyDetails = this.checkedSchoolDutyDetails
        this.schoolDutyDetailsList = schoolDutyDetailsList.filter(function (item) {
          return checkedSchoolDutyDetails.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleSchoolDutyDetailsSelectionChange(selection) {
      this.checkedSchoolDutyDetails = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'SchoolDuty/duty/export',
        {
          ...this.queryParams
        },
        `duty_${Date.now()}.xlsx`
      )
    },
    publish(row) {
      infoPublish(row.id).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("发布成功");
        }
        this.handleQuery()
      }).catch(() => {
          this.$modal.msgError("发布失败")
        }
      )
    },
    withdraw(row) {
      infoWithdraw(row.id).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("撤回成功");
        }
        this.handleQuery()
      }).catch(() => {
          this.$modal.msgError("撤回失败")
        }
      )
    },
  }
}
</script>
