<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="35%"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('客户名称')" prop="ud_name">
        <el-input
          v-model="form.ud_name"
          clearable
          :placeholder="__('请输入客户名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('手机号')" prop="ud_mobile">
        <el-input
          v-model="form.ud_mobile"
          clearable
          :placeholder="__('请输入手机号')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('收货地址')">
        <Area v-model="address" :clearable="true" width="350" />
      </el-form-item>
      <el-form-item :label="__('详细地址')" prop="ud_address">
        <el-input
          v-model="form.ud_address"
          clearable
          :placeholder="__('请输入详细地址')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('邮政编码')" prop="ud_postalcode">
        <el-input
          v-model="form.ud_postalcode"
          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 Area from '@/components/VabArea/area'
  import { doEdit } from '@/api/user/delivery/address'

  export default {
    name: 'AddressEdit',
    components: { Area },
    data() {
      return {
        address: {
          province: {},
          city: {},
          district: {},
        },
        form: {},
        title: '',
        dialogFormVisible: false,
        rules: {
          ud_name: [
            {
              required: true,
              message: '请输入客户名称',
              trigger: 'blur',
            },
          ],
          ud_mobile: [
            {
              required: true,
              message: '请输入手机号',
              trigger: 'blur',
            },
          ],
          ud_address: [
            {
              required: true,
              message: '请输入详细地址',
              trigger: 'blur',
            },
          ],
          ud_postalcode: [
            {
              required: true,
              message: '请输入邮政编码',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {},
    methods: {
      __,
      showEdit(user_id) {
        this.form.user_id = user_id
        this.dialogFormVisible = true
      },
      close() {
        this.address = {
          province: {},
          city: {},
          district: {},
        }
        this.form = {}
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.getParam())
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      getParam() {
        let param = this.form
        let city = this.address.city
        let district = this.address.district
        let province = this.address.province

        if (city.code) param.ud_city_id = city.code
        if (city.name) param.ud_city = city.name

        if (district.code) param.ud_county_id = district.code
        if (district.name) param.ud_county = district.name

        if (province.code) param.ud_province_id = province.code
        if (province.name) param.ud_province = province.name
        return param
      },
    },
  }
</script>
