<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('父菜单')" prop="wm_parent_id">
        <el-input
          v-model="form.wm_parent_id"
          disabled
          :placeholder="__('上级菜单')"
        />
      </el-form-item>
      <el-form-item :label="__('菜单名称')" prop="wm_name">
        <el-input v-model="form.wm_name" :placeholder="__('菜单名称')" />
      </el-form-item>
      <el-form-item :label="__('动作类型')" prop="wm_type_id">
        <el-radio v-model="form.wm_type_id" :label="1">
          {{ __('链接') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="2">
          {{ __('模拟关键字') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="3">
          {{ __('扫码') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="4">
          {{ __('扫码[等待信息]') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="5">
          {{ __('系统拍照发图') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="6">
          {{ __('拍照或相册发图') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="7">
          {{ __('微信相册发图') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="8">
          {{ __('地理位置') }}
        </el-radio>
        <el-radio v-model="form.wm_type_id" :label="9">
          {{ __('微信小程序') }}
        </el-radio>
      </el-form-item>
      <el-form-item :label="__('动作操作')" prop="wm_target">
        <el-input v-model="form.wm_target" :placeholder="__('动作操作')" />
      </el-form-item>
      <el-form-item :label="__('是否启用')" prop="wm_enable">
        <el-switch
          v-model="form.wm_enable"

          :active-value="1"

          :inactive-value="0"
        />
      </el-form-item>

      <el-form-item :label="__('排序')" prop="wm_sort">
        <el-input v-model="form.wm_sort" :placeholder="__('排序')" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/wechat/menu'

  export default {
    name: 'WechatMenuEdit',
    data() {
      return {
        form: {
          wm_type_id: 1,
          wm_enable: 0,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        wm_id: [
          { required: true, trigger: 'blur', message: this.__('菜单编号') },
        ],
        wm_parent_id: [
          { required: true, trigger: 'blur', message: this.__('上级菜单') },
        ],
        wm_name: [
          { required: true, trigger: 'blur', message: this.__('菜单名称') },
        ],
        wm_type_id: [
          { required: true, trigger: 'blur', message: this.__('动作类型') },
        ],
        wm_target: [
          { required: true, trigger: 'blur', message: this.__('动作操作') },
        ],
        wm_enable: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
        store_id: [
          { required: true, trigger: 'blur', message: this.__('商城编号') },
        ],
        wm_sort: [
          { required: true, trigger: 'blur', message: this.__('排序') },
        ],
        wm_is_buildin: [
          { required: true, trigger: 'blur', message: this.__('系统内置') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,

      showEdit(row, wm_parent_id) {
        if (wm_parent_id === 0 || wm_parent_id) {
          this.title = this.__('添加')
          this.form.wm_parent_id = wm_parent_id
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
