<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      :modal="false"
      top="4%"
    >
      <el-form ref="dialogForm" v-loading="loading" :model="formItem" :label-width="'120px'">
        <el-form-item
          :label="$t('ec.resource.dataRule')"
          prop="name"
          :rules="[
            {required: true, message: $t('ec.resource.dataRule') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <el-input v-model="formItem.name" type="text" placeholder="名称" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.code')"
          prop="code"
          :rules="[
            {required: true, message: $t('ec.table.code') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <el-input v-model="formItem.code" type="text" :placeholder="$t('ec.table.code')" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.dataRule.field')"
          prop="field"
        >
          <el-input v-model="formItem.field" type="text" :placeholder="$t('ec.dataRule.field')" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.dataRule.condition')"
          prop="express"
        >
          <ec-select v-model="formItem.express" dict-type="dataRuleCondition" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.dataRule.value')"
          prop="value"
        >
          <el-input v-model="formItem.value" type="text" :placeholder="$t('ec.dataRule.value')" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.dataRule.sqlSegment')"
          prop="sqlSegment"
        >
          <el-input v-model="formItem.sqlSegment" type="textarea" :rows="3" :autosize="{ minRows: 3, maxRows: 5 }" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.remarks')"
          prop="remarks"
        >
          <el-input v-model="formItem.remarks" type="textarea" :rows="3" :autosize="{ minRows: 3, maxRows: 5 }" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('ec.table.close') }}</el-button>
        <el-button v-if="method !== 'view'" type="primary" :loading="confirmLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import mixin from 'ecip-web/utils/common-mixin'
import { getById, save, update } from 'ecip-web/api/vue/resource/sysrDataruleApi'
export default {
  name: 'VueResourceDataRulesForm',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      id: '',
      menuId: '',
      method: '',
      title: '',
      visible: false,
      loading: false,
      confirmLoading: false,
      formItem: {
        name: '',
        code: '',
        field: '',
        express: '',
        value: '',
        sqlSegment: '',
        remarks: ''
      }
    }
  },
  methods: {
    init(method, id, menuId) {
      this.method = method
      this.menuId = menuId
      this.id = id
      if (method === 'add') {
        this.title = this.$t('ec.table.add')
      } else if (method === 'edit') {
        this.title = this.$t('ec.table.edit')
      } else if (method === 'view') {
        this.title = this.$t('ec.table.view')
      }
      this.visible = true
      this.loading = false
      this.$nextTick(async() => {
        // 清空初始化
        this.$refs.dialogForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          try {
            this.loading = true
            const { data } = await getById(id)
            this.formItem = data
          } finally {
            this.loading = false
          }
        }
      })
    },
    async save() {
      let validFlag
      this.$refs.dialogForm.validate(valid => { validFlag = valid })
      if (!validFlag) {
        return
      }

      try {
        this.confirmLoading = true
        this.formItem.menuId = this.menuId

        if (this.method === 'edit') {
          await update(this.id, this.formItem)
        } else {
          await save(this.formItem)
        }
        this.$message.success(this.$t('ec.message.saveSuccess'))
        this.$emit('refreshDataRule')
        this.visible = false
      } finally {
        this.confirmLoading = false
      }
    }
  }
}

</script>

<style scoped>

</style>
