<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="80px">
      <el-form-item label="图片" prop="imageList">
        <image-upload2 v-model="form.imageList" :limit="6"/>
      </el-form-item>
      <el-form-item  label="内容" prop="content">
        <el-input type="textarea" v-model="form.content" placeholder="内容" />
      </el-form-item>
      <el-form-item label="主播编号" prop="usercode">
        <el-autocomplete
            class="inline-input"
            v-model="form.usercode"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
        ></el-autocomplete>
      </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 {enableStatusList, versionPlatformList, yesOrNoList} from '@/constant/statusMap'
import {addDynamic} from "@/api/cai/dynamic";
import {getUserByUsercode, listUserByAnchorUserCode} from "@/api/cai/user";

export default {
  components: {
  },
  data () {
    return {
      versionPlatformList,yesOrNoList,enableStatusList,
      open: false,
      title: '',
      form:{
        imageList: undefined,
        content: undefined,
        usercode: undefined,
      },
      // 表单校验
      rules: {
        imageList: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        usercode: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = "新增动态";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          addDynamic(this.form).then(data => {
            this.$modal.msgSuccess("新增成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
    handleSelect(item){
      getUserByUsercode(item.value).then(res => {
        this.info = res.data
      })
    },
    querySearch(querySearch,cb){
      listUserByAnchorUserCode(querySearch).then(res => {
        cb(res.data.map((terminal) => {
          return {
            value: terminal,
            name: terminal,
          };
        }))
      })
    },
  }
}
</script>
