<template>
  <a-card size="small" :bordered="false" class="urgentItemsOffsetList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="创建时间">
              <a-range-picker
                style="width:100%"
                id="urgentItemsOffsetList-creatTime"
                :disabledDate="disabledDate"
                v-model="time"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </a-form-item>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="客户名称">
              <a-select
                id="urgentItemsOffsetList-buyerName"
                placeholder="请选择"
                allowClear
                v-model="queryParam.buyerName"
                :showSearch="true"
                option-filter-prop="children"
                :filter-option="filterOption">
                <a-select-option v-for="item in custAllList" :key="item.customerSn" :value="item.customerName">{{ item.customerName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="销售单号">
              <a-input id="urgentItemsOffsetList-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="急件单号">
                <a-input id="urgentItemsOffsetList-urgentBillNo" v-model.trim="queryParam.urgentBillNo" allowClear placeholder="请输入急件单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="状态">
                <v-select
                  v-model="queryParam.status"
                  ref="status"
                  id="chainTransferInList-status"
                  code="URGENT_STATUS"
                  placeholder="请选择状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="urgentItemsOffsetList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="urgentItemsOffsetList-reset">重置</a-button>
            <a @click="advanced=!advanced" style="margin-left: 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1340, y: tableHeight }"
      bordered>
      <!-- 单号 -->
      <template slot="urgentBillNo" slot-scope="text, record">
        <span style="color: #ed1c24;cursor: pointer;" @click="handleDetail(record)">{{ record.urgentBillNo }}</span>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import { custAllList } from '@/api/customer'
import { urgentList } from '@/api/urgent'
import { STable, VSelect } from '@/components'
export default {
  components: { STable, VSelect },
  data () {
    return {
      tableHeight: 0,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: { //  查询条件
        buyerName: undefined, //  客户名称
        salesBillNo: '', //  销售单号
        urgentBillNo: '', //  急件单号
        status: undefined //  状态
      },
      disabled: false, //  查询、重置按钮是否可操作
      dateFormat: 'YYYY-MM-DD',
      time: [], //  创建时间
      custAllList: [], //  客户下拉数据
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '急件单号', scopedSlots: { customRender: 'urgentBillNo' }, width: 220, align: 'center' },
        { title: '销售单号', dataIndex: 'salesBillNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'buyerName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '总款数', dataIndex: 'totalCategory', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'totalQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center' },
        { title: '冲减时间', dataIndex: 'offSetTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'statusDictValue', width: 100, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight
        }
        // 创建时间
        if (this.time && this.time.length > 0) {
          this.queryParam.beginDate = moment(this.time[0]).format(this.dateFormat)
          this.queryParam.endDate = moment(this.time[1]).format(this.dateFormat)
        } else {
          this.queryParam.beginDate = undefined
          this.queryParam.endDate = undefined
        }
        return urgentList(Object.assign(parameter, this.queryParam)).then(res => {
          const 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
          return data
        })
      }
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    //  重置
    resetSearchForm () {
      this.queryParam.buyerName = undefined
      this.queryParam.salesBillNo = ''
      this.queryParam.urgentBillNo = ''
      this.queryParam.status = undefined
      this.time = []
      this.$refs.table.refresh(true)
    },
    //  详情
    handleDetail (row) {
      this.$router.push({ path: `/salesManagement/urgentItemsOffset/detail/${row.urgentBillSn}` })
    },
    // 客户无分页列表数据
    getCustAllList () {
      const _this = this
      custAllList().then(res => {
        if (res.status == 200) {
          _this.custAllList = res.data || []
        } else {
          _this.custAllList = []
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getCustAllList()
    })
  }
}
</script>

<style lang="less">
  .urgentItemsOffsetList-wrap{
    .sTable{
      margin-top: 20px;
    }
    .red{
      color: #ed1c24;
    }
    .green{
      color: #19be6b;
    }
  }
</style>
