<template>
  <div class="app-container">
    <el-card>
      <!--      <span style="margin-left: 15px">班次类型:</span>-->
      <el-select v-model="roleName" placeholder="序号" clearable size="small" style="width:100px">
        <el-option
          v-for="item in roleNames"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 15px" plain @click="searchTable()">查询</el-button>
      <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="addShiftTable('form')">新增</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 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="操作" 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>
            <el-button
              size="mini"
              type="text"
              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="20"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </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="serialNumber" style="display: inline-block">
          <el-input
            :disabled="title === '编辑'"
            v-model="form.serialNumber"
            clearable
            size="small"
            placeholder="序号"/>
        </el-form-item>
        <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-item label="是否工作" prop="shiftCode" style="display: inline-block">
          <el-select v-model="form.isWork" placeholder="是否工作" clearable size="small" style="width:200px">
            <el-option
              v-for="item in isWork"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </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="submitAdd('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 = []
      let roleName_check
      this.roleName ? roleName_check = 1 : roleName_check = 0
      var param = []
      param[0] = ['序号_ischeck', roleName_check]
      param[1] = ['序号', this.roleName]
      param[2] = ['PageCurrent', this.pageCurrent]
      param[3] = ['PageSize', this.pageSize]
      param[4] = ['PageCount', '1111', 'int', '1']
      param[5] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '电子看板_工作时间与班次_查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.result.length !== 0) {
          // this.tableData = response.data.result
          // eslint-disable-next-line no-unused-vars
          let startTime, endTime, H, H1, M, M1, S, S1
          for (let i = 0; i < response.data.result.length; i++) {
            startTime = ''
            endTime = ''
            H = response.data.result[i].开始工作时间.Hours
            H = H < 10 ? ('0' + H) : H
            M = response.data.result[i].开始工作时间.Minutes
            M = M < 10 ? ('0' + M) : M
            S = response.data.result[i].开始工作时间.Seconds
            S = S < 10 ? ('0' + S) : S
            H1 = response.data.result[i].结束工作时间.Hours
            H1 = H1 < 10 ? ('0' + H1) : H1
            M1 = response.data.result[i].结束工作时间.Minutes
            M1 = M1 < 10 ? ('0' + M1) : M1
            S1 = response.data.result[i].结束工作时间.Seconds
            S1 = S1 < 10 ? ('0' + S1) : S1
            startTime = H + ':' + M + ':' + S
            endTime = H1 + ':' + M1 + ':' + S1
            this.tableData.push({
              序号: response.data.result[i].序号,
              开始工作时间: startTime,
              结束工作时间: endTime,
              班次代码: response.data.result[i].班次代码,
              是否工作: response.data.result[i].是否工作
            })
          }
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
        console.log(this.tableData, 'this.tableData')
      })
    },
    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.serialNumber]
          param[1] = ['开始工作时间', this.form.startTime]
          param[2] = ['结束工作时间', this.form.endTime]
          param[3] = ['班次代码', this.form.shiftCode]
          param[4] = ['是否工作', this.form.isWork]
          param[5] = ['ResultID', '1', 'int', '1']
          var Data = this.CreateData('12', '电子看板_工作时间与班次_增加', param)
          this.ExecDatabase(Data).then(response => {
            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()
      }
      if (row.是否工作 === '工作') {
        this.form.isWork = 1
      }
      if (row.是否工作 === '不工作') {
        this.form.isWork = 0
      }
      this.title = '编辑'
      this.form.serialNumber = row.序号
      this.form.startTime = row.开始工作时间
      this.form.endTime = row.结束工作时间
      this.form.shiftCode = row.班次代码
      // this.form.isWork = 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[2] = ['结束工作时间', this.form.endTime]
          param[3] = ['班次代码', this.form.shiftCode]
          param[4] = ['是否工作', this.form.isWork]
          param[5] = ['ResultID', '1', 'int', '1']
          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>

