<template>
  <div class="app-container">
    <el-card>
      <!--      <span style="margin-left: 15px">班次类型:</span>-->
      <el-select v-model="roleName" placeholder="班次类型" clearable size="small" style="width:200px">
        <el-option
          v-for="item in roleNames"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 15px" @click="searchTable()">查询</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addShifttable()">新增</el-button>
      <el-button type="warning" icon="el-icon-circle-plus-outline" plain size="small" @click="ShiftType()">班次类型工作时间</el-button>
    </el-card>

    <el-card>
      <el-table
        :data="tableData"
        border
        stripe
        size="mini"
        height="820"
        center
        style="width: 100%">
        <el-table-column label="开始工作时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.开始工作时间 }}
          </template>
        </el-table-column>
        <el-table-column disabled label="结束工作时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.结束工作时间 }}
          </template>
        </el-table-column>
        <el-table-column label="班次类型" align="center">
          <template slot-scope="scope">
            {{ scope.row.班次类型 }}
          </template>
        </el-table-column>
        <el-table-column label="班次代码" align="center">
          <template slot-scope="scope">
            {{ scope.row.班次代码 }}
          </template>
        </el-table-column>
        <el-table-column label="是否夜班" align="center">
          <template slot-scope="scope">
            {{ scope.row.是否夜班 }}
          </template>
        </el-table-column>
        <el-table-column label="是否工作" align="center">
          <template slot-scope="scope">
            {{ scope.row.是否工作 }}
          </template>
        </el-table-column>
        <el-table-column label="是否工作" align="center">
          <template slot-scope="scope">
            {{ scope.row.是否工作 }}
          </template>
        </el-table-column>
        <el-table-column label="日工作能力" align="center">
          <template slot-scope="scope">
            {{ scope.row.日工作能力 }}
          </template>
        </el-table-column>
        <el-table-column label="是否可以安排高难度件" align="center">
          <template slot-scope="scope">
            {{ scope.row.是否可以安排高难度件 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              plain
              icon="el-icon-edit"
              @click="aeditShifttable(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              plain
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      <div class="block" style="margin-top: 10px;">-->
      <!--        <el-pagination-->
      <!--          :current-page="pageCurrent"-->
      <!--          :page-sizes="[10, 20, 30, 40, 50]"-->
      <!--          :page-size="10"-->
      <!--          :total="total"-->
      <!--          layout="total, sizes, prev, pager, next, jumper"-->
      <!--          @size-change="handleSizeChange"-->
      <!--          @current-change="handleCurrentChange"/>-->
      <!--      </div>-->
    </el-card>
    <el-dialog v-el-drag-dialog :visible.sync ="dialogFormVisible1" title="新增班次人员" center style="min-height: 270px" width="900px">
      <el-form ref="form1" :model="form1" :rules="rules" label-position="right" label-width="160px" class="demo-ruleForm">
        <el-row>
          <el-col :span="10">
            <el-form-item label="开始工作时间" >
              <el-time-picker
                v-model="form1.value"
                :picker-options="{
                  selectableRange: '01:30:00 - 23:30:00'
                }"
                value-format="hh:mm:ss"
                size="mini"
                style="width: 200px"
                placeholder="任意时间点"/>
            </el-form-item>
            <el-form-item label="结束工作时间" >
              <el-time-picker
                v-model="form1.value2"
                :picker-options="{
                  selectableRange: '01:30:00 - 23:30:00'
                }"
                value-format="hh:mm:ss"
                size="mini"
                style="width: 200px"
                placeholder="任意时间点"/>
            </el-form-item>
            <el-form-item label="班次类型" >
              <el-input
                v-model="form1.shifttype"
                size="mini"
                style="width:200px"/>
            </el-form-item>
            <el-form-item label="班次代码" >
              <el-input v-model="form1.shiftnumber" size="mini" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="是否夜班" >
              <el-select v-model="form1.nightwork" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="是否工作" >
              <el-select v-model="form1.work" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="是否可以安排临时件" >
              <el-select v-model="form1.temporary" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="是否可以安排高难度件" >
              <el-select v-model="form1.difficult" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" >
              <el-input
                v-model="form1.note"
                size="mini"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOFF">取 消</el-button>
        <el-button type="primary" @click="submitAdd1()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible2" title="编辑" center style="min-height: 270px" width="900px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="10">
            <el-form-item label="开始工作时间" >
              <el-time-picker
                v-model="form1.value"
                :picker-options="{
                  selectableRange: '01:30:00 - 23:30:00'
                }"
                value-format="hh:mm:ss"
                size="mini"
                style="width: 200px"
                placeholder="任意时间点"/>
            </el-form-item>
            <el-form-item label="结束工作时间" >
              <el-time-picker
                v-model="form1.value2"
                :picker-options="{
                  selectableRange: '01:30:00 - 23:30:00'
                }"
                value-format="hh:mm:ss"
                size="mini"
                style="width: 200px"
                placeholder="任意时间点"/>
            </el-form-item>
            <el-form-item label="班次类型" >
              <el-input
                v-model="form2.shifttype"
                size="mini"
                style="width:200px"/>
            </el-form-item>
            <el-form-item label="班次代码" >
              <el-input v-model="form2.shiftnumber" size="mini" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="是否夜班" >
              <el-select v-model="form2.nightwork" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="是否工作" >
              <el-select v-model="form2.work" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="是否可以安排临时件" >
              <el-select v-model="form2.temporary" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="是否可以安排高难度件" >
              <el-select v-model="form2.difficult" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" >
              <el-input
                v-model="form2.note"
                size="mini"
                style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOFF">取消</el-button>
        <el-button type="primary" @click="submitAdd2()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getScheduling, searchProgramme, addShift, editShift, deleteShift, addShiftType } from '@/api/BasicData/ShiftManagement'
export default {
  data() {
    return {
      roleName: '',
      roleNames: [],
      tableData: [],
      hour: '',
      minutes: '',
      seconds: '',
      tableDatate: [],
      dialogFormVisible: false,
      form1: {
        value: '',
        value2: '',
        shifttype: '',
        shiftnumber: '',
        nightwork: '',
        work: '',
        temporary: '',
        difficult: '',
        note: ''
      },
      options: [{
        value: '1',
        label: '是'
      }, {
        value: '0',
        label: '否'
      }],
      rules: { // 表单验证规则
        value: [{ required: true, message: '请选择时间', trigger: 'blur' }],
        value2: [{ required: true, message: '请选择时间', trigger: 'blur' }],
        shifttype: [{ required: true, message: '请输入班次', trigger: 'change' }]
      },
      form2: {
        value: new Date(2016, 9, 10, 18, 40),
        value2: new Date(2016, 9, 10, 18, 40),
        shifttype: '',
        shiftnumber: '',
        nightwork: '',
        work: '',
        temporary: '',
        difficult: '',
        note: ''
      },
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      // pageCurrent: 1,
      // pageSize: 10,
      // total: 0,
      rules2: { // 表单验证规则
        value: [{ required: true, message: '请选择时间', trigger: 'blur' }],
        value2: [{ required: true, message: '请选择时间', trigger: 'blur' }],
        shifttype: [{ required: true, message: '请输入班次', trigger: 'change' }]
      },
      listnumber: ''
    }
  },
  created() {
    this.searchData()
  },
  methods: {
    searchData() {
      searchProgramme().then(response => {
        console.log(response.data)
        for (let i = 0; i < response.data.length; i++) {
          this.roleNames.push({
            label: response.data[i].班次代码,
            value: response.data[i].班次代码
          })
        }
      })
    },
    searchTable() {
      getScheduling(this.roleName).then(response => {
        this.tableDatate = response.data
        for (let i = 0; i < this.tableDatate.length; i++) {
          this.hour = this.tableDatate[i].开始工作时间.Hours
          this.minutes = this.tableDatate[i].开始工作时间.Minutes
          this.seconds = this.tableDatate[i].开始工作时间.Seconds
          this.tableDatate[i].开始工作时间 = JSON.stringify(this.tableDatate[i].开始工作时间)
          this.tableDatate[i].开始工作时间 = this.hour.toString() + ':' + this.minutes.toString() + ':' + this.seconds.toString()
        }
        for (let i = 0; i < this.tableDatate.length; i++) {
          this.hour = this.tableDatate[i].结束工作时间.Hours
          this.minutes = this.tableDatate[i].结束工作时间.Minutes
          this.seconds = this.tableDatate[i].结束工作时间.Seconds
          this.tableDatate[i].结束工作时间 = JSON.stringify(this.tableDatate[i].结束工作时间)
          this.tableDatate[i].结束工作时间 = this.hour.toString() + ':' + this.minutes.toString() + ':' + this.seconds.toString()
        }
        this.tableData = this.tableDatate
        this.total = this.tableData.length
      })
    },
    addShifttable() {
      this.dialogFormVisible1 = true
    },
    aeditShifttable(row) {
      this.listnumber = row.序号
      this.form2.value = row.开始工作时间
      this.form2.value2 = row.结束工作时间
      this.form2.shifttype = row.班次类型
      this.form2.shiftnumber = row.班次代码
      this.form2.nightwork = row.是否夜班
      this.form2.work = row.是否工作
      this.form2.temporary = row.是否可以安排临时件
      this.form2.difficult = row.是否可以安排高难度件
      this.form2.note = row.备注
      this.dialogFormVisible2 = true
    },
    submitAdd1() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          addShift(this.form1.value, this.form1.value2, this.form1.shifttype, this.form1.shiftnumber, this.form1.nightwork, this.form1.work, this.form1.temporary, this.form1.difficult, this.form1.note).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.form1 = {}
              this.searchTable()
              this.dialogFormVisible1 = false
            } else { this.$message.error('增加失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    ShiftType() {
      addShiftType().then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('增加成功')
          this.searchTable()
        } else { this.$message.error('增加失败') }
      })
    },
    submitAdd2() {
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          editShift(this.listnumber, this.form2.value, this.form2.value2, this.form2.shifttype, this.form2.shiftnumber, this.form2.nightwork, this.form2.work, this.form2.temporary, this.form2.difficult, this.form2.note).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('修改成功')
              this.searchTable()
              this.dialogFormVisible2 = false
              this.form2 = {}
            } else { this.$message.error('修改失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleDelete(row) { // 删除
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteShift(row.序号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
          } else { this.$message.error('删除失败') }
          this.searchTable()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    callOFF() {
      this.dialogFormVisible1 = false
      this.form1 = {}
      this.dialogFormVisible2 = false
      this.form2 = {}
    }
    // handleSizeChange(val) {
    //   this.pageSize = val
    //   this.pageCurrent = 1
    //   this.searchTable()
    // },
    // handleCurrentChange(val) {
    //   this.pageCurrent = val
    //   this.searchTable()
    // }
  }
}
</script>

<style scoped>
  .el-card{
    margin: 0px;
  }
</style>
