<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="演出" prop="performId">
        <el-select v-model="queryParams.performId" placeholder="请选择演出" style="width: 300px" clearable>
          <el-option
            v-for="dict in allPerform"
            :key="dict.id"
            :label="dict.name+'  '+dict.startTime"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="weverse账号" prop="weverseEmail">
        <el-input
          v-model="queryParams.weverseEmail"
          placeholder="请输入weverse账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNum">
        <el-input
          v-model="queryParams.phoneNum"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信号" prop="wechatNum">
        <el-input
          v-model="queryParams.wechatNum"
          placeholder="请输入微信号"
          clearable
          @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="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:apply:edit']"
        >修改
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="演出" align="center" prop="perform">
        <template slot-scope="scope">
          {{ scope.row.name }}
          <br>
          {{ scope.row.startTime }}
        </template>
      </el-table-column>
      <el-table-column label="weverse账号" align="center" prop="weverseEmail"/>
      <el-table-column label="weverse密码" align="center" prop="weversePassword"/>
      <el-table-column label="生日" align="center" prop="birthday"/>
      <el-table-column label="手机区号" align="center" prop="countryCode"/>
      <el-table-column label="手机号" align="center" prop="phoneNum"/>
      <el-table-column label="微信号" align="center" prop="wechatNum"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:apply:edit']"
          >修改
          </el-button>
          <el-button v-if="scope.row.status==='0'"
                     size="mini"
                     type="text"
                     icon="el-icon-check"
                     @click="confirm(scope.row)"
                     v-hasPermi="['system:apply:confirm']"
          >确认收款
          </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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="weverse账号" prop="weverseEmail">
          <el-input v-model="form.weverseEmail" placeholder="请输入weverse账号"/>
        </el-form-item>
        <el-form-item label="weverse密码" prop="weversePassword">
          <el-input v-model="form.weversePassword" placeholder="请输入weverse密码"/>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入生日"/>
        </el-form-item>
        <el-form-item label="手机区号" prop="countryCode">
          <el-input v-model="form.countryCode" placeholder="请输入手机区号"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="微信号" prop="wechatNum">
          <el-input v-model="form.wechatNum" placeholder="请输入微信号"/>
        </el-form-item>
        <el-form-item label="Token" prop="token">
          <el-input v-model="form.token" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.apply_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </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>
  </div>
</template>

<script>
import {listApply, getApply, addApply, updateApply, toConfirm} from "@/api/system/apply";
import {getAll} from "@/api/system/perform";

export default {
  name: "Apply",
  dicts: ['apply_status'],
  data() {
    return {
      //
      allPerform: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        performId: null,
        weverseEmail: null,
        weversePassword: null,
        birthday: null,
        countryCode: null,
        phoneNum: null,
        wechatNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        performId: [
          {required: true, message: "演出id不能为空", trigger: "change"}
        ],
        weverseEmail: [
          {required: true, message: "weverse账号不能为空", trigger: "blur"}
        ],
        weversePassword: [
          {required: true, message: "weverse密码不能为空", trigger: "blur"}
        ],
        birthday: [
          {required: true, message: "生日不能为空", trigger: "blur"}
        ],
        countryCode: [
          {required: true, message: "手机区号不能为空", trigger: "blur"}
        ],
        phoneNum: [
          {required: true, message: "手机号不能为空", trigger: "blur"}
        ],
        wechatNum: [
          {required: true, message: "微信号不能为空", trigger: "blur"}
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    getAll().then(response => {
      this.allPerform = response.data;
    });
  },
  methods: {
    /** 查询申请列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        performId: null,
        weverseEmail: null,
        weversePassword: null,
        birthday: null,
        countryCode: null,
        phoneNum: null,
        wechatNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改申请";
      });
    },
    /**
     * 确认收款
     */
    async confirm(row) {
      await toConfirm(row.id);
      this.$modal.msgSuccess("确认成功");
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  }
};
</script>
