<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <Area v-model="queryArea" :level="level" width="450" />
    <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/distribution/user'

  export default {
    name: 'ProxyEdit',
    components: { Area },
    data() {
      return {
        form: {},
        user_id: undefined,
        user_is_ca: undefined,
        queryArea: { province: {}, city: {}, district: {} },
        title: this.__('设置代理'),
        dialogFormVisible: false,
        level: '',
        fieldName: '',
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row, level, fieldName) {
        this.user_id = row.user_id
        this.user_is_ca = 1
        this.level = level
        this.fieldName = fieldName
        this.dialogFormVisible = true
      },
      close() {
        this.queryArea = {
          province: {},
          city: {},
          district: {},
        }
        this.user_id = undefined
        this.level = ''
        this.fieldName = ''
        this.dialogFormVisible = false
      },
      async save() {
        if (!this.validate()) {
          this.$baseMessage('地区不能为空', 'erroe')
          return
        }
        this.$baseConfirm(
          this.__('修改立马生效,是否继续？'),
          null,
          async () => {
            let form = {
              da_district_info:
                this.queryArea.province.name +
                '/' +
                this.queryArea.city.name +
                '/' +
                this.queryArea.district.name,
              da_district_id:
                this.queryArea.province.code +
                ',' +
                this.queryArea.city.code +
                ',' +
                this.queryArea.district.code,
              user_id: this.user_id,
              user_is_ca: 1,
              user_is_da: 1,
              user_is_pa: 1,
            }
            const { msg } = await doEdit(form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          }
        )
      },
      // 校验数据
      validate() {
        let queryArea = this.queryArea
        return (
          Object.keys(queryArea.city).length !== 0 ||
          Object.keys(queryArea.province).length !== 0 ||
          Object.keys(queryArea.district).length !== 0
        )
      },
      // 获取具体代理值
      getObj() {
        const obj = {}
        obj.user_id = this.user_id
        let fieldName = this.fieldName
        let value
        switch (fieldName) {
          case 'user_is_pa':
            value = this.queryArea.province.code
            break
          case 'user_is_ca':
            value = this.queryArea.city.code
            break
          case 'user_is_da':
            value = this.queryArea.district.code
            break
          default:
            value = ''
        }
        obj[fieldName] = value
        return obj
      },
    },
  }
</script>
