<template>
  <el-dialog
    :title="!dataForm.id ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="">
    <el-row>
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="110px">
          <el-row>
            <el-col :span="10">
              <el-form-item label="推广人" prop="tgUserName" :rules="{required: true, message: '推广人必填', trigger: 'blur'}">
                <el-input v-model="dataForm.tgUserName" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="推广店铺" prop="shopId" :rules="{required: true, message: '推广店铺必填', trigger: 'blur'}">
                <el-select v-model="dataForm.shopId" placeholder="推广店铺" style="width: 300px" @change="shopChange"
                           controls-position="right" :clearable="true">
                  <el-option v-for="(item,index) in shopList" :key="index" :label="item.shopName" :value="item.shopId">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="寄拍/付费" prop="payType" :rules="{required: true, message: '付费类型必填', trigger: 'blur'}">
                <el-select v-model="dataForm.payType" placeholder="请选择" size="small" style="width: 300px">
                  <el-option :key="0" label="寄拍" value="寄拍"></el-option>
                  <el-option :key="1" label="付费 " value="付费"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5" :offset="3">
              <el-form-item label="稿费" prop="payAmt">
                <el-input v-model="dataForm.payAmt"size="small" type="number" placeholder="寄拍不填"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="结算状态" prop="payType">
                <el-select v-model="dataForm.settleStatus" placeholder="请选择" size="small">
                  <el-option :key="1" label="待结算" value="待结算"></el-option>
                  <el-option :key="2" label="已结算 " value="已结算"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col  :span="10">
              <el-form-item label="订单编号" prop="orderNumber" :rules="{required: true, message: '订单号必填', trigger: 'blur'}">
                <el-input v-model="dataForm.orderNumber" size="small" type="textarea" rows="3" placeholder="多个订单号时需要换行，一个订单号占一行"></el-input>
              </el-form-item>
            </el-col>
            <el-col  :span="10" :offset="1">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="dataForm.remark" size="small" type="textarea" rows="3"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div v-if="!dataForm.id">
            <h3>博主信息</h3>
            <el-divider></el-divider>
            <el-row v-for="(upItem, index) in dataForm.videoList" v-if="dataForm.videoList.length > 0">
              <el-col :span="5">
                <el-form-item label="博主ID" :prop="'videoList.' + index + '.upId'" :rules="{required: true, message: '博主ID必填', trigger: 'blur'}">
                  <el-input v-model="upItem.upId"size="small"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="博主昵称" :prop="'videoList.' + index + '.upName'" :rules="{required: true, message: '博主昵称必填', trigger: 'blur'}">
                  <el-input v-model="upItem.upName"size="small"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="视频平台" :prop="'videoList.' + index + '.publishPlatform'" :rules="{required: true, message: '视频平台必填', trigger: 'blur'}">
                  <el-select v-model="upItem.publishPlatform" filterable placeholder="请选择">
                    <el-option
                      v-for="(item,index) in videoPlatformList"
                      :key="index"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="粉丝数" :prop="'videoList.' + index + '.fansNum'">
                  <el-input v-model="upItem.fansNum" size="small" type="number"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-button circle @click.prevent="removeUpItem(upItem)" type="danger" icon="el-icon-delete"></el-button>
                <el-button circle @click.prevent="addUpItem()" type="success" icon="el-icon-plus" v-if="index === dataForm.videoList.length - 1"></el-button>
              </el-col>
            </el-row>
            <el-row v-if="dataForm.videoList.length === 0">
              <el-button circle @click.prevent="addUpItem()" type="success" icon="el-icon-plus"></el-button>
            </el-row>
          </div>
    </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
      <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: null,
        tgUserName: null,
        upId: null,
        upName: null,
        orderNumber: null,
        shopId: null,
        shopName: null,
        status: null,
        promotionVideoId: null,
        promotionVideo: null,
        createTime: null,
        updateTime: null,
        settleStatus: '待结算',
        remark: null,
        payType: null,
        payAmt: 0,
        videoList:[{upId:null, upName: null, publishPlatform:null, payAmt:null, fansNum:null}]
      },
      shopList: [],
      videoPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataRule: {
      },
      upList:[{}],
      videoPlatformList: [
        {value: '抖音', label: '抖音'},
        {value: '小红书', label: '小红书'},
        {value: '视频号', label: '视频号'},
        {value: '快手', label: '快手'},
        {value: 'B站', label: 'B站'},
      ],
    }
  },
  mounted () {
    this.getShopList()
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/promotion/promotionOrder/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data
          })
        }
      })
    },
    getShopList() {
      this.$http({
        url: this.$http.adornUrl('/shop/shopAuditing/list'),
        method: 'get',
      }).then(({ data }) => {
        if (data) {
          this.shopList = data
        }
      })
    },
    shopChange (index) {
      this.dataForm.shopName = this.shopList[index].shopName
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.shopName = this.shopList.find(item => item.shopId === this.dataForm.shopId).shopName
          this.$http({
            url: this.$http.adornUrl('/promotion/promotionOrder'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({data}) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
            })
            this.visible = false
            this.$emit('refreshDataList')
          })
        }
      })
    },
    addUpItem(){
      this.dataForm.videoList.push({upId:null, upName: null, publishPlatform:null, payAmt:null, fansNum:null})
    },
    removeUpItem(upItem, index){
      if(this.dataForm.videoList.length === 1){
        return
      }
      this.dataForm.videoList.splice(index, 1)
    },
  }
}
</script>
