<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="账户" prop="advertiserId">
<!--          <el-input v-model="dataForm.advertiserId" placeholder="账户ID"></el-input>-->
              <el-select v-model="dataForm.advertiserId"  clearable placeholder="请选择账户">
                  <el-option
                          v-for="item in advertisers"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                  </el-option>
              </el-select>
      </el-form-item>
<!--        <el-form-item label="账户名称" prop="advertiserName">-->
<!--            <el-input v-model="dataForm.advertiserName" placeholder="账户名称"></el-input>-->
<!--        </el-form-item>-->
          <el-form-item label="贷款标识" prop="loanType">
<!--          <el-input v-model="dataForm.loanType" placeholder="贷款标识"></el-input>-->
              <el-select v-model="dataForm.loanType"  placeholder="请选择贷款标识">
                  <el-option
                          v-for="item in loanTypes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                  </el-option>
              </el-select>
      </el-form-item>
          <el-form-item label="落地页名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="落地页名称"></el-input>
      </el-form-item>
          <el-form-item label="页面地址" prop="url">
          <el-input v-model="dataForm.url" placeholder="页面地址"></el-input>
      </el-form-item>
          <el-form-item label="渠道" prop="channelId">
              <el-select v-model="dataForm.channelId"  clearable placeholder="请选择渠道">
                  <el-option
                          v-for="item in channels"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                  </el-option>
              </el-select>
      </el-form-item>
          <el-form-item label="运营机构" prop="operationId">
              <el-select v-model="dataForm.operationId"  clearable placeholder="请选择运营机构">
                  <el-option
                          v-for="item in operations"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                  </el-option>
              </el-select>
      </el-form-item>
          <el-form-item label="所属公司" prop="companyIds">
              <el-select v-model="dataForm.companyIds"  multiple placeholder="请选择公司">
                  <el-option
                          v-for="item in companys"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                  </el-option>
              </el-select>
<!--          <el-input v-model="dataForm.companyIds" placeholder="所属公司id，存在多个公司共用一个落地页(多个公司，分开)"></el-input>-->
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
<!--          <el-form-item label="状态（开1、关0）" prop="status">-->
<!--          <el-input v-model="dataForm.status" placeholder="状态（开1、关0）"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
        companys:[],
        operations:[],
        channels:[],
        advertisers:[],
        loanTypes:[],
      dataForm: {
        id: '',
        advertiserId: '',
        dvertiserName:'',
        loanType: '',
        name: '',
        url: '',
        channelId: '',
        operationId: '',
        companyIds: [],
        remark: '',
        status: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: '',
        deleteTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        advertiserId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        loanType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        url: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        operationId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        companyIds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        // remark: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deleteTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
        this.getCompanysOptions();
        this.getOperatorsOptions();
        this.getchannelsOptions();
        this.getAdvertisersOptions();
        this.getLoanTypeOptions();
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/demo/landingpages/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
      //初始化公司下拉框中的选项
      async getCompanysOptions() {
          this.$http.get(`/demo/companies/list`).then(({ data: res }) => {
              if (res.code !== 0) {
                  return this.$message.error(res.msg)
              }
              this.companys=[];
              if (res.data!=null){
                  res.data.forEach(element => {
                      this.companys.push({label:element.name,value:element.id})
                  })
              }
          }).catch(() => {})
      },
      //初始化贷款标识下拉框中的选项
      async getLoanTypeOptions() {
          this.$http.get(`/sys/dict/data/list?dataType=loan_type`).then(({ data: res }) => {
              if (res.code !== 0) {
                  return this.$message.error(res.msg)
              }
              this.loanTypes=[];
              if (res.data!=null){
                  res.data.forEach(element => {
                      this.loanTypes.push({label:element.dictLabel,value:element.dictValue})
                  })
              }
          }).catch(() => {})
      },
    //初始化渠道下拉框中的选项
    async getchannelsOptions() {
        this.$http.get(`/demo/channels/list`).then(({data: res}) => {
            if (res.code !== 0) {
                return this.$message.error(res.msg)
            }
            this.channels = [];
            if (res.data != null) {
                res.data.forEach(element => {
                    this.channels.push({label: element.name, value: element.id})
                })
            }
        }).catch(() => {
        })
    },
    //初始化公司下拉框中的选项
    async getOperatorsOptions() {
        this.$http.get(`/demo/operations/list`).then(({data: res}) => {
            if (res.code !== 0) {
                return this.$message.error(res.msg)
            }
            this.operations = [];
            if (res.data != null) {
                res.data.forEach(element => {
                    this.operations.push({label: element.name, value: element.id})
                })
            }
        }).catch(() => {
        })
    },
    //初始化广告主账户下拉框中的选项
    async getAdvertisersOptions() {
        this.$http.get(`/sys/rdadvertiser/list`).then(({data: res}) => {
            if (res.code !== 0) {
                return this.$message.error(res.msg)
            }
            this.advertisers = [];
            if (res.data != null) {
                res.data.forEach(element => {
                    this.advertisers.push({label: element.advertiserName, value: element.advertiserId})
                })
            }
        }).catch(() => {
        })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/demo/landingpages/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
