<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('选择分类')" prop="category_id">
        <el-select
          v-model="form.category_id"
          clearable
          :disabled="isEdit"
          :placeholder="__('请选择')"
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('分类别名')" prop="category_alias">
        <el-input
          v-model="form.category_alias"
          clearable
          :placeholder="__('请输入分类别名')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('选择模版')" prop="page_id">
        <el-select
          v-model="form.page_id"
          clearable
          :placeholder="__('请选择模版')"
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="item in templateOptions"
            :key="item.page_id"
            :label="item.page_name"
            :value="item.page_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('分类图片')" prop="category_image">
        <upload
          height="64px"
          :image="form.category_image"
          width="64px"
          @upImage="
            (image) => {
              form.category_image = image
            }
          "
        />
      </el-form-item>
      <el-form-item :label="__('B端分类')" prop="category_is_store">
        <el-switch
          v-model="form.category_is_store"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="category_order">
        <el-input
          v-model="form.category_order"
          clearable
          :placeholder="__('请输入排序')"
          :style="{ width: '100%' }"
        />
      </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 { getTree } from '@/api/base/product/category'
  import { doEdit } from '@/api/base/product/category/mobile'
  import { getList as getTemplates } from '@/api/page/base'
  import Upload from '@/components/upload'

  export default {
    name: 'BaseProductCategoryMobileEdit',
    components: { Upload },
    data() {
      return {
        isEdit: false,
        categoryOptions: [],
        templateOptions: [],
        form: {
          category_image: '',
          page_id: undefined,
        },
        rules: {
          category_id: [
            {
              required: true,
              message: this.__('请选择分类'),
              trigger: 'change',
            },
          ],
          category_alias: [
            {
              required: true,
              message: this.__('请输入分类别名'),
              trigger: 'blur',
            },
          ],
          page_id: [
            {
              required: true,
              message: this.__('请选择模版'),
              trigger: 'change',
            },
          ],
          category_order: [
            {
              required: true,
              message: this.__('请输入排序'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.categoryTree()
      this.getTemplateList()
    },
    methods: {
      async categoryTree() {
        const { data } = await getTree({ category_parent_id: 0 })
        this.categoryOptions = data
      },
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
          this.form.isAdd = true
        } else {
          this.isEdit = true
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
          this.form.isAdd = false
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          category_image: '',
          page_id: undefined,
        }
        this.isEdit = false
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.addCategoryName()
            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
          }
        })
      },
      addCategoryName() {
        let category_id = this.form.category_id
        if (!category_id) return
        let obj = this.categoryOptions.find((item) => item.id === category_id)
        this.form.category_name = obj.label
      },
      async getTemplateList() {
        let param = {
          pageNum: 1,
          pageSize: 9999,
          page_type: 3,
        }
        const { data } = await getTemplates(param)
        if (data) {
          this.templateOptions = data.items
        } else {
          this.templateOptions = []
        }
      },
    },
  }
</script>
