<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">

        <a-form :form="form">
          <a-form-item
            label="用户名"
            :labelCol="{
              xs: { span: 20 },
              sm: { span: 3 }
            }"
            :wrapperCol="{
              xs: { span: 20 },
              sm: { span: 10 }
            }">
            <!--            <a-input readOnly :value="userInfo.username" />-->
            <label>{{ userInfo.username }}</label>
          </a-form-item>
          <a-form-item
            label="姓名"
            :labelCol="{
              xs: { span: 20 },
              sm: { span: 3 }
            }"
            :wrapperCol="{
              xs: { span: 20 },
              sm: { span: 10 }
            }">
            <!--            <a-input readOnly :value="userInfo.realName" />-->
            <label>{{ userInfo.realName }}</label>
          </a-form-item>
          <a-form-item
            label="手机号"
            :labelCol="{
              xs: { span: 20 },
              sm: { span: 3 }
            }"
            :wrapperCol="{
              xs: { span: 20 },
              sm: { span: 10 }
            }">
            <!--            <a-input readOnly v-model="userInfo.phone"/>-->
            <label>{{ userInfo.phone }}</label>
          </a-form-item>
          <!--          <a-form-item-->
          <!--            label="公司"-->
          <!--            :labelCol="{-->
          <!--              xs: { span: 20 },-->
          <!--              sm: { span: 3 }-->
          <!--            }"-->
          <!--            :wrapperCol="{-->
          <!--              xs: { span: 20 },-->
          <!--              sm: { span: 9 }-->
          <!--            }">-->
          <!--            <a-input readOnly />-->
          <!--          </a-form-item>-->

          <a-form-item
            label="币种"
            :labelCol="{
              xs: { span: 20 },
              sm: { span: 3 }
            }"
            :wrapperCol="{
              xs: { span: 20 },
              sm: { span: 10 }
            }"
          >
            <a-select v-decorator="['moneyTypes']" placeholder="请选择" mode="multiple">
              <a-select-option
                v-for="(label,value) in moneyTypeMap"
                :key="value"
                :label="label"
                :value="value">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item
            label="自动审批"
            :labelCol="{
              xs: { span: 20 },
              sm: { span: 3 }
            }"
            :wrapperCol="{
              xs: { span: 20 },
              sm: { span: 10 }
            }"
          >
            <a-checkbox
              style="width: 20%;"
              v-decorator="[ 'enquiryAutoAudit', { valuePropName: 'checked' }]" >
            </a-checkbox>
            <a-input type="number" style="width: 80%" addon-before="延时" addon-after="小时" v-decorator="['auditDelayPeriod', {initialValue: 0}]"/>
          </a-form-item>
        </a-form>

      </a-col>
      <a-col :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)" >
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img test="test" :src="option.img"/>
        </div>
      </a-col>

    </a-row>

    <avatar-modal ref="modal" @ok="setAvatar"/>
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">
        <a-button type="primary" @click="save">保存</a-button>
      </a-col>
    </a-row>

  </div>
</template>

<script>
import AvatarModal from './modules/AvatarModal'
import { updateUserInfo } from '@/api/upms/user'
export default {
  components: {
    AvatarModal
  },
  data () {
    console.log(this.$store.getters.userInfo)
    return {
      // cropper
      preview: {},
      moneyTypeMap: {},
      form: this.$form.createForm(this),
      userInfo: this.$store.getters.userInfo,
      option: {
        img: this.BaseTool.Constant.FILE_URL + this.$store.getters.avatar,
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      }
    }
  },
  created () {
    this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
  },
  mounted () {
    const { form: { setFieldsValue } } = this
    this.$nextTick(() => {
      setFieldsValue({
        moneyTypes: this.userInfo.moneyType ? this.userInfo.moneyType.split(',') : [],
        enquiryAutoAudit: this.userInfo.enquiryAutoAudit,
        auditDelayPeriod: this.userInfo.auditDelayPeriod

      })
    })
  },
  methods: {
    setAvatar (url) {
      this.option.img = this.BaseTool.Constant.FILE_URL + url
    },
    save () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields({ force: true }, (errors, values) => {
        values.userId = this.userInfo.userId
        values.moneyType = values.moneyTypes ? values.moneyTypes.join(',') : ''
        updateUserInfo(values)
          .then(() => {
            this.confirmLoading = false
            this.$message.info('修改成功')
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    }
  }
}
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
