<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" ref="ruleForm">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="审核时间">
                <rangeDate id="expenseReimbursementDetailList-rangeDate" ref="rangeDate" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="费用单号" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                <a-input id="expenseReimbursementDetailList-expenseAccountNo" v-model.trim="queryParam.expenseAccountNo" allowClear placeholder="请输入费用单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="申请人" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                <employee style="width: 100%;" id="expenseReimbursementDetailList-applyPersonSn" @change="employeeChange" v-model="queryParam.applyPersonSn"></employee>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="申请部门" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                  <department style="width: 100%;" id="expenseReimbursementDetailList-applyDepartmentSn" @change="applyDepartmentChange" v-model="queryParam.applyDepartmentSn"></department>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="费用发生月份">
                  <a-month-picker
                    id="expenseReimbursementDetailList-month"
                    placeholder="请选择月份"
                    v-model="queryParam.expenseDate"
                    @change="onChange"
                    :disabled-date="disabledDate"
                    style="width: 100%;"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="费用类型">
                  <expenseType
                    id="expenseReimbursementDetailList-expenseType"
                    v-model="expenseTypes"
                    :changeOnSelect="true"
                    @change="expenseChange"
                    placeholder="请选择费用类型"
                  ></expenseType>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="记账类型">
                  <v-select
                    v-model="queryParam.accountType"
                    ref="accountType"
                    id="expenseReimbursementDetailList-accountType"
                    code="EXPENSE_ACCOUNT_TYPE"
                    placeholder="请选择记账类型"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="记账名称">
                  <!-- 部门 -->
                  <department id="expenseReimbursementDetailList-department" v-show="queryParam.accountType=='INNER_ACCOUNT'" @change="accountNameSnChange" v-model="queryParam.accountNameSn" placeholder="请选择记账名称(输入名称搜索)"></department>
                  <!-- 经销商 -->
                  <custList id="expenseReimbursementDetailList-custList" v-show="queryParam.accountType=='CUSTOMER'" ref="custList" @change="accountNameSnChange" placeholder="请选择记账名称(输入名称搜索)"></custList>
                  <!-- 供应商 -->
                  <supplier id="expenseReimbursementDetailList-supplier" v-show="queryParam.accountType=='OUT_ACCOUNT'" @change="accountNameSnChange" v-model="queryParam.accountNameSn" placeholder="请选择记账名称(输入名称搜索)"></supplier>
                  <!-- 其它往来 -->
                  <otherTransactions id="expenseReimbursementDetailList-other" v-show="queryParam.accountType=='OTHER_ACCOUNT'" v-model="queryParam.accountNameSn" placeholder="请选择记账名称(输入名称搜索)"></otherTransactions>
                  <a-select style="width: 100%" v-if="!queryParam.accountType" placeholder="请选择记账名称" id="expenseReimbursementDetailList-kong">
                    <a-select-option value="" :disabled="true">
                      请先选择记账类型
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属省份">
                  <Area id="expenseReimbursementDetailList-area" placeholder="请选择所属省份" v-model="queryParam.provinceSn"></Area>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属城市">
                  <Area id="expenseReimbursementDetailList-area" placeholder="请选择所属城市" leve="city" :parentId="queryParam.provinceSn" v-model="queryParam.citySn"></Area>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="费用承担方类型" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                  <v-select
                    code="EXPENSE_ACCOUNT_DETAIL_SPLIT_OBJ_TYPE"
                    id="expenseReimbursementDetailList-splitObjType"
                    allowClear
                    style="width: 100%;"
                    v-model="queryParam.splitObjType"
                    @change="splitObjTypeChange"
                    placeholder="请选择承担方类型"
                  ></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="费用承担方" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                  <!-- 部门 -->
                  <department id="expenseReimbursementDetailList-department" v-show="queryParam.splitObjType=='DEPARTMENT'" style="width: 100%;" placeholder="请选择承担方" v-model="queryParam.splitObjSn"></department>
                  <!-- 经销商 -->
                  <custList id="expenseReimbursementDetailList-custList" v-show="queryParam.splitObjType=='CUSTOMER'" ref="custList" @change="custChange" placeholder="请选择(输入名称搜索)"></custList>
                  <a-select style="width: 100%" v-if="!queryParam.splitObjType" placeholder="请选择" id="expenseReimbursementDetailList-kong1">
                    <a-select-option value="" :disabled="true">
                      请先选择承担方类型
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="所属区域">
                  <a-input id="expenseReimbursementDetailList-subarea" v-model.trim="queryParam.subareaArea.subareaName" allowClear placeholder="请输入所属区域"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="所属分区">
                  <a-input id="expenseReimbursementDetailList-subareaArea" v-model.trim="queryParam.subareaArea.subareaAreaName" allowClear placeholder="请输入所属分区"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24" v-show="queryParam.splitObjType=='DEPARTMENT'">
                <a-form-item label="费用承担人" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                  <employee id="expenseReimbursementDetailList-employee" style="width: 100%;" placeholder="请选择承担人员" v-model="queryParam.childSplitObjSn"></employee>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24" v-show="queryParam.splitObjType==undefined">
                <a-form-item label="费用承担人" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                  <a-select style="width: 100%" placeholder="请选择" id="expenseReimbursementDetailList-kong2">
                    <a-select-option value="" :disabled="true">
                      请先选择承担方类型
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="含促销销售">
                  <a-select style="width: 100%;" v-model="queryParam.hasSalesPromo" placeholder="请选择">
                    <a-select-option value="1">包含</a-select-option>
                    <a-select-option value="0">不包含</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="24" :sm="24" style="text-align: center;">
              <div class="table-page-search-submitButtons">
                <a-button id="expenseReimbursementDetailList-search" type="primary" :disabled="disabled" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button id="expenseReimbursementDetailList-reset" style="margin-left: 8px" :disabled="disabled" @click="resetSearchForm()">重置</a-button>
                <a-button
                  style="margin-left: 10px"
                  type="primary"
                  class="button-warning"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading"
                  v-if="$hasPermissions('B_expenseAcctDetailReport')"
                  id="expenseAcctDetail-export">导出</a-button>
                <a @click="advanced=!advanced" style="margin-left: 8px" id="expenseReimbursementDetailList-advanced">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="expenseReimbursementDetailList-wrap">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- alert -->
        <div>
          总单数：<strong>{{ countData&&(countData.detailNum || countData.detailNum==0) ? countData.detailNum : '--' }}</strong>；
          总费用金额：<strong>{{ countData&&(countData.expense || countData.expense==0) ? toThousands(countData.expense) : '--' }}</strong>；
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+80+'px' }"
          size="small"
          rowKeyName="no"
          :rowKey="(record) => record.no"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :scroll="{ y: tableHeight,x:2990 }"
          :defaultLoadData="false"
          bordered>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import 'moment/locale/zh-cn'
import getDate from '@/libs/getDate.js'
import rangeDate from '@/views/common/rangeDate.vue'
import subarea from '@/views/common/subarea.js'
import { STable, VSelect } from '@/components'
import Area from '@/views/common/area.js'
import department from '../expenseReimbursement/department.js'
import employee from '../expenseReimbursement/employee.js'
import custList from '@/views/common/custList.vue'
import supplier from '@/views/common/supplier.js'
import expenseType from '@/views/common/expenseType.js'
import { expenseAccountDetailList, expenseAcctDetailReport, expenseAccountDetailCount } from '@/api/expenseManagement'
import otherTransactions from '@/views/common/otherTransactions.js'
import { hdExportExcel } from '@/libs/exportExcel'
moment.locale('zh-cn')
export default {
  name: 'ExpenseReimbursementDetailList',
  mixins: [commonMixin],
  components: { STable, VSelect, department, employee, subarea, rangeDate, Area, custList, supplier, expenseType, otherTransactions },
  data () {
    const _this = this
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      openModal: false, // 选择客户弹框是否显示
      exportLoading: false, // 导出loading
      tableHeight: 0,
      time: [
        moment(getDate.getCurrMonthDays().starttime, 'YYYY-MM-DD'),
        moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
      ],
      countData: null, // 统计信息
      expenseTypes: [], // 费用类型
      // 查询参数
      queryParam: {
        expenseDate: undefined, // 月份
        beginDate: getDate.getCurrMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        expenseAccountNo: '', // 费用单号
        applyPersonSn: undefined, // 申请人
        applyDepartmentSn: undefined, // 申请部门
        expenseType: undefined, //  费用类型
        xpenseType2: undefined, //  费用类型1
        xpenseType3: undefined, //  费用类型2
        accountType: undefined, // 记账类型
        accountNameSn: undefined, // 记账名称
        splitObjType: undefined, // 费用承担方类型
        splitObjSn: undefined, //  费用承担方
        childSplitObjSn: undefined, // 费用承担人
        subareaArea: { // 区域信息
          subareaName: '',
          subareaAreaName: ''
        },
        provinceName: '', // 省
        provinceSn: undefined,
        cityName: '', // 市
        citySn: undefined,
        bizUserSn: undefined, // 客服
        hasSalesPromo: '1'
      },
      addrProvinceList: [], // 省列表
      addrCityList: [], // 城市列表数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        delete parameter.tableId
        delete parameter.index
        const params = Object.assign(parameter, this.queryParam)
        if (params.expenseDate) {
          params.expenseDate = params.expenseDate + '-01'
        }
        // 统计
        this.getDetailCount(params)
        return expenseAccountDetailList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '审核时间', dataIndex: 'finishDate', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用单号', dataIndex: 'expenseAccountNo', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', dataIndex: 'salesBillNo', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请人', dataIndex: 'applyPersonName', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请部门', dataIndex: 'applyDepartmentName', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用类型(一级)', dataIndex: 'expenseTypeName', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用类型(二级)', dataIndex: 'expenseTypeName2', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用发生月份', dataIndex: 'expenseDate', width: 90, align: 'center', customRender: function (text) { return moment(text).format('YYYY年MM月') || '--' } },
        { title: '主题', dataIndex: 'title', align: 'center', width: 150, customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '详细说明', dataIndex: 'content', align: 'center', width: 150, customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '记账类型', dataIndex: 'accountTypeDictValue', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '记账名称', dataIndex: 'accountName', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原记账名称', dataIndex: 'origAccountName', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '财务编码', dataIndex: 'accountNameFnumber', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属区域', dataIndex: 'subareaArea.subareaName', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属分区', dataIndex: 'subareaArea.subareaAreaName', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属省份', dataIndex: 'provinceName', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属城市', dataIndex: 'cityName', width: 90, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '记账费用', dataIndex: 'expense', width: 120, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '费用承担方', dataIndex: 'splitObjName', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '承担方财务编码', dataIndex: 'splitObjFnumber', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用承担金额', dataIndex: 'splitAmount', width: 120, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '费用承担人', dataIndex: 'childSplitObjName', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '人员平摊费用', dataIndex: 'childSplitAmount', width: 120, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        // { title: '费用归属品牌', dataIndex: 'productBrandName', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '费用归属品类(一级)', dataIndex: 'productTypeName1', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '费用归属品牌(二级)', dataIndex: 'productTypeName2', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '费用归属品牌(三级)', dataIndex: 'productTypeName3', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'remarks', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ]
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 费用发生月份
    onChange (date, dateString) {
      this.queryParam.expenseDate = dateString
    },
    // 费用发生月份禁用时间
    disabledDate (current) {
      return current && current > moment().endOf('day')
    },
    // 费用类型
    expenseChange (val, opts) {
      this.expenseTypes = val || []
      this.queryParam.expenseType = val && val[0] ? val[0] : ''
      this.queryParam.expenseType2 = val && val[1] ? val[1] : ''
      this.queryParam.expenseType3 = val && val[2] ? val[2] : ''
    },
    // 经销商 ，费用承担方
    custChange (val) {
      this.queryParam.splitObjSn = val.key
    },
    // 申请人选择
    employeeChange (v) {
      this.queryParam.employeeSn = v.key
    },
    // 申请部门选择
    applyDepartmentChange (v) {
      this.queryParam.applyDepartmentSn = v.key
    },
    // 记账名称变化时
    accountNameSnChange (v, row) {
      if (v) {
        this.queryParam.accountNameSn = v.key
      }
    },
    // 费用承担方类型
    splitObjTypeChange (v) {
      this.queryParam.splitObjSn = undefined
      this.queryParam.childSplitObjSn = undefined
      this.$refs.custList.dealerName = []
    },
    // 统计信息
    getDetailCount (params) {
      expenseAccountDetailCount(params).then(res => {
        this.countData = res.data || null
      })
    },
    //  导出费用报销明细统计报表
    handleExport () {
      const _this = this
      const params = JSON.parse(JSON.stringify(_this.queryParam))
      if (params.expenseDate) {
        params.expenseDate = params.expenseDate + '-01'
      }
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(expenseAcctDetailReport, params, '费用报销明细统计报表', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    // 重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam = {
        expenseDate: '', // 月份
        beginDate: getDate.getCurrMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        expenseAccountNo: '', // 费用单号
        applyPersonSn: undefined, // 申请人
        applyDepartmentSn: undefined, // 申请部门
        expenseType: undefined, //  费用类型
        expenseType2: undefined, //  费用类型2
        expenseType3: undefined, //  费用类型3
        accountType: undefined, // 记账类型
        accountNameSn: undefined, // 记账名称
        splitObjType: undefined, // 费用承担方类型
        splitObjSn: undefined, //  费用承担方
        childSplitObjSn: undefined, // 费用承担人
        subareaArea: {
          subareaName: '',
          subareaAreaName: ''
        },
        provinceName: '',
        cityName: '',
        provinceSn: undefined,
        citySn: undefined,
        bizUserSn: undefined,
        hasSalesPromo: '1'
      }
      this.expenseTypes = []
      this.$refs.table.refresh(true)
    },
    // 初始化页面
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .expenseReimbursementDetailList-wrap{
    .sTable{
      margin-top: 10px;
      .badge-con-t{
        .ant-badge-count{
          transform: scale(0.8);
          font-size: 13px;
        }
      }
    }
  }
</style>
