<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('关键字Pid')" prop="wk_parent_id">
        <el-input
          v-model="form.wk_parent_id"
          disabled
          :placeholder="__('关键字Pid')"
        />
      </el-form-item>
      <el-form-item :label="__('触发关键字')" prop="wk_name">
        <el-input
          v-model="form.wk_name"
          :disabled="form.wk_name != null"
          :placeholder="__('触发关键字')"
        />
      </el-form-item>
      <el-form-item disabled="" :label="__('触发类型')" prop="wk_type_id">
        <el-select v-model="form.wk_type_id" :placeholder="__('触发类型')">
          <el-option :label="__('纯文本')" value="1" />
          <el-option :label="__('图文')" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('链接')" prop="wk_url">
        <el-input v-model="form.wk_url" :placeholder="__('链接')" />
      </el-form-item>
      <el-form-item :label="__('关键词描述')" prop="wk_desc">
        <el-input v-model="form.wk_desc" :placeholder="__('关键词描述')" />
      </el-form-item>
      <el-form-item :label="__('标题[图文有效]')" prop="wk_title">
        <el-input v-model="form.wk_title" :placeholder="__('标题[图文有效]')" />
      </el-form-item>
      <el-form-item :label="__('封面[图文有效]')" prop="wk_image">
        <el-input v-model="form.wk_image" :placeholder="__('封面[图文有效]')" />
      </el-form-item>
      <el-form-item :label="__('是否启用')" prop="wk_enable">
        <el-switch
          v-model="form.wk_enable"

          :active-value="1"

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

      <el-form-item :label="__('备注')" prop="wk_remark">
        <el-input v-model="form.wk_remark" :placeholder="__('备注')" />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="wk_sort">
        <el-input
          v-model.number="form.wk_sort"
          clearable
          :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/keyword'

  export default {
    name: 'WechatKeywordEdit',
    data() {
      return {
        form: {
          wk_enable: 1,
          wk_name: '',
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        wk_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('触发关键字编号'),
          },
        ],
        wk_parent_id: [
          { required: true, trigger: 'blur', message: this.__('关键字P编号') },
        ],
        wk_name: [
          { required: true, trigger: 'blur', message: this.__('触发关键字') },
        ],
        wk_type_id: [
          { required: true, trigger: 'blur', message: this.__('触发类型') },
        ],
        wk_url: [{ required: true, trigger: 'blur', message: this.__('链接') }],
        wk_desc: [
          { required: true, trigger: 'blur', message: this.__('关键词描述') },
        ],
        wk_title: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('标题[图文有效]'),
          },
        ],
        wk_image: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('封面[图文有效]'),
          },
        ],
        wk_enable: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
        wk_is_buildin: [
          { required: true, trigger: 'blur', message: this.__('系统内置') },
        ],
        // wk_remark: [
        //   { required: true, trigger: 'blur', message: this.__('备注') },
        // ],
        store_id: [
          { required: true, trigger: 'blur', message: this.__('商城编号') },
        ],
        wk_sort: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('排序请输入数字类型'),
          },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row, wk_parent_id, wk_name) {
        if (wk_parent_id === 0 || wk_parent_id) {
          this.title = this.__('添加')
          this.form.wk_parent_id = wk_parent_id

          this.form.wk_name = wk_name

          console.log(this.form.wk_name)
        } else {
          this.title = this.__('编辑')
          const obj = Object.assign({}, row)
          this.form = {
            wk_id: obj.wk_id,
            wk_parent_id: obj.wk_parent_id,
            wk_name: obj.wk_name,
            wk_enable: obj.wk_enable,
            wk_image: obj.wk_image,
            wk_title: obj.wk_title,
            wk_desc: obj.wk_desc,
            wk_url: obj.wk_url,
            wk_type_id: obj.wk_type_id,
            wk_sort: obj.wk_sort,
            wk_remark: obj.wk_remark,
          }
        }
        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>
