<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="用户id" prop="customerId">
          <el-input v-model="dataForm.customerId" placeholder="用户id"></el-input>
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId">
	      <el-select v-model="dataForm.companyId" placeholder="请选择公司">
	          <el-option
	                  v-for="item in companys"
	                  :key="item.value"
	                  :label="item.label"
	                  :value="item.value">
	          </el-option>
	      </el-select>
      </el-form-item>
      <el-form-item label="回访类型" prop="callType">
	      <el-select v-model="dataForm.callType" placeholder="回访类型">
	          <el-option
	                  v-for="item in callTypes"
	                  :key="item.value"
	                  :label="item.label"
	                  :value="item.value">
	          </el-option>
	      </el-select>
      </el-form-item>
          <el-form-item label="推送内容" prop="content">
          <el-input v-model="dataForm.content" placeholder="推送内容"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
	      <el-select v-model="dataForm.status" placeholder="状态">
	          <el-option
	                  v-for="item in statusList"
	                  :key="item.value"
	                  :label="item.label"
	                  :value="item.value">
	          </el-option>
	      </el-select>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        customerId: '',
        companyId: '',
        callType: '',
        content: '',
        remark: '',
        isPush: '',
        pushCode: '',
        pushMsg: '',
        pushDate: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: '',
        deleteTime: '',
        status: ''
      },
      companys:[],
      callTypes:[
        {
            'label':'未回访',
            'value':'0',
        },
        {
            'label':'有效表单',
            'value':'1',
        }
      ],
      statusList:[
        {
            'label':'无效',
            'value':0,
        },
        {
            'label':'有效',
            'value':1,
        }
      ],
    }
  },
  computed: {
    dataRule () {
      return {
        customerId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        companyId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        callType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.getCompanysOptions();
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/callback/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    //初始化公司下拉框中的选项
	async getCompanysOptions() {
	    this.$http.get(`/sys/companies/list`).then(({ data: res }) => {
	      if (res.code !== 0) {
	        return this.$message.error(res.msg)
	      }
	      this.companys=[];
	      if (res.data!=null){
	      	res.data.forEach(element => {
	        	this.companys.push({label:element.name,value:element.id})
	        })
	      }
	   }).catch(() => {})
	},
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/callback/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
