<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <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"
          v-hasPermi="['system:oaClaim:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:oaClaim:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:oaClaim:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="oaClaimList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" v-if="true"/>
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="出差天数" align="center" prop="tripDays" />
      <el-table-column label="报销金额" align="center" prop="cost" />
      <el-table-column label="关联项目" align="center" prop="projectName" >
        <template slot-scope="scope">
          <el-button type="text" >{{ scope.row.projectName!==null?scope.row.projectName:'未关联项目' }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="projectId">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status_===1?'success':'warning'">{{scope.row.status_===1?'完成':'进行中'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报销时间" align="center" prop="completedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="handleDetail(scope.row)"
            v-hasPermi="['system:oaClaim:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:oaClaim: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"
    />

    <!-- 添加或修改差旅费报销对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报销人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入报销人" />
        </el-form-item>
        <el-form-item label="票据文件id列表" prop="fileIds">
          <el-input v-model="form.fileIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报销缘由/备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出差天数" prop="tripDays">
          <el-input v-model="form.tripDays" placeholder="请输入出差天数" />
        </el-form-item>
        <el-form-item label="报销金额" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入报销金额" />
        </el-form-item>
        <el-form-item label="票据总数" prop="detailNumber">
          <el-input v-model="form.detailNumber" placeholder="请输入票据总数" />
        </el-form-item>
        <el-form-item label="关联项目" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入关联项目" />
        </el-form-item>
        <el-form-item label="关联流程id" prop="procInsId">
          <el-input v-model="form.procInsId" placeholder="请输入关联流程id" />
        </el-form-item>
        <el-form-item label="报销时间" prop="completedTime">
          <el-date-picker clearable
                          v-model="form.completedTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择报销时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOaClaim, getOaClaim, delOaClaim, addOaClaim, updateOaClaim } from "@/api/oa/claim";

export default {
  name: "OaClaim",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 差旅费报销表格数据
      oaClaimList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        fileIds: undefined,
        startTime: undefined,
        endTime: undefined,
        tripDays: undefined,
        cost: undefined,
        detailNumber: undefined,
        projectId: undefined,
        procInsId: undefined,
        completedTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        claimId: [
          { required: true, message: "主键id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "报销人不能为空", trigger: "blur" }
        ],
        fileIds: [
          { required: true, message: "票据文件id列表不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "报销缘由/备注不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        tripDays: [
          { required: true, message: "出差天数不能为空", trigger: "blur" }
        ],
        cost: [
          { required: true, message: "报销金额不能为空", trigger: "blur" }
        ],
        detailNumber: [
          { required: true, message: "票据总数不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "关联项目不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询差旅费报销列表 */
    getList() {
      this.loading = true;
      listOaClaim(this.queryParams).then(response => {
        this.oaClaimList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        claimId: undefined,
        userId: undefined,
        fileIds: undefined,
        remark: undefined,
        startTime: undefined,
        endTime: undefined,
        tripDays: undefined,
        cost: undefined,
        detailNumber: undefined,
        projectId: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined,
        procInsId: undefined,
        completedTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.claimId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/claim/addTripClaim')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const claimId = row.claimId || this.ids
      getOaClaim(claimId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改差旅费报销";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.claimId != null) {
            updateOaClaim(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOaClaim(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const claimIds = row.claimId || this.ids;
      this.$modal.confirm('是否确认删除差旅费报销编号为"' + claimIds + '"的数据项？').then(() => {
        this.loading = true;
        return delOaClaim(claimIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/oaClaim/export', {
        ...this.queryParams
      }, `oaClaim_${new Date().getTime()}.xlsx`)
    },

    /******************路由*******************/
    handleDetail(row){
      this.$router.push('/claim/detail/'+row.claimId);
    }
  }
};
</script>
