<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="ID" prop="id"
              :rules="[
                { required: true, message:'ID不能为空', trigger:'blur' }
              ]"
            >
              <el-input v-model="formItem.id" size="mini" type="text" placeholder="ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码标题（详情）" prop="actionInfo"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.actionInfo" size="mini" type="text" placeholder="二维码标题（详情）" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码类型：QR_SCENE：临时整型，QR_STR_SCENE：临时字符串，QR_LIMIT_SCENE：永久整型，QR_LIMIT_STR_SCENE：永久字符串" prop="actionName"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.actionName" size="mini" type="text" placeholder="二维码类型：QR_SCENE：临时整型，QR_STR_SCENE：临时字符串，QR_LIMIT_SCENE：永久整型，QR_LIMIT_STR_SCENE：永久字符串" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="整型场景值ID" prop="sceneId"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.sceneId" size="mini" type="text" placeholder="整型场景值ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字符串场景值ID" prop="sceneStr"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.sceneStr" size="mini" type="text" placeholder="字符串场景值ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码ticket" prop="ticket"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.ticket" size="mini" type="text" placeholder="二维码ticket" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效时间(秒)" prop="expireSeconds"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.expireSeconds" size="mini" type="text" placeholder="有效时间(秒)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="过期时间" prop="expireDate"
              :rules="[
              ]"
            >
              <ec-date v-model="formItem.expireDate" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码地址" prop="qrcodeUrl"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.qrcodeUrl" size="mini" type="text" placeholder="二维码地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公众帐号ID" prop="jwid"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.jwid" size="mini" type="text" placeholder="公众帐号ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="触发类型：text文本/news图文" prop="msgType"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.msgType" size="mini" type="text" placeholder="触发类型：text文本/news图文" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文本内容" prop="textContent"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.textContent" size="mini" type="text" placeholder="文本内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图文选择类型（1：自定义，2：选择模板）" prop="actionNewsType"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.actionNewsType" size="mini" type="text" placeholder="图文选择类型（1：自定义，2：选择模板）" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图文标题" prop="newsTitle"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.newsTitle" size="mini" type="text" placeholder="图文标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图文摘要" prop="newsDesc"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.newsDesc" size="mini" type="text" placeholder="图文摘要" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图文封面图" prop="newsImg"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.newsImg" size="mini" type="text" placeholder="图文封面图" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图文跳转链接" prop="newsUrl"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.newsUrl" size="mini" type="text" placeholder="图文跳转链接" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图文选择模板ID" prop="newsTemplateid"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.newsTemplateid" size="mini" type="text" placeholder="图文选择模板ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签，逗号隔开" prop="tags"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.tags" size="mini" type="text" placeholder="标签，逗号隔开" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人登录名称" prop="createBy"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.createBy" size="mini" type="text" placeholder="创建人登录名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime"
              :rules="[
              ]"
            >
              <ec-date v-model="formItem.createTime" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改人登录名称" prop="updateBy"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.updateBy" size="mini" type="text" placeholder="修改人登录名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改时间" prop="updateTime"
              :rules="[
              ]"
            >
              <ec-date v-model="formItem.updateTime" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="visible = false">关闭</el-button>
        <el-button v-if="method !== 'view'" type="primary" size="mini" :loading="saveLoading" @click="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/weixinQrcodeApi'

export default {
  name: 'modules.wx.weixinQrcodeForm',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      visible: false,
      method: '',
      title: '',
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      formItem: {
        id: '',
        actionInfo: '',
        actionName: '',
        sceneId: '',
        sceneStr: '',
        ticket: '',
        expireSeconds: '',
        expireDate: '',
        qrcodeUrl: '',
        jwid: '',
        msgType: '',
        textContent: '',
        actionNewsType: '',
        newsTitle: '',
        newsDesc: '',
        newsImg: '',
        newsUrl: '',
        newsTemplateid: '',
        tags: '',
        createBy: {
          id: ''
        },
        createTime: '',
        updateBy: {
          id: ''
        },
        updateTime: ''
      }
    }
  },
  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>
