<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="公司名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="公司名称"></el-input>
      </el-form-item>
          <el-form-item label="手机" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机"></el-input>
      </el-form-item>
          <el-form-item label="公司IP" prop="ip">
          <el-input v-model="dataForm.ip" placeholder="公司IP"></el-input>
      </el-form-item>
          <el-form-item label="公司唯一标识" prop="logo">
          <el-input v-model="dataForm.logo" placeholder="公司唯一标识"></el-input>
      </el-form-item>
          <el-form-item label="密钥" prop="token">
          <el-input v-model="dataForm.token" placeholder="密钥"></el-input>
      </el-form-item>
          <el-form-item label="公司地址" prop="address">
          <el-input v-model="dataForm.address" placeholder="公司地址"></el-input>
      </el-form-item>
          <el-form-item label="推送URL" prop="url">
          <el-input v-model="dataForm.url" placeholder="推送URL"></el-input>
      </el-form-item>
          <el-form-item label="公司邮箱" prop="email">
          <el-input v-model="dataForm.email" placeholder="公司邮箱"></el-input>
      </el-form-item>
      
      <el-form-item prop="deptName" :label="$t('user.deptName')">
        <ren-dept-tree v-model="dataForm.deptId" :placeholder="$t('dept.title')" :dept-name.sync="dataForm.deptName"></ren-dept-tree>
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" clearable 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-item label="是否去重" prop="repeatFlag">
          <el-select v-model="dataForm.repeatFlag" clearable placeholder="请选择">
	          <el-option
	                  v-for="item in repeatFlagList"
	                  :key="item.value"
	                  :label="item.label"
	                  :value="item.value">
	          </el-option>
          </el-select>
      </el-form-item>
          <el-form-item label="去重URL" prop="repeatUrl">
          <el-input v-model="dataForm.repeatUrl" placeholder="去重URL"></el-input>
      </el-form-item>
          <el-form-item label="去重密钥" prop="repeatKey">
          <el-input v-model="dataForm.repeatKey" placeholder="去重密钥"></el-input>
      </el-form-item>
          <el-form-item label="有效表单规则(0,1)" prop="formReturn">
          <el-input v-model="dataForm.formReturn" placeholder="有效表单规则(0,1)"></el-input>
      </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,
      statusList:[
        {
            'label':'停用',
            'value':0,
        },
        {
            'label':'正常',
            'value':1,
        }
      ],
      repeatFlagList:[
        {
            'label':'否',
            'value':0,
        },
        {
            'label':'是',
            'value':1,
        }
      ],
      dataForm: {
        id: '',
        name: '',
        mobile: '',
        ip: '',
        logo: '',
        token: '',
        address: '',
        url: '',
        email: '',
        status: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: '',
        deleteTime: '',
        deptId: '',
        deptName: '',
        repeatFlag: '',
        repeatUrl: '',
        repeatKey: '',
        formReturn: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ip: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        logo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        token: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        url: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        repeatFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.dataForm.deptId = ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/companies/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/companies/', 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>
