<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body width="700px">
    <el-row class="table-title" style="margin-top: -20px;margin-bottom: 20px">
      <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>控制器采集设置</div>
      </el-col>
    </el-row>
    <el-form ref="controlForm" :rules="controlRule" :model="controlForm" label-position="right" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="12" >
          <el-form-item label="采集开始时间" prop="collectStartTime">
            <el-time-picker
              v-model="controlForm.collectStartTime"
              value-format="HH:mm:ss"
              placeholder="选择采集开始时间"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="采集间隔" prop="collectCycle">
            <el-input v-model.trim="controlForm.collectCycle" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" type="number"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" >
          <el-form-item label="采集次数" prop="collectCnt">
            <el-input v-model.trim="controlForm.collectCnt" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" type="number"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: center">
      <el-button type="primary" style="width: 100px;font-size: 16px;" @click="updateDataControl">保 存</el-button>
    </div>
    <el-row class="table-title" style="margin-top: 20px;margin-bottom: 20px">
      <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>灯箱采集设置</div>
      </el-col>
    </el-row>
    <el-form ref="boxForm" :rules="boxRule" :model="boxForm" label-position="right" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="12" >
          <el-form-item label="采集开始时间" prop="collectStartTime">
            <el-time-picker
              v-model="boxForm.collectStartTime"
              value-format="HH:mm:ss"
              placeholder="选择采集开始时间"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="采集间隔" prop="collectCycle">
            <el-input v-model.trim="boxForm.collectCycle" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" type="number"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" >
          <el-form-item label="采集次数" prop="collectCnt">
            <el-input v-model.trim="boxForm.collectCnt" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" type="number"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: center">
      <el-button type="primary" style="width: 100px;font-size: 16px;" @click="updateDataBox">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setConfig } from '@/api/system/strategy'
import { getConfig } from '@/api/system/strategy'
export default {
  name: 'EditTime',
  data() {
    return {
      deptShow: true,
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      controlForm: {
        deviceType: '1',
        collectStartTime: '',
        collectCycle: '',
        collectCnt: ''
      }, // 表单
      boxForm: {
        deviceType: '4',
        collectStartTime: '',
        collectCycle: '',
        collectCnt: ''
      }, // 表单
      controlRule: {
        collectStartTime: [{ required: true, message: '采集开始时间不能为空', trigger: ['blur', 'change'] }],
        collectCycle: [{ required: true, message: '采集间隔不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))$/, required: true, message: '请输入合法数字', trigger: ['blur', 'change'] }],
        collectCnt: [{ required: true, message: '采集次数不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))$/, required: true, message: '请输入合法数字', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      boxRule: {
        collectStartTime: [{ required: true, message: '采集开始时间不能为空', trigger: ['blur', 'change'] }],
        collectCycle: [{ required: true, message: '采集间隔不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))$/, required: true, message: '请输入合法数字', trigger: ['blur', 'change'] }],
        collectCnt: [{ required: true, message: '采集次数不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))$/, required: true, message: '请输入合法数字', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      textMap: {
        update: '采集设置'
      } // 表头显示标题
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证

      } else if (dialogStatus === 'update' || dialogStatus === 'detail') { // 如果是修改，将row中数据填写到输入框中
        getConfig('1').then(response => {
          if (response.code === 200) {
            this.controlForm.collectStartTime = response.data.collectStartTime
            this.controlForm.collectCycle = response.data.collectCycle
            this.controlForm.collectCnt = response.data.collectCnt
          }
        })
        getConfig('4').then(response => {
          if (response.code === 200) {
            this.boxForm.collectStartTime = response.data.collectStartTime
            this.boxForm.collectCycle = response.data.collectCycle
            this.boxForm.collectCnt = response.data.collectCnt
          }
        })
      }
    },
    // 重置表单
    resetForm() {
    },
    // 修改数据
    updateDataControl() {
      this.$refs['controlForm'].validate((valid) => {
        if (valid) {
          if (Number(this.controlForm.collectCycle) * (Number(this.controlForm.collectCnt) - 1) > 1440) {
            this.$message.error('数据采集周期*(数据采集次数-1)不能大于1440')
            return
          }
          setConfig(this.controlForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    updateDataBox() {
      this.$refs['boxForm'].validate((valid) => {
        if (valid) {
          if (Number(this.boxForm.collectCycle) * (Number(this.boxForm.collectCnt) - 1) > 1440) {
            this.$message.error('数据采集周期*(数据采集次数-1)不能大于1440')
            return
          }
          setConfig(this.boxForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight: 40px;
  .el-form-item {
    margin-bottom: 22px;
  }
  .el-select{
    width: 100%;
  }
  .table-title {
    background-color: rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header {
      line-height: $tableTitleHeight;
      color: #606266;
      font-size: 14px;
      font-weight: bold;
      i {
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
