<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--      <div v-if="crud.props.searchToggle">-->
      <!--        &lt;!&ndash; 搜索 &ndash;&gt;-->
      <!--        <label class="el-form-item-label">报销类型</label>-->
      <!--        <el-input v-model="query.reimburseType" clearable placeholder="报销类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />-->
      <!--        <rrOperation :crud="crud" />-->
      <!--      </div>-->
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item v-show="false" label="报销人" prop="user.id" hidden="hidden">
            <el-input v-model="form.user.id" />
          </el-form-item>
          <el-form-item label="报销人" prop="user.nickName">
            <el-input v-model="form.user.nickName" disabled />
          </el-form-item>
          <el-form-item label="手机" prop="user.phone">
            <el-input v-model="form.user.phone" disabled />
          </el-form-item>
          <el-form-item label="部门" prop="user.dept.name">
            <el-input v-model="form.user.dept.name" disabled />
          </el-form-item>
          <el-form-item label="申请事由" prop="reason">
            <el-input v-model="form.reason" style="" />
          </el-form-item>
          <el-form-item label="申请金额" prop="fee">
            <el-input v-model="form.fee" style="" />
          </el-form-item>
          <el-form-item label="报销类型" prop="reimburseType">
            <el-select v-model="form.reimburseType" filterable placeholder="请选择" style="width:185px">
              <el-option
                v-for="item in dict.purchase_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="businessFeeShow" label="业务费类型" prop="businessFeeType">
            <el-select v-model="form.businessFeeType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.business_fee_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="资金来源">
            <el-select v-model="form.feeSource" style="width:185px" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.fee_source"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="项目名称" prop="projectName" >-->
          <!--            <el-input v-model="form.projectName" style="" />-->
          <!--          </el-form-item>-->

          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" style="" />
            <!--            <project-select v-model="form.projectName" ></project-select>-->
          </el-form-item>
          <el-form-item label="项目编号">
            <el-input v-model="form.projectNo" style="" />
          </el-form-item>
          <el-form-item label="项目预算">
            <el-input v-model="form.projectFee" style="" />
          </el-form-item>
          <el-form-item label="花费产生时间" prop="reimburseTime">
            <el-date-picker v-model="form.reimburseTime" type="datetime" style="width:185px" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" style="" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="user.nickName" label="申请人" />
        <el-table-column prop="reason" label="申请事由" />
        <el-table-column prop="fee" label="申请金额" />
        <el-table-column prop="reimburseType" label="报销类型">
          <template slot-scope="scope">
            {{ dict.label.purchase_type[scope.row.reimburseType] }}
          </template>
        </el-table-column>
        <el-table-column prop="feeSource" label="资金来源">
          <template slot-scope="scope">
            {{ dict.label.fee_source[scope.row.feeSource] }}
          </template>
        </el-table-column>
        <el-table-column prop="projectName" label="项目名称" />
        <el-table-column prop="projectNo" label="项目编号" />
        <el-table-column prop="projectFee" label="项目预算" />
        <el-table-column prop="reimburseTime" label="花费产生时间" />
        <el-table-column prop="applyTime" label="申请时间" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-if="checkPer(['admin','reimburse:edit','reimburse:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudReimburse from '@/api/system/reimburse'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'

const defaultForm = { user: { dept: {}}, userId: null, reason: null, fee: null, reimburseType: null, feeSource: null, projectName: null, projectNo: null, projectFee: null, reimburseTime: null, remark: null, createBy: null, updateBy: null, createTime: null, updateTime: null, reimburseId: null }
export default {
  name: 'Reimburse',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['purchase_type', 'fee_source', 'business_fee_type'],
  cruds() {
    return CRUD({ title: '报销单', url: 'api/reimburse', idField: 'reimburseId', sort: 'reimburseId,desc', crudMethod: { ...crudReimburse }})
  },
  data() {
    return {
      businessFeeShow: false,
      permission: {
        add: ['admin', 'reimburse:add'],
        edit: ['admin', 'reimburse:edit'],
        del: ['admin', 'reimburse:del']
      },
      rules: {
        reason: [
          { required: true, message: '申请事由不能为空', trigger: 'blur' }
        ],
        fee: [
          { required: true, message: '申请金额不能为空', trigger: 'blur' }
        ],
        reimburseType: [
          { required: true, message: '报销类型不能为空', trigger: 'blur' }
        ],
        feeSource: [
          { required: true, message: '资金来源不能为空', trigger: 'blur' }
        ],
        reimburseTime: [
          { required: true, message: '花费产生时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'reimburseType', display_name: '报销类型' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  watch: {
    'form.reimburseType': {
      deep: true,
      handler(val) {
        this.businessFeeShow = val === 'business_fee'
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      this.form.user = this.user
    }
  }
}
</script>

<style scoped>

</style>
