<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="150px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('市场分类父编号')" prop="category_parent_id">
        <el-input
          v-model="form.category_parent_id"
          :placeholder="__('请输入单行文本市场分类父编号')"
          readonly
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('市场分类名称')" prop="category_name">
        <el-input
          v-model="form.category_name"
          clearable
          :placeholder="__('请输入市场分类名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('分类图片')" prop="category_image">
        <el-upload
          ref="category_image"
          :action="action"
          :before-upload="BeforeUpload"
          class="avatar-uploader"
          :on-success="(value) => handleAvatarSuccess('category_image', value)"
          :show-file-list="false"
        >
          <img
            v-if="form.category_image"
            class="avatar"
            :src="form.category_image"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item :label="__('纬度')" prop="market_latitude">
        <el-input
          v-model="form.market_latitude"
          :placeholder="__('请输入纬度')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('经度')" prop="market_longitude">
        <el-input
          v-model="form.market_longitude"
          :placeholder="__('请输入经度')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="category_order">
        <el-input
          v-model.number="form.category_order"
          :placeholder="__('请输入排序')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="category_is_enable" required>
        <el-switch
          v-model="form.category_is_enable"
          :active-text="__('显示')"
          :inactive-text="__('不显示')"
        />
      </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/base/market/category'
  import { URL } from '@/config'

  export default {
    name: 'BaseMarketCategoryEdit',
    data() {
      return {
        action: URL.action,
        form: {
          category_image: '',
          category_is_enable: true,
        },
        rules: {
          category_parent_id: [
            {
              required: true,
              message: this.__('请输入市场分类父编号'),
              trigger: 'blur',
            },
          ],
          category_name: [
            {
              required: true,
              message: this.__('请输入市场分类名称'),
              trigger: 'blur',
            },
          ],
          market_latitude: [
            {
              required: true,
              message: this.__('请输入纬度'),
              trigger: 'blur',
            },
          ],
          market_longitude: [
            {
              required: true,
              message: this.__('请输入经度'),
              trigger: 'blur',
            },
          ],
          category_order: [
            {
              required: true,
              message: this.__('请输入排序'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row, category_parent_id) {
        if (category_parent_id === 0 || category_parent_id) {
          this.title = this.__('添加')
          this.form.category_parent_id = category_parent_id
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        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
          }
        })
      },
      handleAvatarSuccess(name, res) {
        this.form[name] = res.data
      },
      BeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
    },
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
