<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item
        :label="__('规格类别名称')"
        label-width="110px"
        prop="spec_name"
      >
        <el-input
          v-model="form.spec_name"
          clearable
          :placeholder="__('请输入单行文本规格类别名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('显示类型')"
        label-width="110px"
        prop="spec_format"
      >
        <el-select
          v-model="form.spec_format"
          clearable
          :disabled="form.spec_format !== undefined"
          :placeholder="__('请选择显示类型')"
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in spec_formatOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('排序')" label-width="110px" prop="spec_order">
        <el-input
          v-model.number="form.spec_order"
          clearable
          :placeholder="__('请输入排序，数字越小越靠前')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('商品类别')"
        label-width="110px"
        prop="spec_category_id"
      >
        <tree-select
          ref="treeSelect"
          :checked-keys="defaultCheckedKeys"
          clearable
          :data="data"
          :default-props="defaultProps"
          :height="300"
          :node-key="nodeKey"
          size="small"
          :width="220"
          @change="change"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { getTree } from '@/api/base/product/category'
  import { doEdit } from '@/api/base/product/spec'
  import TreeSelect from '@/components/treeSelect'
  import { translateTitle as __ } from '@/utils/i18n'

  export default {
    name: 'SpecManagementEdit',
    components: { TreeSelect },
    data() {
      return {
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        nodeKey: 'id',
        defaultCheckedKeys: [],
        form: {
          spec_name: undefined,
          spec_order: 0,
          spec_category_id: undefined,
        },
        rules: {
          spec_name: [
            {
              required: true,
              message: this.__('请输入单行文本规格类别名称'),
              trigger: 'blur',
            },
          ],
          spec_format: [
            {
              required: true,
              message: this.__('请选择显示类型'),
              trigger: 'change',
            },
          ],
          spec_order: [
            {
              required: true,
              message: this.__('请输入数字类型'),
              trigger: 'blur',
            },
          ],
          spec_category_id: [
            {
              required: true,
              message: this.__('请选择商品类别'),
              trigger: 'blur',
            },
          ],
        },
        spec_formatOptions: [
          {
            label: 'text',
            value: 'text',
          },
          {
            label: 'image',
            value: 'image',
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.getTree()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          if (row.spec_category_id)
            this.defaultCheckedKeys.push(row.spec_category_id)
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['treeSelect'].isShowSelect = false
        this.$refs['treeSelect'].selectedData = []
        ;(this.form = {
          spec_name: undefined,
          spec_order: 0,
          spec_category_id: undefined,
        }),
          this.$refs['form'].resetFields()
        this.defaultCheckedKeys = []
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      async getTree() {
        const { data } = await getTree()
        this.data = data
      },
      change(id) {
        this.form.spec_category_id = id
      },
    },
  }
</script>
