<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form ref="dialogForm" v-loading="loading" :model="formItem" label-width="120px">
        <el-row  :gutter="15">
          <el-col :span="12">
            <el-form-item label="openid" prop="openid"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.openid" size="mini" type="text" placeholder="openid" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('ec.wx.scanTime')" prop="scanTime"
              :rules="[
              ]"
            >
              <ec-date v-model="formItem.scanTime" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('ec.wx.sceneId')" prop="sceneId"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.sceneId" size="mini" type="text" :placeholder="$t('ec.wx.sceneId')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('ec.wx.jwId')" prop="jwid"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.jwid" size="mini" type="text" :placeholder="$t('ec.wx.jwId')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否扫码关注 0:非扫码关注,1:扫码关注" prop="isScanSubscribe"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.isScanSubscribe" size="mini" type="text" placeholder="是否扫码关注 0:非扫码关注,1:扫码关注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="visible = false">{{ $t('ec.dialog.close') }}</el-button>
        <el-button v-if="method !== 'view'" type="primary" size="mini" :loading="saveLoading" @click="save">{{ $t('ec.dialog.save') }}</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/wx/weixinQrcodeScanRecordApi'

export default {
  name: 'modules.wx.weixinQrcodeScanRecordForm',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      visible: false,
      method: '',
      title: '',
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      formItem: {
        openid: '',
        scanTime: '',
        sceneId: '',
        jwid: '',
        isScanSubscribe: ''
      }
    }
  },
  created() {
  },
  methods: {
    init (method, id) {
      this.method = method
      if (method === 'add') {
        this.title = `新建公众号扫码记录`
      } else if (method === 'edit') {
        this.title = '修改公众号扫码记录'
      } else if (method === 'view') {
        this.title = '查看公众号扫码记录'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.dialogForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          getById(id).then(res => {
            this.formItem = res.data
            // this.inputForm = this.recover(this.inputForm, res.data)
          }).catch(e => {
            this.$messgae.error(`数据回显失败: ${e}`)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    save() {
      let validFlag
      this.$refs.dialogForm.validate(valid => { validFlag = valid })
      if (!validFlag) {
        return
      }
      this.saveLoading = true
      if (this.method === 'edit') {
        update(this.id, this.formItem).then(res => {
          this.$message.success('保存成功')
          this.$emit('refreshDataList')
          this.visible = false
        }).catch(e => {
          this.$message.error(`保存失败: ${e}`)
        }).finally(() => {
          this.saveLoading = false
        })
      } else {
        save(this.formItem).then(res => {
          this.$message.success('保存成功')
          this.$emit('refreshDataList')
          this.visible = false
        }).catch(e => {
          this.$message.error(`保存失败: ${e}`)
        }).finally(() => {
          this.saveLoading = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
