<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核时间" prop="startEndTime">
        <el-date-picker
          v-model="startEndTime"
          clearable
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="类型" prop="identityType">
        <el-select v-model="queryParams.identityType" placeholder="请选择通告主身份类型" clearable>
          <el-option
            v-for="dict in dict.type.dl_identity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="通告主" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          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="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['member:apply:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="openHis"
        >审核历史
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList">
      <el-table-column label="通告主昵称" align="center" prop="nickname"/>
      <el-table-column label="身份类型" align="center" prop="identityType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dl_identity_type" :value="scope.row.identityType"/>
        </template>
      </el-table-column>
      <el-table-column label="申请描述" align="center" prop="content"/>
      <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="['member:apply:edit']"
          >审核
          </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" label-width="80px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>申请信息</span>
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="通告主" prop="nickname">
                <el-input :disabled="true" v-model="form.nickname" placeholder="请输入申请者昵称"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份类型" prop="identityType">
                <el-select :disabled="true" v-model="form.identityType" placeholder="请选择申请认证的通告主身份类型">
                  <el-option
                    v-for="dict in dict.type.dl_identity_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="申请描述">
                <el-input :disabled="true" type="textarea" v-model="form.content" placeholder="请输入申请描述"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input :disabled="true" type="textarea" v-model="form.remark" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="附件" prop="images">
                <ImageUpload v-model="form.images" :disabled="true"></ImageUpload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>审核信息</span>
          </div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审核状态" prop="identityType">
                <el-select v-model="form.approvalStatus" placeholder="待审核">
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="审核备注" prop="approvalRemark">
                <el-input v-model="form.approvalRemark" type="textarea" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </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>
    <ApplyForm ref="formRef" />
  </div>
</template>

<script>
import {listApply, getApply, delApply, addApply, updateApply} from "@/api/member/apply";
import {formatDateDay} from "@/utils/index";
import ApplyForm from '@/views/member/apply/form/applyForm.vue';

export default {
  name: "Apply",
  dicts: ['dl_identity_type'],
  components: {
    ApplyForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      statusOptions: [{
        value: '0',
        label: '待审核'
      }, {
        value: '1',
        label: '通过'
      }, {
        value: '2',
        label: '不通过'
      }],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通告主认证申请表格数据
      applyList: [],
      //创建时间段
      startEndTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approvalStatus: 0,
        nickname: null,
        identityType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**查看历史*/
    openHis(){
      this.$refs["formRef"].initForm()
    },

    /** 查询通告主认证申请列表 */
    getList() {
      this.loading = true;
      console.log(this.startEndTime, "startEndTime")
      if (this.startEndTime.length > 0) {
        this.queryParams.startTime = formatDateDay(this.startEndTime[0])
        this.queryParams.endTime = formatDateDay(this.startEndTime[1])
      } else {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
      listApply(this.queryParams).then(response => {
        this.applyList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /**查看审核历史*/
    checkHis() {

    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        nickname: null,
        identityType: null,
        content: null,
        remark: null,
        images: null,
        approvalStatus: null,
        approvalUserId: null,
        approvalTime: null,
        approvalRemark: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.startEndTime = []
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 审核按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核";
      });
    },

    /** 提交按钮 */
    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 {
            this.$modal.msgError("出现错误");
          }
        }
      });
    },

    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('member/apply/export', {
    //     ...this.queryParams
    //   }, `apply_${new Date().getTime()}.xlsx`)
    // }
  }
};
</script>
