<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属用户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入所属用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:userTalent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:userTalent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:userTalent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['course:userTalent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs type="card" v-model="queryParams.isAudit" @tab-click="handleClickX">
      <el-tab-pane v-for="(item,index) in isAuditOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table border v-loading="loading" :data="userTalentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="达人id" align="center" prop="talentId" />
      <el-table-column label="所属用户" align="center" prop="userId" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="avatar" width="120">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.avatar" width="100">
            <img :src="scope.row.avatar" style="max-width: 200px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex === 1">
            <i class="el-icon-male"></i>
          </span>
          <span v-else-if="scope.row.sex === 2">
            <i class="el-icon-female"></i>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="位置" align="center" prop="address" />
      <el-table-column label="达人等级" align="center" prop="level" />
      <el-table-column label="粉丝数" align="center" prop="fans" />
      <el-table-column label="获赞数" align="center" prop="likes" />
      <el-table-column label="作品数" align="center" prop="videoNum" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isAudit==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="auditTalent(scope.row)"
            v-hasPermi="['course:userTalent:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:userTalent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:userTalent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改达人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="性别" clearable size="small">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联用户" prop="userId" >
              <el-select v-model="form.userId" remote filterable reserve-keyword placeholder="输入手机号搜索" @change="selectUser" :remote-method="userMethod" >
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.userId"
                  :value="item.userId">
                  <span style="float: left">{{ item.userId }}</span>
                  <span style="margin-left: 30px ;">{{item.nickName}}</span>
                  <span style="margin-left: 30px">{{ item.phone }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="头像" prop="avatar">
          <ImageUpload v-model="form.avatar" type="image" :num="10" :width="150" :height="150" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>


        <el-row>
          <el-col :span="8">
            <el-form-item label="获赞数" prop="likes">
              <el-input-number v-model="form.likes" :min="0"   label="获赞数"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="达人等级" prop="level">
              <el-input-number v-model="form.level" :min="1" placeholder="达人等级" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="标签" prop="tags">
          <el-select v-model="tags" multiple placeholder="请选择标签" filterable clearable size="small">
            <el-option
              v-for="dict in tagsOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="抖音链接" prop="tiktokLink">
          <el-input v-model="form.tiktokLink" type="textarea" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="快手链接" prop="kwaiLink">
          <el-input v-model="form.kwaiLink" type="textarea" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="证书编号" prop="certificateCode" >
          <el-input v-model="form.certificateCode" placeholder="请输入证书编号" maxlength="200"/>
        </el-form-item>
        <el-form-item label="专业证书" prop="certificateImages">
          <ImageUpload v-model="form.certificateImages" type="image" :num="10" :width="150" :height="150" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="auditDialog.title" :visible.sync="auditDialog.open" width="800px" append-to-body @close="auditDialogClose">
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="100px">
        <el-form-item label="审核" prop="isAudit">
        <el-radio-group v-model="auditForm.isAudit">
          <el-radio :label="item.dictValue" v-for="item in auditRadio" >{{item.dictLabel}}</el-radio>
        </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="auditForm.remark" placeholder="备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-button type="primary" @click="submitAuditForm">提交</el-button>
        <el-button @click="auditDialogClose">取消</el-button>
      </div>
    </el-dialog>
    <el-drawer
      :with-header="false"
      size="75%"
      :visible.sync="show.open">
      <userTalentDetails  ref="userTalentDetails" />
    </el-drawer>
  </div>
</template>

<script>
import {
  listUserTalent,
  getUserTalent,
  delUserTalent,
  addUserTalent,
  updateUserTalent,
  exportUserTalent,
  auditUserTalent,
} from '@/api/course/userTalent'
import { listBySearch} from "@/api/his/user";
import ImageUpload from '@/components/ImageUpload/index';
import VideoUpload from '@/components/VideoUpload/index';
import Editor from '@/components/Editor/wang';
import userTalentDetails from '../../components/course/userTalentDetails.vue';
export default {
  name: "UserTalent",
  components: {
    Editor,ImageUpload,userTalentDetails,VideoUpload
  },
  data() {
    return {
      show:{
        title:"达人详情",
        open :false
      },
      auditRadio: [{
        "dictLabel": "通过",
        "dictValue": 1
      }, {
        "dictLabel": "驳回",
        "dictValue": -1
      }],
      auditDialog:{
        title:"达人审核",
        open:false
      },
      auditForm:{},
      auditRules:{
        isAudit: [
          { required: true, message: "请选择审核状态", trigger: "blur" }
        ],
      },
      isAuditOptions:[],
      userParam:{
        phone:null
      },
      userList:[],
      tagsOptions:[],
      tags:[],
      sexOptions:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 达人表格数据
      userTalentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        nickName: null,
        avatar: null,
        title: null,
        sex: null,
        position: null,
        tags: null,
        level: null,
        fans: null,
        likes: null,
        isDel: null,
        isAudit:0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "达人昵称不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "达人性别不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "关联用户不能为空", trigger: "change" }
        ],
        avatar: [
          { required: true, message: "请上传头像", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_patient_sex").then(response => {
            this.sexOptions = response.data;
    });
    this.getDicts("sys_course_tags").then(response => {
      this.tagsOptions = response.data;
    });
    this.getDicts("sys_company_isaudit").then(response => {
      this.isAuditOptions = response.data;
    });
  },
  methods: {
    handleVideoDuration(duration) {
      this.form.videoDuration = duration;
    },
    submitAuditForm(){
      this.$refs["auditForm"].validate(valid => {
        if(valid){
          if (this.auditForm.talentId != null) {
            auditUserTalent(this.auditForm).then(response => {
              this.msgSuccess("审核成功");
              this.auditDialogClose();
              this.getList();
            });
          }
        }
      });
    },
    auditDialogClose(){
      this.auditDialog.open = false;
    },
    auditTalent(row){
      this.auditDialog.open=true;
      this.auditForm.talentId=row.talentId
    },
    handleDetails(row){
      this.show.open=true;
      setTimeout(() => {
        this.$refs.userTalentDetails.getDetails(row.talentId);
      }, 500);
    },
    handleClickX(){
      this.getList()
    },
    selectUser(){
      const user = this.userList.find(item=> item.userId === this.form.userId);
      if(user){
        this.form.phone = user.phone;
      }
    },
    userMethod(query){
      if (query !== '') {
        this.userParam.phone = query;
        listBySearch(this.userParam).then(response => {
          this.userList = response.data;
        });
      }
    },
    /** 查询达人列表 */
    getList() {
      this.loading = true;
      listUserTalent(this.queryParams).then(response => {
        this.userTalentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        talentId: null,
        userId: null,
        createTime: null,
        updateTime: null,
        nickName: null,
        avatar: null,
        title: null,
        sex: null,
        position: null,
        tags: null,
        level: null,
        fans: null,
        likes: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.talentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.userList=[];
      this.open = true;
      this.title = "添加达人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.userList=[];
      const talentId = row.talentId || this.ids
      getUserTalent(talentId).then(response => {
        this.form = response.data;
        if(response.data.tags!=null){
          this.tags = response.data.tags.split(",")
        }
        this.form.sex = response.data.sex.toString();
        this.open = true;
        this.title = "修改达人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if(this.tags.length>0){
              this.form.tags=this.tags.toString();
            }
            else{
              this.form.tags=null
            }
        if (valid) {
          if (this.form.talentId != null) {
            updateUserTalent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserTalent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const talentIds = row.talentId || this.ids;
      this.$confirm('是否确认删除达人编号为"' + talentIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserTalent(talentIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有达人数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportUserTalent(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
