<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item :label="__('模板名称')" prop="transport_type_name">
        <el-input
          v-model="form.transport_type_name"
          :placeholder="__('模板名称')"
        />
      </el-form-item>

      <el-form-item
        :label="__('计费规则')"
        prop="transport_type_pricing_method"
      >
        <el-radio-group v-model="form.transport_type_pricing_method">
          <el-radio :label="1">{{ __('按件数') }}</el-radio>
          <el-radio :label="2">{{ __('按重量') }}</el-radio>
          <el-radio :label="3">{{ __('按体积') }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        :label="__('免运费额度')"
        prop="transport_type_freight_free"
      >
        <el-input
          v-model="form.transport_type_freight_free"
          :placeholder="__('免运费额度')"
        />
      </el-form-item>

      <el-switch
        v-model="form.transport_type_free"

        :active-text="__('全免')"
        :active-value="1"

        :inactive-text="__('不全免')"
        :inactive-value="0"
        style="display: block"
      />
    </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/type'

  export default {
    name: 'StoreTransportTypeEdit',
    data() {
      return {
        form: {
          transport_type_free: 0,
          transport_type_pricing_method: 2,
        },
        rules: {
          transport_type_id: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('物流及售卖区域编号'),
            },
          ],
          transport_type_name: [
            { required: true, trigger: 'blur', message: this.__('模板名称') },
          ],
          store_id: [
            { required: true, trigger: 'blur', message: this.__('所属店铺') },
          ],
          chain_id: [
            { required: true, trigger: 'blur', message: this.__('門店编号') },
          ],
          transport_type_pricing_method: [
            { required: true, trigger: 'blur', message: this.__('计费规则') },
          ],
          transport_type_time: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('最后编辑时间'),
            },
          ],
          transport_type_freight_free: [
            { required: true, trigger: 'blur', message: this.__('免运费额度') },
          ],
          transport_type_buildin: [
            { required: true, trigger: 'blur', message: this.__('系统内置') },
          ],
          transport_type_free: [
            { required: true, trigger: 'blur', message: this.__('全免运费') },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          const obj = Object.assign({}, row)
          this.form = {
            transport_type_id: obj.transport_type_id,
            transport_type_name: obj.transport_type_name,
            transport_type_pricing_method: obj.transport_type_pricing_method,
            transport_type_freight_free: obj.transport_type_freight_free,
            transport_type_free: obj.transport_type_free,
          }
        }
        this.dialogFormVisible = true
      },

      close() {
        this.form = {
          transport_type_free: 0,
          transport_type_pricing_method: 2,
        }
        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>
