<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="650px"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('联系人')" prop="ss_name">
        <el-input
          v-model="form.ss_name"
          clearable
          :placeholder="__('请输入联系人')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-row>
        <el-col :span="10">
          <el-form-item :label="__('手机号码')" prop="ss_intl">
            <el-select v-model="form.ss_intl" :style="{ width: '100%' }">
              <el-option
                v-for="(item, index) in intlOptions"
                :key="index"
                :disabled="item.disabled"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label-width="0" prop="ss_mobile">
            <el-input
              v-model="form.ss_mobile"
              clearable
              :placeholder="__('请输入手机号码')"
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item :label="__('发货地址')">
        <Area
          v-model="selectArea"
          :placeholder="__('请输入发货地址')"
          width="500"
        />
      </el-form-item>
      <el-form-item :label="__('详细地址')" prop="ss_address">
        <el-input
          v-model="form.ss_address"
          clearable
          :placeholder="__('详细地址-不必重复填写地区')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('邮编')" prop="ss_postalcode">
        <el-input
          v-model="form.ss_postalcode"
          clearable
          :placeholder="__('请输入邮编')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="ss_is_default">
        <el-radio-group v-model="form.ss_is_default" size="medium">
          <el-radio
            v-for="(item, index) in ss_is_defaultOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </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 { edit } from '@/api/store/shipping/address'
  import Area from '@/components/VabArea/area'
  import { isPhone } from '@/utils/validate'

  export default {
    name: 'StoreShippingAddressEdit',
    components: { Area },
    data() {
      const validatePhone = (rule, value, callback) => {
        if (!this.form.ss_mobile || !isPhone(this.form.ss_mobile)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }
      return {
        selectArea: {
          province: {},
          city: {},
          district: {},
        },
        title: '',
        dialogFormVisible: false,
        form: {},
        rules: {
          ss_name: [
            {
              required: true,
              message: '请输入联系人',
              trigger: 'blur',
            },
          ],
          ss_mobile: [
            {
              required: true,
              message: '请输入手机号码',
              trigger: 'blur',
            },
            { validator: validatePhone, trigger: 'blur' },
          ],
          ss_intl: [
            {
              required: true,
              message: '请选择国家编码',
              trigger: 'change',
            },
          ],
          ss_address: [
            {
              required: true,
              message: '详细地址-不必重复填写地区',
              trigger: 'blur',
            },
          ],
          ss_is_default: [
            {
              required: true,
              message: '不能为空',
              trigger: 'change',
            },
          ],
        },
        ss_is_defaultOptions: [
          {
            label: this.__('默认'),
            value: 1,
          },
          {
            label: this.__('非默认'),
            value: 0,
          },
        ],
        intlOptions: [
          {
            label: this.__('简中（+86）'),
            value: '+86',
          },
          {
            label: 'U.S.A(+1)',
            value: '+1',
          },
          {
            label: 'عربي ، (+966)',
            value: '966+',
          },
          {
            label: this.__('日本語(+81)'),
            value: '+81',
          },
        ],
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.initArea(row)
          this.form = {
            ss_id: row.ss_id,
            ss_name: row.ss_name,
            ss_intl: row.ss_intl,
            ss_mobile: row.ss_mobile,
            ss_district_info: row.ss_district_info,
            ss_province_id: row.ss_province_id,
            ss_province: row.ss_province,
            ss_city_id: row.ss_city_id,
            ss_city: row.ss_city,
            ss_county_id: row.ss_county_id,
            ss_county: row.ss_county,
            ss_postalcode: row.ss_postalcode,
            ss_address: row.ss_address,
            ss_is_default: row.ss_is_default,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.selectArea = {
          province: {},
          city: {},
          district: {},
        }
        this.form = {}
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.saveArea()
            const { msg, status } = await edit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      saveArea() {
        let province = this.selectArea.province
        let city = this.selectArea.city
        let district = this.selectArea.district
        this.form.ss_province_id = province.code
        this.form.ss_province = province.name
        this.form.ss_city_id = city.code
        this.form.ss_city = city.name
        this.form.ss_county_id = district.code
        this.form.ss_county = district.name
      },
      initArea(row) {
        this.selectArea.province = {
          code: row.ss_province_id,
          name: row.ss_province,
        }
        this.selectArea.city = {
          code: row.ss_city_id,
          name: row.ss_city,
        }
        this.selectArea.district = {
          code: row.ss_county_id,
          name: row.ss_county,
        }
      },
    },
  }
</script>
