<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="dataForm"
      status-icon
      label-width="300px"
    >
      <el-form-item label="下次外拨间隔时间-->无应答" prop="jz_call_noanswer" type="number">
        <el-input v-model="dataForm.jz_call_noanswer">
          <template slot="append">小时</template>
        </el-input>
      </el-form-item>
      <el-form-item label="下次外拨间隔时间-->通话中" prop="jz_call_busy">
        <el-input v-model="dataForm.jz_call_busy" type="number">
          <template slot="append">小时</template>
        </el-input>
      </el-form-item>
      <el-form-item label="下次外拨间隔时间-->关机" prop="jz_call_power_off" type="number">
        <el-input v-model="dataForm.jz_call_power_off">
          <template slot="append">小时</template>
        </el-input>
      </el-form-item>
      <el-form-item label="下次外拨间隔时间-->停机" prop="jz_call_overdue" type="number">
        <el-input v-model="dataForm.jz_call_overdue">
          <template slot="append">小时</template>
        </el-input>
      </el-form-item>
      <el-form-item label="下次外拨间隔时间-->无法拨通" prop="jz_call_no_connected" type="number">
        <el-input v-model="dataForm.jz_call_no_connected">
          <template slot="append">小时</template>
        </el-input>
      </el-form-item>
      <el-form-item />
      <el-form-item label="未接通连续拨打次数" prop="jz_call_no_connnect_times" type="number">
        <el-input v-model="dataForm.jz_call_no_connnect_times">
          <template slot="append">次数</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="update">确定</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { listWx, updateWx } from '@/api/config'

export default {
  name: 'ConfigWx',
  data() {
    return {
      dataForm: {
        jz_call_noanswer: 0,
        jz_call_talking: 0,
        jz_call_power_off: 0,
        jz_call_overdue: 0,
        jz_call_no_connected: 0,
        jz_call_no_connnect_times: 3
      },
      rules: {
        jz_call_noanswer: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        jz_call_talking: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        jz_call_power_off: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        jz_call_overdue: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        jz_call_no_connected: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        jz_call_no_connnect_times: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init: function() {
      listWx().then(response => {
        this.dataForm = response.data.data
      })
    },
    cancel() {
      this.init()
    },
    update() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.doUpdate()
      })
    },
    doUpdate() {
      updateWx(this.dataForm)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '外拨配置成功，即时生效'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    }
  }
}
</script>
