<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="letterName">
        <el-input
          v-model="queryParams.letterName"
          placeholder="请输入介绍信名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.letter_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="handleAddOrUpdate(null)"
          v-hasPermi="['core:letter:add']"
        >录入介绍信</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="letterList" @selection-change="handleSelectionChange">
      <el-table-column label="介绍信名称" align="center" prop="letterName" />
      <el-table-column label="介绍信有效期" align="center" prop="startTime" width="250">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime}}~{{scope.row.endTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="接待机构" align="center" prop="orgNames" >
        <template slot-scope="r">
          <span class="text-style" :title="r.row.orgNames">{{ r.row.orgNames }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建机构" align="center" prop="orgName" />
<!--      <el-table-column label="状态" width="80" align="center" prop="status" >
        <template slot-scope="r">
          <dict-tag :options="dict.type.letter_status" :value="r.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" width="80" align="center" prop="checkStart" >
        <template slot-scope="r">
          <span>{{r.row.checkStatus==null?"待审批":r.row.checkStatus===1?"通过":"不通过"}}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="备注" align="center" prop="description" >
        <template slot-scope="r">
          <span class="text-style" :title="r.row.description">{{ r.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="r">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="LookOrAudit(r.row.id,0)"
            v-hasPermi="['core:letter:query']"
          >详情</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="LookOrAudit(r.row.id,1)"
            v-hasPermi="['core:letter:audit']"
          >审批</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="auditAllList(r.row.id)"
            v-hasPermi="['core:letter:auditAllList']"
          >审批情况</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddOrUpdate(r.row.id)"
            v-hasPermi="['core:letter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(r.row)"
            v-hasPermi="['core:letter: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"
    />
    <dialog-edit-letter ref="dialogAddLetter" @success="handleQuery(true)" @closed="getList"></dialog-edit-letter>
    <dialog-letter-view ref="dialogLetterView" @success="handleQuery(true)" @closed="getList"></dialog-letter-view>
    <dialog-audit-view ref="dialogAuditView" @success="handleQuery(true)" @closed="getList"></dialog-audit-view>
  </div>
</template>

<script>
import { listLetter, getLetter, delLetter, addLetter, updateLetter } from "@/api/core/letter";
import dialogEditLetter from "./dialog.editletter";
import dialogLetterView from "./dialog.letterview";
import dialogAuditView from "./dialog.auditview";

export default {
  components: {dialogEditLetter,dialogLetterView,dialogAuditView},
  dicts: ['letter_status'],
  name: "Letter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 介绍信表格数据
      letterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        letterName: null,
        status: null,
        orgId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.orgId = this.$store.getters.orgId;
    this.getList();
  },
  methods: {
    /** 查询介绍信列表 */
    getList() {
      this.loading = true;
      listLetter(this.queryParams).then(response => {
        this.letterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        letterName: null,
        startTime: null,
        endTime: null,
        letterFile: null,
        orgId: null,
        orgPath: null,
        orgName: null,
        checkUser: null,
        checkTime: null,
        checkStatus: null,
        status:null,
        description: null,
        createBy: null,
        createTime: 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
    },
    /** 新增按钮操作 */
    handleAddOrUpdate(id) {
      this.$refs["dialogAddLetter"].show(id,{});
    },
    LookOrAudit(id,type) {
      this.$refs["dialogLetterView"].show(id,type,{});
    },
    auditAllList(id) {
      this.$refs["dialogAuditView"].show(id,{});
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLetter(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) {
            updateLetter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLetter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该介绍信记录？').then(function() {
        return delLetter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/letter/export', {
        ...this.queryParams
      }, `letter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss">
.title {
clear: both;
margin: auto;
text-align: center;
color: black;
font-size: xx-large;
letter-spacing: 3px;
}
.text-style{
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
