<template>
  <a-modal
    :title="title"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="小程序名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序名称"
            v-decorator="[
              'name',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入小程序名称' },
                  { max: 50, message: '小程序名称最多50个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="appid" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序appid"
            v-decorator="[
              'appid',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入小程序appid' },
                  { max: 50, message: '小程序appid最多50个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="小程序密钥" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序密钥"
            v-decorator="[
              'appsecret',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入小程序密钥' },
                  { max: 50, message: '小程序密钥最多100个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序标题"
            v-decorator="[
              'title',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入小程序标题' },
                  { max: 10, message: '小程序标题最多10个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="主题色" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序主题色"
            v-decorator="[
              'themecolor',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入小程序主题色' },
                  { max: 20, message: '小程序主题色最多20个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-textarea
            :rows="4"
            placeholder="请输入小程序说明"
            v-decorator="['desc', { rules: [{ max: 200, message: '说明最多200个字' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { Submit } from '@/api/modular/main/wx'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      loading: false,
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      currentInfo: {},
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    init(record) {
      this.visible = true
      this.currentInfo = record
      if (record && record.id) {
        setTimeout(() => {
          this.form.setFieldsValue({
            name: record.name,
            appid: record.appid,
            appsecret: record.appsecret,
            desc: record.desc,
            title: record.title,
            themecolor: record.themecolor
          })
        }, 100)
      }
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          Submit(values)
            .then(res => {
              if (res.code === 0) {
                if (this.currentInfo && this.currentInfo.id) {
                  this.$message.success('编辑成功')
                } else {
                  this.$message.success('添加成功')
                }
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$myerror(res.message)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
