<template>
  <div>
    <m-search class="mb-3" :items="searchItems" v-model="searchValues" @search="onSearch"></m-search>
    <m-table
      :cardTitle="cardTitle"
      v-loading="loading"
      v-permission="['salary:claim:sharingClaim:list']"
      :items="items"
      :headers="headers"
      :page-size="pageInfo.size"
      :page-current="pageInfo.current"
      :total="total"
      @page-change="onPageChange"
    >
      <template #employeeProfitSharingRatio="{ row }">
        {{ row.employeeProfitSharingRatio * 100 + '%' }}
      </template>
      <template #actions="{ row }">
        <m-button type="primary" text @click="onClaim(row, false)" v-permission="['salary:claim:sharingClaim:claim']">分润认领</m-button>
        <m-button type="primary" text @click="onClaim(row, true)" v-permission="['salary:claim:sharingClaim:allot']">专业部门分配</m-button>
      </template>
    </m-table>

    <ClaimForm ref="claimFormRef" @submit="onSubmit" />
  </div>
</template>

<script>
import { dateFormat } from '@/utils/date'
import {
  getProfitSharingClaim,
  getCustomerProfitSharingClaimDeptAdd,
  getCustomerProfitSharingClaimAdd
} from '@/api/salary'
import ClaimForm from '../components/form.vue'
export default {
  name: 'salaryClaimSharing',
  components: {
    ClaimForm
  },
  data () {
    return {
      searchItems: [
        {
          label: '月份',
          prop: 'month',
          type: 'datePicker',
          options: {
            clearable: false,
            type: 'month',
            format: 'yyyy-MM',
            valueFormat: 'yyyy-MM',
            placeholder: '选择查询月份'
          }
        },
        {
          label: '客户编号',
          prop: 'customerId',
          type: 'input',
          options: {
            placeholder: '请输入客户编号'
          }
        },
        {
          label: '科目名称',
          prop: 'subjectName',
          type: 'input',
          options: {
            placeholder: '请输入科目名称'
          }
        }
      ],
      searchValues: {
        subjectName: null,
        customerId: null,
        month: dateFormat('YYYY-mm', new Date())
      },
      headers: [
        { label: '客户编号', prop: 'customerId', align: 'center' },
        { label: '一级科目编码/名称', prop: 'oneLevelSubject', width: 180 },
        { label: '二级科目编码/名称', prop: 'twoLevelSubject', width: 180 },
        { label: '金额', prop: 'amount' },
        { label: '机构名称', prop: 'organizationName' },
        { label: '统一认证号1', prop: 'unifiedCertificationNumber1', width: 150 },
        { label: '员工姓名1', prop: 'employeeName1', width: 120 },
        { label: '统一认证号2', prop: 'unifiedCertificationNumber2', width: 150 },
        { label: '员工姓名2', prop: 'employeeName2', width: 120 },
        { label: '管户层级标识', prop: 'customerLevelIdentifier' },
        { label: '客户类别标识', prop: 'customerCategoryIdentifier' },
        { label: '员工分润比例', prop: 'employeeProfitSharingRatio', align: 'center' },
        { label: '数据日期', prop: 'dataDate', width: 120 },
        { label: '操作', prop: 'actions', fixed: 'right', width: 180 }
      ],
      items: [],
      total: 0,
      pageInfo: {
        current: 1,
        size: 10
      },
      loading: false,
      cardTitle: null,
      isDept: false
    }
  },
  methods: {
    async onInit (cardTitle) {
      if (this.cardTitle) {
        this.cardTitle = cardTitle
      }
      this.loading = true
      try {
        const { data } = await getProfitSharingClaim({
          ...this.pageInfo,
          ...this.searchValues
        })
        this.items = data.records
        this.total = data.total
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    onClaim (item, isDept) {
      this.isDept = isDept
      this.$refs.claimFormRef.open(item, isDept)
    },
    async onSubmit (query) {
      this.$refs.claimFormRef.setLoading(true)
      const submitApi = this.isDept ? getCustomerProfitSharingClaimDeptAdd : getCustomerProfitSharingClaimAdd
      try {
        await submitApi(query)
        this.$refs.claimFormRef.close()
        this.$message.success('操作成功')
        this.onInit()
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.$refs.claimFormRef.setLoading(false)
      }
    },
    onSearch () {
      this.pageInfo.current = 1
      this.onInit()
    },
    onPageChange (index) {
      this.pageInfo.current = index
      this.onInit()
    }
  }
}
</script>

<style lang="scss" scoped>
  /* 自定义样式 */
</style>
