<template>
  <div class="matchSoo-wrap">
    <a-card style="margin-top: 5px;" size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="创建时间">
                  <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}">
                  <dealerSubareaScopeList ref="dealerSubareaScopeList" id="matchSoo-buyerName" @change="custChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="销售单号">
                  <a-input id="matchSoo-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="matchSoo-dispatchBillNo" v-model.trim="queryParam.dispatchBillNo" 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}">
                    <dealerSubareaScopeList ref="shbuyerName" id="matchSoo-shbuyerName" @change="shcustChange" />
                  </a-form-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-item label="业务状态">
                    <v-select
                      v-model="queryParam.billStatus"
                      ref="billStatus"
                      id="matchSoo-billStatus"
                      code="DISPATCH_BILL_STATUS"
                      placeholder="请选择业务状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-item label="单据状态">
                    <v-select
                      v-model="queryParam.voidFlag"
                      ref="voidFlag"
                      id="pushOrder-voidFlag"
                      code="VOID_FLAG"
                      placeholder="请选择单据状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-item label="对单状态">
                    <v-select
                      v-model="queryParam.checkStatus"
                      ref="checkStatus"
                      id="matchSoo-checkStatus"
                      code="CHECK_STATUS"
                      placeholder="请选择对单状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="对单时间">
                    <rangeDate ref="checkDate" :value="checktime" @change="checkDateChange" />
                  </a-form-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="所在区域">
                    <subarea id="matchSoo-subarea" ref="subarea" @change="subareaChange"></subarea>
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="地区" prop="shippingAddrProvinceSn">
                    <Area id="matchSoo-shippingAddrProvinceSn" v-model="queryParam.shippingAddrProvinceSn" placeholder="请选择省"></Area>
                  </a-form-model-item>
                </a-col>
              </template>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" :disabled="disabled" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" :disabled="disabled" @click="resetSearchForm()">重置</a-button>
                  <a @click="advanced=!advanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'"/>
                  </a>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y:tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 销售单号 -->
          <template slot="salesBillNo" slot-scope="text, record">
            <span class="link-bule" v-if="$hasPermissions('B_salesDetail')" @click="handleDetail(record,0)">{{ record.salesBillNo }}</span>
            <span v-else>{{ record.salesBillNo }}</span>
          </template>
          <!-- 备货单号 -->
          <template slot="dispatchBillNo" slot-scope="text, record">
            <span class="link-bule" v-if="$hasPermissions('B_dispatchDetail')" @click="handleDetail(record,1)">{{ record.dispatchBillNo }}</span>
            <span v-else>{{ record.dispatchBillNo }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              class="button-warning"
              v-if="record.checkStatus =='WAIT'&&record.voidFlag==0&&$hasPermissions('B_DDAudit')"
              @click="handleAudit(record)"
            >对单通过</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
    <!-- 查看销售单或备货单详情 -->
    <commonModal
      :modalTit="detailType?'备货单详情':'销售单详情'"
      bodyPadding="10px"
      width="70%"
      :showFooter="false"
      :openModal="showDetailModal"
      @cancel="cancelDetail">
      <salesDetail v-if="detailType==0" ref="salesDetail" :bizSn="bizSn"></salesDetail>
      <dispatchDetail v-if="detailType==1" ref="dispatchDetail" :bizSn="bizSn"></dispatchDetail>
    </commonModal>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import rangeDate from '@/views/common/rangeDate.vue'
import { STable, VSelect } from '@/components'
import subarea from '@/views/common/subarea.js'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import Area from '@/views/common/area.js'
import { dispatchlList, dispatchCheck } from '@/api/dispatch'
import commonModal from '@/views/common/commonModal.vue'
import salesDetail from '@/views/salesManagement/salesQuery/detail.vue'
import dispatchDetail from '@/views/salesManagement/pushOrderManagement/detail.vue'
export default {
  name: 'MatchSendOutOrderList',
  mixins: [commonMixin],
  components: { STable, VSelect, dealerSubareaScopeList, rangeDate, subarea, Area, commonModal, salesDetail, dispatchDetail },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      showDetailModal: false,
      detailType: 2,
      bizSn: null,
      tableHeight: 0,
      time: [],
      checktime: [],
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        checkBeginDate: '',
        checkEndDate: '',
        buyerSn: undefined, //  客户名称
        receiverSn: undefined, // 收货客户
        salesBillNo: '', //  销售单号
        dispatchBillNo: '', //  备货单号
        billStatus: undefined, //  业务状态
        subareaArea:{
          subareaSn: undefined,
          subareaAreaSn: undefined
        },
        voidFlag: undefined,
        shippingAddrProvinceSn: undefined,
        checkStatus: undefined
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dispatchlList(Object.assign(parameter, this.queryParam)).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
        })
      }
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', scopedSlots: { customRender: 'salesBillNo' }, width: '10%', align: 'center' },
        { title: '备货单号', scopedSlots: { customRender: 'dispatchBillNo' }, width: '10%', align: 'center' },
        { title: '发货编号', dataIndex: 'sendNo', width: '6%', align: 'center' },
        { title: '客户名称', dataIndex: 'buyerName', width: '14%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收货客户名称', dataIndex: 'receiverName', width: '14%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品款数', dataIndex: 'totalCategory', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '产品数量', dataIndex: 'totalQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '总售价', dataIndex: 'totalAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '业务状态', dataIndex: 'billStatusDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据状态', dataIndex: 'voidFlagDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单状态', dataIndex: 'checkStatusDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单人员', dataIndex: 'checkPersonName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单时间', dataIndex: 'checkDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '7%', align: 'center' }
      ]
      if (this.$hasPermissions('M_matchSendOutOrderList_salesPrice')) { //  售价权限
        arr.splice(9, 0, { title: '总售价', dataIndex: 'totalAmount', width: '6%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      return arr
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    checkDateChange (date) {
      this.queryParam.checkBeginDate = date[0]
      this.queryParam.checkEndDate = date[1]
    },
    custChange (val) {
      this.queryParam.buyerSn = val.key
    },
    shcustChange (val) {
      this.queryParam.receiverSn = val.key
    },
    // 详情
    handleDetail (row, type) {
      this.detailType = type
      this.bizSn = type == 0 ? row.salesBillSn : row.dispatchBillSn
      this.$store.state.app.curActionPermission = type == 0 ? 'B_salesDetail' : 'B_dispatchDetail'
      this.showDetailModal = true
    },
    cancelDetail () {
      this.showDetailModal = false
      this.detailType = 2
      this.bizSn = null
      this.$store.state.app.curActionPermission = ''
    },
    // 对单
    handleAudit (row) {
      const _this = this
      this.$confirm({
        title: '操作提示',
        content: '确认对单通过吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          dispatchCheck({ dispatchBillSn: row.dispatchBillSn }).then(res => {
            _this.$message.info(res.message)
            if (res.status == 200) {
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    subareaChange(val){
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.$refs.rangeDate.resetDate(this.time)
      this.checktime = []
      this.$refs.checkDate.resetDate(this.checktime)
      this.$refs.dealerSubareaScopeList.resetForm()
      this.$refs.shbuyerName.resetForm()
      this.queryParam = {
        beginDate: '',
        endDate: '',
        checkBeginDate: '',
        checkEndDate: '',
        buyerSn: undefined, //  客户名称
        receiverSn: undefined, // 收货客户
        salesBillNo: '', //  销售单号
        dispatchBillNo: '', //  备货单号
        billStatus: undefined, //  业务状态
        subareaArea:{
          subareaSn: undefined,
          subareaAreaSn: undefined
        },
        voidFlag: undefined,
        shippingAddrProvinceSn: undefined,
        checkStatus: undefined
      }
      if(this.advanced){
        this.$refs.subarea.clearData()
      }
      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 - 210
    }
  },
  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">
  .matchSoo-wrap{
    .sTable{
      margin-top: 10px;
    }
    .ant-tabs-bar{
      margin:0;
    }
  }
</style>
