<template>
  <div class="app-container">
    <el-card>
      <!--      <span style="margin-left: 15px">班次类型:</span>-->
      <el-button type="primary" icon="el-icon-refresh" size="small" plain @click="searchTable()">刷新</el-button>
      <el-table
        :data="tableData"
        border
        stripe
        size="mini"
        height="750"
        center
        style="width: 100%">
        <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="text"
              plain
              icon="el-icon-edit"
              @click="editShiftTable(scope.row)">编辑</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" :title="title" center style="min-height: 200px;" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px">
        <el-form-item label="开始工作时间" prop="startTime" style="display: inline-block">
          <el-time-picker v-model="form.startTime" :clearable="false" :picker-options="{selectableRange: '00:00:00 - 23:59:59'}" size="small" value-format="HH:mm:ss" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="结束工作时间" prop="endTime" style="display: inline-block">
          <el-time-picker v-model="form.endTime" :clearable="false" :picker-options="{selectableRange: '00:00:00 - 23:59:59'}" size="small" value-format="HH:mm:ss" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="班次代码" prop="shiftCode" style="display: inline-block">
          <el-input
            v-model="form.shiftCode"
            clearable
            size="small"
            placeholder="班次代码"/>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        
        <el-button v-if="title === '编辑'" type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      roleName: '',
      roleNames: [],
      tableData: [],
      hour: '',
      minutes: '',
      seconds: '',
      dialogFormVisible: false,
      isWork: [
        {
          label: '不工作',
          value: 0
        },
        {
          label: '工作',
          value: 1
        }
      ],
      form: {
        serialNumber: '',
        startTime: '',
        endTime: '',
        shiftCode: '',
        isWork: ''
      },
      rules: { // 表单验证规则
        serialNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        startTime: [{ required: true, message: '请输入', trigger: 'blur' }],
        endTime: [{ required: true, message: '请输入', trigger: 'blur' }],
        isWork: [{ required: true, message: '请选择', trigger: 'change' }],
        shiftCode: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      title: '',
      pageCurrent: 1,
      pageSize: 20,
      total: 0
    }
  },
  created() {
    this.getNumber()
    this.searchTable()
  },
  methods: {
    // 获取工位名称
    getNumber() {
      this.roleNames = []
      request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `SELECT [序号],[开始工作时间],[结束工作时间],[班次代码],CASE 是否工作 WHEN 1 THEN '工作' ELSE '不工作' End AS 是否工作
                    FROM [dbo].[电子看板_工作时间与班次] WHERE [序号]<>'0'`
        }
      }).then(response => {
        if (response.data.length) {
          for (let i = 0; i < response.data.length; i++) {
            this.roleNames.push({
              label: response.data[i].序号,
              value: response.data[i].序号
            })
          }
        }
      })
    },
    // 查询订单
    searchTable() {
      this.tableData = []
      var Data = this.CreateData('11', '基本数据_人员班组管理_班次信息_查询')
      this.ExecDatabase(Data).then(response => {
        const res = response && response.data ? response.data : []
        if (res && res.length) {
          this.tableData = res
        }
        this.loading = false
      })
    },
    addShiftTable(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.title = '增加'
      this.form.serialNumber = ''
      this.form.startTime = ''
      this.form.endTime = ''
      this.form.shiftCode = ''
      this.form.isWork = ''
      this.dialogFormVisible = true
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['班次代码', this.form.shiftCode]
          param[1] = ['开始时间', this.form.startTime]
          param[2] = ['结束时间', this.form.endTime]
          var Data = this.CreateData('12', '电子看板_工作时间与班次_增加', param)
          this.ExecDatabase(Data).then(response => {
            this.$message({ message: '修改成功', type: 'success' })
            this.searchTable()
            return
            console.log(response)
            if (response.data.result[0].result === '1') {
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.dialogFormVisible = false
              this.searchTable()
            } else {
              this.$message.error('增加失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    editShiftTable(row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '编辑'
      this.form.startTime = row.开始时间
      this.form.endTime = row.结束时间
      this.form.shiftCode = row.班次代码
      this.dialogFormVisible = true
    },
    submitAdd1() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['序号', this.form.serialNumber]
          param[1] = ['开始工作时间', this.form.startTime]
          param[0] = ['班次代码', this.form.shiftCode]
          param[1] = ['开始时间', this.form.startTime]
          param[2] = ['结束时间', this.form.endTime]
          var Data = this.CreateData('12', '基本数据_人员班组管理_班次信息_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data.result[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('修改失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['序号', row.序号]
        var Data = this.CreateData('12', '电子看板_工作时间与班次_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchTable()
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    callOff() {
      this.dialogFormVisible = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
</style>
