<template>
  <div class="accountStatement-query-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <div style="width:100%">
        <a-form layout="inline" @keyup.enter.native="$refs.chooseTable.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="4" :sm="24">
              <a-form-item label="时间">
                <rangeDate id="accountStatement-query-rangeDate" ref="rangeDate" :value="creatDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="业务单号">
                <a-input id="accountStatement-query-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入业务单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="借贷类型">
                <v-select
                  v-model="queryParam.changeType"
                  ref="jdType"
                  id="accountStatement-query-jdType"
                  code="VERIFY_ACCOUNT_CHANGE_TYPE"
                  placeholder="请选择借贷类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="业务类型">
                <v-select
                  v-model="queryParam.bizType"
                  ref="bizType"
                  id="accountStatement-query-bizType"
                  code="VERIFY_ACCOUNT_BIZ_TYPE"
                  placeholder="请选择业务类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24" v-if="queryParam.bizType&&queryParam.bizType!='FINANCE_BOOK'">
              <a-form-item label="业务状态">
                <!-- 备货单状态，销售退货单，调拨单，费用报销单 -->
                <v-select
                  v-model="queryParam.bizStatus"
                  ref="bizStatus"
                  id="accountStatement-query-bizStatus"
                  :code="bizStatusCode"
                  :notIn="bizStatusNoCode"
                  placeholder="请选择业务状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.chooseTable.refresh(true)" id="accountStatement-query-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" id="accountStatement-query-reset">重置</a-button>
            </a-col>
            <a-col :md="24" :sm="24" style="margin-bottom: 10px;display: flex;align-items: center;justify-content: space-between;">
              <div>
                <a-button type="primary" style="margin-right: 5px" @click="handleBatchAdd" :loading="addMoreLoading">批量选择</a-button>
                <span v-if="selNums&&selNums!=0" style="margin:0 0 0 10px;">已选中{{ selNums }}项</span>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="chooseTable"
      size="small"
      :rowKey="(record) => record.verifyAccountBizSn"
      rowKeyName="verifyAccountBizSn"
      :row-selection="{ columnWidth: 40 }"
      @rowSelection="rowSelectionFun"
      :columns="columns"
      :data="loadData"
      :defaultLoadData="false"
      :scroll="{ y: 300 }"
      bordered>
      <!-- 备注 -->
      <template slot="remarks" slot-scope="text, record">
        <div @dblclick.stop :title="record.remarks">
          <a-input
            size="small"
            :disabled="record.disabled"
            v-model="record.remarks"
            style="width: 100%;"
            placeholder="请输入备注"/>
        </div>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          size="small"
          type="link"
          class="button-info"
          :loading="newLoading"
          @click="handleAdd(record)"
        >选择</a-button>
      </template>
    </s-table>
  </div>
</template>

<script>
import { verifyAccountBillQueryWaitSelectList } from '@/api/verifyAccount.js'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { toThousands } from '@/libs/tools.js'
export default {
  name: 'QueryPart',
  components: { STable, VSelect, rangeDate },
  props: {
    newLoading: Boolean,
    addMoreLoading: {
      type: Boolean,
      default: false
    },
    itemSn: {
      type: String,
      default: ''
    },
    dealerSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      buyerSn: '',
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        bizNo: '',
        changeType: undefined,
        bizType: undefined,
        bizStatus: undefined
      },
      creatDate: [], //  创建时间
      rowSelectionInfo: null,
      isChecked: false,
      repeatList: null,
      warehouseSn: null,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const params = {
          ...parameter,
          dealerSn: this.dealerSn,
          ...this.queryParam
        }
        return verifyAccountBillQueryWaitSelectList(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
              const dates = data.list[i].bizDate.split('-')
              data.list[i].year = dates[0]
              data.list[i].month = dates[1]
              data.list[i].day = dates[2].split(' ')[0]
              // 是否禁用输入框
              data.list[i].disabled = data.list[i].remarks != '' && data.list[i].bizType == 'EXPENSE'
              // 正为借方， 负数为贷方
              if (data.list[i].bizAmount > 0) {
                data.list[i].debitAmount = data.list[i].bizAmount
              } else {
                data.list[i].creditorAmount = data.list[i].bizAmount * -1
              }
            }
          }
          return data
        })
      },
      bizStatusText: [],
      codeArr: {
        'DISPATCH': 'DISPATCH_BILL_STATUS',
        'SALES_RETURN': 'SALES_RETURN_BILL_STATUS',
        'ALLOCATE': 'ALLOCATE_STATUS',
        'EXPENSE': 'EXPENSE_STATE',
        'FINANCE_BOOK': 'FINANCE_BOOK_STATE',
        'CHANGE': 'VERIFY_ACCOUNT_CHANGE_STATUS'
      }
    }
  },
  computed: {
    bizStatusCode () {
      return this.codeArr[this.queryParam.bizType]
    },
    bizStatusNoCode () {
      const arr = [
        'STOCK_UP_REJECT',
        'AUDIT_REJECT',
        'WAIT_CHECK',
        'FINANCIAL_REJECT',
        'CANCEL',
        'WAIT_RECEIVE',
        'WAIT_CUSTOMER_SERVICE_CONFIRM'
      ]
      if (this.queryParam.bizType == 'SALES_RETURN') {
        arr.push('WAIT_AUDIT')
      }
      if (this.queryParam.bizType !== 'CHANGE') {
        arr.push('WAIT_SUBMIT')
      }
      return arr
    },
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '年', dataIndex: 'year', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '月', dataIndex: 'month', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '日', dataIndex: 'day', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务单号', dataIndex: 'bizNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务类型', dataIndex: 'bizTypeDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'bizStatusDictValue', width: '8%', align: 'center', customRender: function (text, record) { return text || '--' } },
        { title: '费用/调拨类型', dataIndex: 'expenseAllocateType', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '摘要', dataIndex: 'digestInfo', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客诉索赔编码', dataIndex: 'productCode', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '借方', dataIndex: 'debitAmount', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? toThousands(text, 2) : '--') } },
        { title: '贷方', dataIndex: 'creditorAmount', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? toThousands(text, 2) : '--') } },
        { title: '备注', scopedSlots: { customRender: 'remarks' }, width: '18%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '7%', align: 'center' }
      ]
      return arr
    },
    selNums () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length || 0
    }
  },
  methods: {
    //  创建时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] : ''
      this.queryParam.endDate = date[1] ? date[1] : ''
    },
    //  重置
    resetSearchForm () {
      this.queryParam.bizNo = ''
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.bizNo = ''
      this.queryParam.changeType = undefined
      this.queryParam.bizType = undefined
      this.queryParam.bizStatus = undefined
      this.creatDate = []
      this.$refs.rangeDate.resetDate(this.creatDate)
      this.$refs.chooseTable.refresh(true)
    },
    pageInit () {
      this.$refs.chooseTable.refresh()
      // this.clearSelected()
    },
    // 添加
    handleAdd (row) {
      this.$emit('add', row)
    },
    // 清空选择
    clearSelected (row) {
      this.$refs.chooseTable.clearSelected()
    },
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 批量添加
    handleBatchAdd () {
      const row = this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys
      if (!row || row.length == 0) {
        this.$message.warning('请先选择单据！')
        return
      }
      this.$emit('bachAdd', this.rowSelectionInfo.selectedRows)
    }
  }
}
</script>
<style lang="less" scoped>
  .accountStatement-query-wrap{
    .redBg-row{
      background-color: #f5cdc8;
    }
  }
  /deep/.sTable input:disabled{
    color:gray !important;
    -webkit-text-fill-color:gray !important;
  }
</style>
