<template>
  <div class="app-container">
    <div class="main-right-box">
          <div class="main-search-box">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="介绍信编号" prop="letterNo">
        <el-input
          v-model="queryParams.letterNo"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访事由" prop="reasons">
        <el-input
          v-model="queryParams.reasons"
          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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">

      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
    </el-col>
    <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['core:letter:add']"
        >录入介绍信</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
          </div>
    <el-table v-loading="loading" height="650" border :data="letterList">
      <el-table-column label="介绍信编号" align="center" prop="letterNo" width="120">
      </el-table-column>
      
      <el-table-column label="接待机构" header-align="center" align="left"  prop="receptionOrgNames" />

      <el-table-column label="来访事由" header-align="center" align="left" prop="reasons"  width="300"/>
      <el-table-column label="开具日期" header-align="center" align="left" prop="startTime"  width="150"/>
      <el-table-column label="有效天数" header-align="center" align="center" prop="effectiveDays" width="100"/>
      <!-- <el-table-column label="介绍信有效期" align="center" width="250">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime}}~{{scope.row.endTime}}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" width="100" prop="status" >
        <template slot-scope="scope">
              <dict-tag :options="dict.type.letter_status" :value="scope.row.status"/>
            </template>
      </el-table-column>

      <el-table-column label="备注" header-align="center" align="left" width="200" prop="description" />
      <el-table-column label="操作" header-align="center" align="left" width="280" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            v-if="scope.row.status==0"
            @click="handleUseLetter(scope.row)"
            v-hasPermi="['core:letter:edit']"
          >下发</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetailInfo(scope.row)"
            v-hasPermi="['core:letter:edit']"
          >详情</el-button>        
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            v-if="!scope.row.haveOutInRecord && scope.row.status!=2"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['core:letter:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status!=2"
            icon="el-icon-delete"
            @click="handleDelete(scope.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-add-letter ref="dialogAddLetter" @success="handleQuery(true)"></dialog-add-letter>
    <dialog-letter-detail ref="dialogLetterDetail" @success="handleQuery(true)"></dialog-letter-detail>
  </div>
  </div>
</template>

<script>
import { listLetter, getLetter, delLetter, addLetter, updateLetter,updateLetterStatus } from "@/api/core/letter";
import dialogAddLetter from "./dialog.addletter";
import dialogLetterDetail from "./dialog.letter.detail.vue";
export default {
  components: {dialogAddLetter,dialogLetterDetail},
  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,
        letterNo:null,
        reasons: null,
        status: null,
        requestFromWeb:true,
        type:1,
        orgId:this.$store.getters.orgId,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    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();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs["dialogAddLetter"].show(null,{});
    },
    
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      this.$refs["dialogAddLetter"].show(row.id ,{});

    },
    handleUseLetter(row)
    {
      updateLetterStatus({id:row.id,status:1}).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.getList();
            });
    },
    handleDetailInfo(row)
    {
      this.$refs["dialogLetterDetail"].show(row.id);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLetter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.getList();
            });
          } else {
            addLetter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除介绍信编号为 "' + row.letterNo + '" 的数据项？').then(function() {
        return delLetter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
