<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item prop="transport_item_default_num">
            <el-input
              v-model="form.transport_item_default_num"
              :placeholder="__('默认数量')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="transport_item_default_price">
            <el-input
              v-model="form.transport_item_default_price"
              :placeholder="__('默认运费')"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item prop="transport_item_add_num">
            <el-input
              v-model="form.transport_item_add_num"
              :placeholder="__('增加数量')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="transport_item_add_price">
            <el-input
              v-model="form.transport_item_add_price"
              :placeholder="__('增加运费')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </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/store/transport/item'

  export default {
    name: 'StoreTransportItemEdit',
    data() {
      return {
        form: {
          transport_item_default_price: undefined,
          transport_item_add_num: undefined,
          transport_city_ids: undefined,
          transport_item_add_price: undefined,
        },
        rules: {
          transport_item_default_num: [
            { required: true, trigger: 'blur', message: '默认数量' },
          ],
          transport_item_default_price: [
            { required: true, trigger: 'blur', message: '默认运费' },
          ],
          transport_item_add_num: [
            { required: true, trigger: 'blur', message: '增加数量' },
          ],
          transport_item_add_price: [
            { required: true, trigger: 'blur', message: '增加运费' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        this.form.transport_type_id = row.transport_type_id
        this.title = this.__('添加')
        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>
