<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="applyName">
        <el-input
          v-model="queryParams.applyName"
          placeholder="请输入奖项名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="applyStatus">
        <el-select v-model="queryParams.applyStatus" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.rew_apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.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"
        >申请</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <custom-table
      :columns="tableColumns"
      :tableData="noticeList"
      :loading="loading"
      :actions="tableActions"
      :query-params="queryParams"
      :apply-type="applyParam.applyType"
      @handleQuery="handleQuery"
      @resetQuery="resetQuery"
      @handleView="handleView"
      @handleAdd="handleAdd"
      @getList="getList"
      @handleSelectionChange="handleSelectionChange"
    ></custom-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或查看公告对话框 -->
    <el-dialog :title="title" :fullscreen="true" center :visible.sync="open" top="0" width="100%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="260px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="是否获得过黄炎培职业教育奖" prop="isReward">
              <el-radio-group :disabled="isReadOnly" v-model="form.isReward">
                <el-radio :label="0">是</el-radio>
                <el-radio :label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <Update
                :applyId="applyId"
                :table-data="fileData"
                :isShowButton="isShowButton"
                :isShowUploadButton="isShowUploadButton"
                :template-type="templateType"
              >
              </Update>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isView === true" :disabled="isDisabled" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改公告对话框 -->
    <el-dialog :title="title" :fullscreen="true" center :visible.sync="editOpen" top="0" width="100%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="260px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="是否获得过黄炎培职业教育奖" prop="isReward">
              <el-radio-group :disabled="isReadOnly" v-model="form.isReward">
                <el-radio :label="0">是</el-radio>
                <el-radio :label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <Update
                :applyId="applyId"
                :dialog-type="dialogType"
                :table-data="fileData"
                :isShowButton="isShowButton"
                :isShowUploadButton="isShowUploadButton"
                :template-type="templateType"
                ref="childComponent"
              >
              </Update>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isView === true" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApply,
  getDetail,
  getApplyId,
  updateApply,
  delApply, listFile, checkUploadFile
} from "@/api/reward/apply";
import CustomTable from "@/components/CustomTable/CustomTable.vue";
import Update from '@/components/AllTypeFile/update.vue'
export default {
  name: "apply",
  components: {CustomTable,Update},
  dicts: ['rew_apply_status','rew_contribution_award'],
  data() {
    return {
      templateType:'apply',
      dialogType:'edit',
      isShowUploadButton:false,
      //列数据
      tableColumns: [
        { label: '创建者', prop: 'createBy', align: 'center' },
        { label: '奖项名称', prop: 'applyName', align: 'center', showOverflowTooltip: true },
        { label: '状态', prop: 'applyStatus', align: 'center',slotName: 'customSlotName' },
        { label: '创建时间', prop: 'createTime', align: 'center' },
        { label: '提交时间', prop: 'appTime', align: 'center',type:'date' },
      ],
      //操作列
      tableActions: [
        { text: '查看', icon: 'el-icon-view', handler: this.handleView,},
        { text: '修改', icon: 'el-icon-edit', handler: this.handleUpdate, },
        { text: '删除', icon: 'el-icon-delete', handler: this.handleDelete,},
        { text: '退回意见', icon: 'el-icon-s-comment', handler: this.applyMsg,}
      ],
      //文件上传配置项
      fileData:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      isReadOnly:false,
      isShowButton:true,
      isView:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      editOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyName: undefined,
        createBy: undefined,
        applyStatus: undefined,
        applyType:undefined,
      },
      // 表单参数
      form:{},
      // 表单校验
      rules: {
        applyName: [
          { required: true, message: "奖项名称不能为空", trigger: "blur" }
        ],
        isReward: [
          { required: true, message: "请选择", trigger: "change" }
        ]
      },
      applyParam:{
        applyType:''
      },
      applyId:'',
      isDisabled:false,
    };
  },
  computed: {

  },
  created() {

  },
  mounted() {
    if(this.$route.path.includes('contributionAward') === true){
      this.$store.dispatch('apply/setApplyType', { key: 'applyType', value: 'rew_contribution_award' });
      this.applyParam.applyType = this.$store.state.apply.applyType;
      this.queryParams.applyType = this.$store.state.apply.applyType;
    }
    this.getList('mounted');
  },
  methods: {
    /** 查询公告列表 */
    getList(item) {
      this.loading = true;
      let queryObj = {
        pageNum: 1,
        pageSize: 10,
        applyStatus:undefined,
        applyType: this.applyParam.applyType
      }
      listApply(item ==='mounted' ? queryObj:this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.fileData = this.dict.type.rew_contribution_award.map(item => {
          return {
            name: item.label,
            fileCode: item.value,
            status: '',
            expanded: false,
            childrenList: []
          };
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.editOpen = false
      this.reset();
      this.fileData.forEach((item)=>{
        item.childrenList = []
      })
    },
    // 表单重置
    reset() {
      this.form = {
        // applyName: undefined,
        isReward: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 查询参数
      this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          applyName: undefined,
          createBy: undefined,
          applyStatus: undefined,
          applyType: this.applyParam.applyType
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applyId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    applyMsg(row){
      let applyMsg = row.applyMsg
      this.$alert(applyMsg, '退回意见', {
        confirmButtonText: '确定',
      });
    },
    /**查看按钮 */
    handleView(row){
      this.editOpen = true;
      this.title = "查看奖项";
      const applyId = row.applyId || this.ids
      this.isShowUploadButton = false
      this.isShowButton = false
      getDetail(applyId).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.editOpen = true;
        this.isReadOnly = true
        this.isView = false
        this.fileData.forEach((item)=>{
          item.childrenList = []
        })
        if (response.code === 200) {
          response.data.fileRelationVOList.forEach((item) => {
            let index = this.fileData.findIndex(i => i.fileCode === item.fileCode);
            if (index !== -1) {
              this.fileData[index].childrenList.push({
                name: item.fileName,
                fileName:item.realName,
                url: item.filePath,
                state:item.fileState,
                fileId:item.fileId,
                fileCode:item.fileCode,
              });
            }

          });
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd(applyType) {
      this.reset();
      this.isView=true;
      this.open = true;
      this.isReadOnly = false
      this.isShowButton = true
      this.isShowUploadButton = true
      this.title = "添加奖项";
      // if(this.noticeList.length === 0){
        getApplyId(this.applyParam).then(response => {
          if (response.code === 200){
            this.$store.dispatch('apply/setApplyId', { key: 'applyId', value: response.data.applyId });
            this.applyId = localStorage.getItem('applyId')
            this.getList()
          }
        }).catch((err)=>{
            this.isDisabled = true
        });
      // }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isView=true;
      this.applyId = row.applyId
      this.isReadOnly = false
      this.isShowButton = true
      this.isShowUploadButton = true
      const applyId = row.applyId || this.ids
      getDetail(applyId).then(response => {
        this.form.isReward = response.data.isReward;
        this.editOpen = true;
        this.title = "修改奖项";
        this.fileData.forEach((item)=>{
          item.childrenList = []
        })
        if (response.code === 200) {
          response.data.fileRelationVOList.forEach((item) => {
            let index = this.fileData.findIndex(i => i.fileCode === item.fileCode);
            if (index !== -1) {
              this.fileData[index].childrenList.push({
                name: item.fileName,
                fileName:item.realName,
                url: item.filePath,
                state:item.fileState,
                fileId:item.fileId,
                fileCode:item.fileCode,
              });
            }

          });
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.applyId = this.applyId
          if (this.form.applyId !== '') {
            checkUploadFile(this.applyId,this.applyParam.applyType).then((res)=>{
              if (res.code === 200){
                updateApply(this.form).then(response => {
                  this.$modal.msgSuccess("保存成功");
                  this.open = false;
                  this.editOpen = false
                  this.getList();
                });
              }else{
                this.$modal.msgError(res.msg);
              }
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applyId = row.applyId || this.ids
      this.$modal.confirm('是否确认删除奖项？').then(function() {
        return delApply(applyId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
        localStorage.removeItem('applyId');
      }).catch(() => {});
    }
  }
};
</script>
<style scoped lang="scss">
::v-deep .el-table__cell{
  padding: 8px 0;
}
::v-deep .el-dialog--center .el-dialog__body{
  padding-bottom: 0;
}
::v-deep .el-dialog__footer{
  padding: 0;
}
.fullscreen-dialog {
  .el-dialog__wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background-color: rgba(0, 0, 0, 0.5); /* 遮罩层透明度 */
  }

  .el-dialog {
    width: 100%;
    height: 100%;
    margin: 0;
    border: none;
    border-radius: 0;
  }
}
</style>
