<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="账户所属人">
          <!-- <a-input placeholder="请输入userName" v-decorator="['userName', {}]" disabled /> -->
          <a-select
            v-decorator="['userId', {}]"
            placeholder="请输入userName"
            showSearch
            optionFilterProp="children"
            :filterOption="filterOption"
            style="width:100%"
            disabled
          >
            <a-select-option :value="item.userId" v-for="(item, index) of options" :key="index">
              {{ item.realName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="账号id">
          <a-input placeholder="请输入账户id --授权" v-decorator="['accountId', {}]" disabled />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="广告主名称">
          <a-input placeholder="请输入广告主名称" v-decorator="['advertiserId_dictText', {}]" disabled />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="项目名称">
          <a-input placeholder="请输入项目名称" v-decorator="['projectId_dictText', {}]" disabled />
        </a-form-item>
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="输入账号  --登录">
          <a-input placeholder="请输入输入账号  --登录" v-decorator="['accountName', {}]" />
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="授权名称">
          <a-input placeholder="请输入授权名称" v-decorator="['authName', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="媒体类型">
          <!-- <a-input placeholder="请输入平台类型 1 头条 2快手" /> -->
          <a-select v-decorator="['mediaId', {}]" disabled>
            <a-select-option value="1">头条</a-select-option>
            <a-select-option value="2">快手</a-select-option>
            <a-select-option value="3">头条内广</a-select-option>
            <a-select-option value="4">快手内广</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="系统名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group buttonStyle="outline" v-decorator="['systemType', {}]" @change="systemTypeName=''">
            <a-radio-button value="不限">不限</a-radio-button>
            <a-radio-button value="ios">ios</a-radio-button>
            <a-radio-button value="android">android</a-radio-button>
            <a-radio-button value="4">其他</a-radio-button>
          </a-radio-group>
          <a-input
            v-model="systemTypeName"
            v-if="getFormFieldValue('systemType') == '4'"
            placeholder="请输入系统类型"
          />
        </a-form-item>
        <a-form-item label="预警金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['warningAmount', { rules: [{ required: true, message: '请输入预警金额' }] }]" /> -->
          <a-input-number
            style="width:100%"
            v-decorator="[
              'warningAmount',
              { rules: [{ required: true, message: '请输入预警金额' }], initialValue: 5000 }
            ]"
          />
        </a-form-item>
        <a-form-item label="预警比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number
            :step="0.1"
            style="width:100%"
            v-decorator="[
              'warningProportion',
              { rules: [{ required: true, message: '请输入预警比例' }], initialValue: 1.5 }
            ]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import { mapGetters } from 'vuex'
export default {
  name: 'UserAllocationModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/ctop/userAllocation/add',
        edit: '/ctop/userAllocation/edit'
      },
      options: [],
      systemTypeName: ''
    }
  },
  created() {},
  methods: {
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    ...mapGetters(['nickname', 'avatar', 'userInfo']),
    add() {
      this.edit({})
    },
    edit(record) {
      getAction('/sys/user/getAllUserListByCompany', { userId: this.userInfo().id }).then(res => {
        if (res.success) {
          this.options = res.result
        }
      })
      this.form.resetFields()
      var data = record
      this.systemTypeName = data.systemType
      if (data.systemType != '不限' && data.systemType != 'ios' && data.systemType != 'android') {
        data.systemType = '4'
      }

      this.model = Object.assign({}, data)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'userId',
            'userName',
            'accountId',
            'advertiserId',
            'advertiserId_dictText',
            'projectId',
            'projectId_dictText',
            'accountName',
            'authName',
            'mediaId',
            'warningAmount',
            'warningProportion',
            'systemType'
          )
        )
        //时间格式化
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.userName = this.options.filter(item => {
            return item.userId == values.userId
          })[0].realName
          if (values.systemType == '4') {
            formData.systemType = this.systemTypeName
          }
          //时间格式化

          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    getFormFieldValue(field) {
      return this.form.getFieldValue(field)
    }
  }
}
</script>

<style lang="less" scoped></style>
