<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item :label="systemName+'号'" prop="usercode">
        <el-autocomplete
            class="inline-input"
            v-model="form.usercode"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="昵称" v-if="info.nickname">
        {{ info.nickname }} 【{{ info.usercode }}】
      </el-form-item>
      <el-form-item label="性别" v-if="info.gender">
        <cai-dict-tag :options="genderList" :value="info.gender" />
      </el-form-item>
      <el-form-item label="头像" v-if="info.avatar">
        <image-avatar :src="info.avatar"/>
      </el-form-item>
      <el-form-item>
        注意：只有主播才可以上推荐置顶
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker
            v-model="form.beginTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择置顶开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择置顶结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="比重" prop="sortBy">
        <el-input  v-model="form.sortBy"  placeholder="请输入推广比重，数字越大排名越靠前" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getUserByUsercode, listUserByUserCode } from '@/api/cai/user'
import { genderList } from '@/constant/statusMap'
import { addAnchorTop, getAnchorTopByUserCode } from '@/api/cai/anchorTop'

export default {
  components: {
  },
  data () {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      genderList,
      open: false,
      title: '',
      form:{
        usercode: undefined,
        sortBy: undefined,
        beginTime: undefined,
        endTime: undefined
      },
      info:{
      },
      memberPriceList:[],
      // 表单校验
      rules: {
        usercode: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        sortBy: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        beginTime: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ]
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (usercode) {
      this.open = true;
      this.title = "新增或修改主播置顶"
      this.info = {};
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        console.log(usercode)
        if(usercode){
          getAnchorTopByUserCode(usercode).then(response => {
            if(response.data){
              this.form = response.data;
            }
          });
        }
      })
    },
    querySearch(querySearch,cb){
      listUserByUserCode(querySearch).then(res => {
        cb(res.data.map((terminal) => {
          return {
            value: terminal,
            name: terminal,
          };
        }))
      })
    },
    handleSelect(item){
      getUserByUsercode(item.value).then(res => {
        this.info = res.data
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          addAnchorTop(this.form).then(data => {
            this.$modal.msgSuccess("操作主播置顶成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
