<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline"
            v-if="checkPermission(['finance-income:add'])" @click="handleAdd">添加</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入备注" @advancedSearch="advancedSearchEvent" advanced>
            <template slot="advanced-content">
              <el-form ref="advancedForm" :model="advancedForm" label-width="90px">
                <el-form-item label="类型">
                  <el-select v-model="advancedForm.source_type" clearable>
                    <el-option v-for="item in financeTypeList" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="流水编号">
                  <el-input v-model="advancedForm.sn" clearable />
                </el-form-item>
                <el-form-item label="时间">
                  <el-date-picker v-model="advancedForm.create_time" type="daterange" align="center" :default-time="['00:00:00', '23:59:59']"
                                  :editable="false" unlink-panels range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-form>
            </template>
          </en-table-search>
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column prop="sn" label="流水编号" />
        <el-table-column label="类型">
          <template slot-scope="scope">
            {{ sourceTypeText(scope.row.source_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="金额">
          <template slot-scope="scope">{{ '￥' + scope.row.amount }}</template>
        </el-table-column>
        <el-table-column prop="source_sn" label="备注" />
        <el-table-column label="时间">
          <template slot-scope="scope">{{ scope.row.create_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="danger"
                       v-if="checkPermission(['finance-income:delete'])"
                       @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>

    <!-- 添加弹窗 -->
    <el-dialog :title="collectingAccountForm.id ? '编辑' : '添加'" :visible.sync="dialogVisible" width="500px"
      @open="handleDialogOpen" :modal-append-to-body="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="collectingAccountForm" :rules="formRules" ref="collectingAccountForm" label-width="60px">
        <el-form-item label="编号" prop="sn" v-if="collectingAccountForm.id">
          <el-input v-model="collectingAccountForm.sn" disabled ></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="source_type">
          <el-select v-model="collectingAccountForm.source_type" style="width: 100%">
            <el-option v-for="item in financeTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="collectingAccountForm.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="collectingAccountForm.source_sn" type="textarea" :autosize="{ minRows: 3, maxRows: 4 }"
                    placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCollectingAccountForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_FinanceItem from '@/api/financeItem'
import * as API_Setting from '@/api/setting'
import {RegExp} from '~/ui-utils'
export default {
  name: 'financeIncomeList',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10,
        amount_type: 'Income'
      },
      // 列表数据
      tableData: '',
      // 添加/编辑表单
      collectingAccountForm: {},
      advancedForm: {

      },
      // 添加|编辑 表单规则
      formRules: {
        source_type: [
          this.MixinRequired('类型必填！')
        ],
        amount: [
          this.MixinRequired('金额必填！'),
          {
            validator: (rule, value, callback) => {
              if (value && !RegExp.money.test(value)) {
                callback(new Error('请输入正确的金额'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        source_sn: [
          this.MixinRequired('备注必填！')
        ]
      },
      // 是否显示 添加|编辑 表单
      dialogVisible: false,
      financeTypeList: []
    }
  },
  watch: {
  },
  mounted() {
    this.GET_CollectingAccountList()
    this.GET_DictDataInfo()
  },
  methods: {
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      this.params.page_no = 1

      if (this.advancedForm.create_time) {
        this.advancedForm.create_time[0] = this.advancedForm.create_time[0].getTime() / 1000
        this.advancedForm.create_time[1] = this.advancedForm.create_time[1].getTime() / 1000
      }

      this.GET_CollectingAccountList()
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_CollectingAccountList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_CollectingAccountList()
    },
    sourceTypeText(val) {
      if (val) {
        return this.financeTypeList.find(item => item.value === val).label
      }
    },
    /** 添加弹窗 */
    handleAdd() {
      this.collectingAccountForm = {}
      this.dialogVisible = true
    },

    /** 编辑弹窗 */
    handleEdit(row) {
      this.collectingAccountForm = this.MixinClone(row)
      this.dialogVisible = true
    },
    /** 删除 */
    handleDelete(id) {
      this.$confirm('确定要删除吗？', '提示', { type: 'warning' }).then(() => {
        API_FinanceItem.del(id).then(() => {
          this.$message.success('删除成功！')
          this.GET_CollectingAccountList()
        })
      }).catch(() => { })
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['collectingAccountForm'].clearValidate() })
    },

    /** 提交 添加|编辑 表单 */
    submitCollectingAccountForm() {
      this.$refs['collectingAccountForm'].validate((valid) => {
        if (valid) {
          const { id } = this.collectingAccountForm
          const data = this.MixinClone(this.collectingAccountForm)
          data.amount_type = 'Income'
          if (id) {
            API_FinanceItem.edit(id, data).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_CollectingAccountList()
            })
          } else {
            API_FinanceItem.add(data).then(() => {
              this.dialogVisible = false
              this.$message.success('添加成功！')
              this.GET_CollectingAccountList()
            })
          }
        } else {
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        source_sn: data
      }
      this.params.page_no = 1
      this.GET_CollectingAccountList()
    },

    /** 查询分页列表 */
    GET_CollectingAccountList() {
      this.loading = true
      API_FinanceItem.getList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    },
    GET_DictDataInfo() {
      API_Setting.getDictDataInfo('erp_finance_income_type').then(response => {
        this.financeTypeList = response
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

.birthday{
  width: 94%;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
