<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入手机号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="搜索码" prop="scanCode">
            <el-input
              v-model="queryParams.scanCode"
              placeholder="请输入搜索码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="邀请码" prop="inviteCode">
            <el-input
              v-model="queryParams.inviteCode"
              placeholder="请输入邀请码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        <el-form-item label="是否活跃" prop="isHot">
          <el-select v-model="queryParams.isHot" placeholder="是否活跃" clearable>
            <el-option
             v-for="item in isHot"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="审核状态" clearable>
          <el-option
            v-for="item in status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
       <el-form-item label="是否显示" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="是否显示" clearable>
          <el-option
            v-for="item in isShow"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </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="['img:creator:add']"
            >新增</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="['img:creator:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="creatorList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="编号" align="center" key="id" prop="id" v-if="columns[0].visible" />
          <el-table-column label="姓名" align="center" key="name" prop="name" v-if="columns[1].visible"/>
          <el-table-column label="头像地址" align="center" key="img" prop="img" v-if="columns[2].visible">
            <template slot-scope="scope">
              <el-image :src="scope.row.img" :preview-src-list="bigImgList" @click="getBigImg(scope.row)" style="width: 50px; height: 50px"/>
            </template>
          </el-table-column>
          <el-table-column label="搜索码" align="center" key="scanCode" prop="scanCode" v-if="columns[3].visible"/>
          <el-table-column label="手机号" align="center" key="phone" prop="phone" v-if="columns[4].visible" width="120" />
          <el-table-column label="简介" align="center" key="intro" prop="intro" v-if="columns[5].visible" :show-overflow-tooltip="true" />
          <el-table-column label="邀请码" align="center" key="inviteCode" prop="inviteCode" v-if="columns[6].visible" />
          <el-table-column label="三方平台账号地址" align="center" key="url" prop="url" v-if="columns[7].visible" :show-overflow-tooltip="true" width="160" />
          <el-table-column label="状态" align="center" key="status" prop="status" v-if="columns[8].visible" width="120">
            <template slot-scope="scope">
              {{scope.row.status == '0' ? '待审核' : scope.row.status == '1' ? "审核通过": scope.row.status == '2' ? "审核拒绝":''}}
            </template>
          </el-table-column>
          <el-table-column label="是否活跃" align="center" key="isHot" prop="isHot" v-if="columns[9].visible">
            <template slot-scope="scope">
              {{scope.row.isHot == '0' ? "否" : scope.row.isHot == '1' ? "是":''}}
            </template>
          </el-table-column>
          <el-table-column label="序号" align="center" key="sort" prop="sort" v-if="columns[10].visible" :show-overflow-tooltip="true" />
          <el-table-column label="是否显示" align="center" key="isShow" v-if="columns[11].visible">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isShow"
                active-value="1"
                inactive-value="0"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[12].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
           <el-table-column label="更新时间" align="center" prop="updateTime" v-if="columns[13].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
               <el-button
               v-if="scope.row.status === '0'"
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleVerify(scope.row)"
                v-hasPermi="['img:creator:verify']"
              >审核</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['img:tiktok:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['img:tiktok: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-col>
    </el-row>

    <!-- 审核艺术家对话框 -->
    <el-dialog :title="title" :visible.sync="verifyOpen" width="600px" append-to-body>
      <el-form ref="verifyForm" :model="verifyForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核理由" prop="reason">
              <el-input v-model="verifyForm.reason" placeholder="请输入审核理由" maxlength="40" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="verifyPass">审核通过</el-button>
        <el-button type="primary" @click="verifyUnPass">审核拒绝</el-button>
      </div>
    </el-dialog>

    <!-- 修改艺术家对话框 -->
    <el-dialog :title="title" :visible.sync="editOpen" width="600px" append-to-body>
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="editForm.name" placeholder="请输入姓名" maxlength="40" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="搜索码" prop="scanCode">
              <el-input v-model="editForm.scanCode" placeholder="请输入搜索码" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
           <el-form-item label="手机号" prop="phone">
              <el-input v-model="editForm.phone" placeholder="请输入手机号" maxlength="11" />
            </el-form-item> 
        </el-col>
        <el-col :span="12">
           <el-form-item label="邀请码" prop="inviteCode">
              <el-input v-model="editForm.inviteCode" placeholder="请输入邀请码" maxlength="11" />
            </el-form-item> 
        </el-col>
        </el-row>
        <el-row>
           <el-col :span="24">
            <el-form-item label="个人简介">
              <el-input v-model="editForm.intro" type="textarea" placeholder="请输入个人简介"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="20">
                <el-form-item label="三方平台账号地址" prop="url" label-width="130px">
                    <el-input v-model="editForm.url" placeholder="请输入第三方平台账号详情地址" maxlength="64" />
                </el-form-item> 
            </el-col>
           <el-col :span="15">
            <el-form-item label="头像上传" prop="img">
                <el-upload
                  ref="upload"
                  :action="upload.url"
                  :limit="1"
                  accept=".jpg, .jpeg, .png"
                  :headers="upload.headers"
                  :disabled="upload.isUploading"
                  :on-progress="handleFileUploadProgress"
                  :on-success="handleFileSuccess"
                  :file-list="fileList"
                  :auto-upload="true"
                  list-type="picture"
                  drag>
                 
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                  <span>仅允许上传jpg,jpeg,png格式图片。</span>
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelEdit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCreator, addCreator, editCreator, delCreator, getCreatorDetails, changeStatus, verifyPass, verifyUnPass  } from "@/api/img/creator";
import { getUserProfile } from "@/api/system/user";
import { getToken } from "@/utils/auth";

export default {
  name: "Creator",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      creatorList: null,
      // 弹出层标题
      title: "",
      // 是否显示修改艺术家弹出层
      editOpen: false,
      // 是否显示艺术家审核弹出层
      verifyOpen: false,
      //上传图片文件列表
      fileList:[],
      //是否活跃
      isHot:[
         { value: '0', label: '否'},
         { value: '1', label: '是'}
      ],
      //审核状态
      status:[
         { value: '0', label: '待审核'},
         { value: '1', label: '审核通过'},
         { value: '2', label: '审核拒绝'}
      ],
       //是否显示
      isShow: [
          { value: '0', label: '否'},
          { value: '1', label: '是'}
        ],
      //头像大图：
      bigImgList:[],
      // 升级艺术家表单参数
      updateForm: {},
      // 修改艺术家表单参数
      editForm: {},
      // 审核艺术家表单参数
      verifyForm: {},
       // 图片上传参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload" //http://localhost:7010/file/upload
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `编号`, visible: true },
        { key: 1, label: `姓名`, visible: true },
        { key: 2, label: `头像地址`, visible: true },
        { key: 3, label: `搜索码`, visible: true },
        { key: 4, label: `手机号`, visible: true },
        { key: 5, label: `简介`, visible: true },
        { key: 6, label: `邀请码`, visible: true },
        { key: 7, label: `第三方平台账号详情地址`, visible: true },
        { key: 8, label: `状态`, visible: true },
        { key: 9, label: `是否活跃`, visible: true },
        { key: 10, label: `序号`, visible: true },
        { key: 11, label: `是否显示`, visible: true },
        { key: 12, label: `创建时间`, visible: true },
        { key: 13, label: `更新时间`, visible: true }
      ],
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        reason: [
            { required: true, message: "审核理由不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listCreator(this.queryParams).then(response => {
          this.creatorList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.isShow === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"id为"' + row.id + '"的用户吗？').then(function() {
        let data = {
          id: row.id,
          status: row.isShow
        }
        return changeStatus(data);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.isShow = row.isShow === "0" ? "1" : "0";
      });
    },
    // 取消修改艺术家按钮
    cancelEdit() {
      this.editOpen = false;
      this.reset();
    },
    //审核表单重置
    verifyReset(){
        this.verifyForm={
            reason: undefined
        }
    },
    // 表单重置
    reset() {
      this.editForm = {
        name: undefined,
        scanCode: undefined,
        inviteCode: undefined,
        phone: undefined,
        intro: undefined,
        img: undefined,
        url: undefined
      };
      this.resetForm("editForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.editOpen = true;
      this.title = "新增艺术家";
    },
    /** 审核按钮操作 */
    handleVerify(row){
        getUserProfile().then(response => {
            this.verifyForm.adminId = response.data.userId
        });
        this.verifyReset();
        this.verifyForm.id = row.id || this.ids;
        this.verifyOpen = true;
        this.title = "艺术家审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.fileList = []
      this.reset();
      const id = row.id || this.ids;
      getCreatorDetails(id).then(response => {
        this.editForm = response.data;
        this.editOpen = true;
        this.title = "修改艺术家";
      });
    },
    //审核通过
    verifyPass:function(){
        this.$refs["verifyForm"].validate(valid =>{
        if(valid) {
           verifyPass(this.verifyForm).then(response => {
              this.$modal.msgSuccess("审核通过成功");
              this.verifyOpen = false;
              this.getList();
            });
        }
      })
    },
    //审核拒绝
    verifyUnPass:function(){
        this.$refs["verifyForm"].validate(valid =>{
        if(valid) {
           verifyUnPass(this.verifyForm).then(response => {
              this.$modal.msgSuccess("审核拒绝成功");
              this.verifyOpen = false;
              this.getList();
            });
        }
      })
    },
    /** 新增/修改艺术家 */
    submitForm:function(){
      this.$refs["editForm"].validate(valid =>{
        if(valid) {
          if (this.editForm.id != undefined) {
            editCreator(this.editForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.editOpen = false;
              this.getList();
            });
          }else{
            addCreator(this.editForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.editOpen = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + id + '"的艺术家吗？').then(function() {
        return delCreator(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.editForm.img = response.data
      this.fileList.url = response.data
      this.fileList.name = '图片上传结果'
      this.upload.open = false;
      this.upload.isUploading = false;
    }
  }
};
</script>