<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item label-width="0" prop="user_id" required>
        <el-select
          v-model.trim="form.user_id"
          clearable
          filterable
          :loading="loading"
          :placeholder="__('请输入用户昵称')"
          remote
          :remote-method="remoteMethod"
          reserve-keyword
        >
          <el-option
            v-for="item in accountOptions"
            :key="item.user_id"
            :label="item.user_nickname"
            :value="item.user_id"
          />
        </el-select>
      </el-form-item>
      <!--
      <el-form-item label-width="0" prop="user_is_sp">
        <el-switch
          v-model="form.user_is_sp"
          active-text="是服务商"
          inactive-text="非服务商"
          :active-value=1
          :inactive-value=0
        />
      </el-form-item>
      <el-form-item label-width="0" prop="user_is_pa">
        <Area
          v-model="pa_id_str"
          :clearable="true"
          level="province"
          :placeholder="__('是否省代理')"
          width="250"
        />
      </el-form-item>
      <el-form-item label-width="0" prop="user_is_ca">
        <Area
          v-model="da_id_str"
          :clearable="true"
          level="city"
          :placeholder="__('是否市代理')"
          width="250"
        />
      </el-form-item>
      <el-form-item label-width="0" prop="user_is_da">
        <Area
          v-model="ca_id_str"
          :clearable="true"
          level="district"
          :placeholder="__('是否区代理')"
          width="250"
        />
      </el-form-item>
      <el-form-item label-width="0" prop="user_is_pt" required>
        <el-switch
          v-model="form.user_is_pt"
          active-text="是合伙人"
          inactive-text="不是合伙人"
          :active-value=1
          :inactive-value=0
        />
      </el-form-item>
      -->
      <el-form-item label-width="0" prop="user_active" required>
        <el-switch
          v-model="form.user_active"
          :active-text="__('有效')"
          :active-value="1"
          :inactive-text="__('未生效')"
          :inactive-value="0"
        />
      </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 { getAccounts } from '@/api/user/base'
  import { add } from '@/api/distribution/user'

  export default {
    name: 'DistributionUserEdit',
    components: { Area },
    data() {
      return {
        pa_id_str: { province: {}, city: {}, district: {} },
        da_id_str: { province: {}, city: {}, district: {} },
        ca_id_str: { province: {}, city: {}, district: {} },
        accountOptions: [],
        form: {
          pa_id_str: '',
          ca_id_str: '',
          da_id_str: '',
          user_active: 1,
        },
        rules: {
          user_id: [
            {
              required: true,
              message: this.__('请输入用户昵称'),
              trigger: 'change',
            },
          ],
        },
        title: '',
        loading: true,
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        }
        this.dialogFormVisible = true
      },
      close() {
        this.clearForm()
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            //this.addProxyParam()
            const { msg, status } = await add(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      addProxyParam() {
        let pa_id_str = this.pa_id_str.province.code
        let ca_id_str =
          this.da_id_str.province.code + ',' + this.da_id_str.city.code
        let da_id_str =
          this.ca_id_str.province.code +
          ',' +
          this.ca_id_str.city.code +
          ',' +
          this.ca_id_str.district.code
        ;(this.form.pa_id_str = pa_id_str),
          (this.form.ca_id_str = ca_id_str),
          (this.form.da_id_str = da_id_str)
      },
      clearForm() {
        this.form = {
          user_is_sp: 0,
          user_is_pt: 0,
          user_active: 0,
        }
        this.user_is_pa = { province: {}, city: {}, district: {} }
        this.user_is_ca = { province: {}, city: {}, district: {} }
        this.user_is_da = { province: {}, city: {}, district: {} }
      },
    },
  }
</script>
