<template>
  <a-card size="small" :bordered="false" class="chainTransferInList-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="chainTransferInList-time"
                :disabledDate="disabledDate"
                v-model="time"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="调出对象">
              <a-select
                placeholder="请选择调出对象"
                allowClear
                id="chainTransferInList-outTenantSn"
                v-model="queryParam.outTenantSn"
                :showSearch="true"
                option-filter-prop="children"
                :filter-option="filterOption">
                <a-select-option v-for="item in outTenantSnData" :key="item.sn" :value="item.sn">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="调拨产品类型">
              <v-select
                v-model="queryParam.allocationType"
                ref="allocationType"
                id="chainTransferInList-allocationType"
                code="ALLOCATION_LINKAGE_PRODUCT_TYPE"
                placeholder="请选择调拨产品类型"
                allowClear></v-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="业务状态">
                <v-select
                  v-model="queryParam.state"
                  ref="state"
                  id="chainTransferInList-state"
                  code="ALLOCATION_LINKAGE_PUT_STATUS"
                  placeholder="请选择业务状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="财务状态">
                <v-select
                  v-model="queryParam.settleState"
                  ref="settleState"
                  id="chainTransferInList-settleState"
                  code="FINANCIAL_PAY_STATUS"
                  placeholder="请选择财务状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24" style="margin-bottom: 24px;">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="chainTransferInList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="chainTransferInList-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: 1650, y: tableHeight }"
      bordered>
      <!-- 连锁调入单号 -->
      <template slot="allocationLinkagePutNo" slot-scope="text, record">
        <span style="color: #ed1c24;cursor: pointer;" @click="handleDetail(record)">{{ record.allocationLinkagePutNo }}</span>
      </template>
      <!-- 财务状态 -->
      <template slot="settleState" slot-scope="text, record">
        <a-badge :color="text=='FINISH'?'#87d068':'gold'" :text="record.settleStateDictValue" />
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          size="small"
          type="link"
          v-if="record.state == 'WAIT_SUBMIT'"
          class="button-primary"
          @click="handleEdit(record)"
          id="chainTransferInList-warehousing-btn">入库</a-button>
        <a-button
          size="small"
          type="link"
          v-if="record.state == 'WAIT_AUDIT'"
          class="button-warning"
          @click="handleExamine(record)"
          id="chainTransferInList-examine-btn">审核</a-button>
        <a-button
          size="small"
          type="link"
          v-if="record.state == 'WAIT_AUDIT'"
          class="button-info"
          @click="handleEdit(record)"
          id="chainTransferInList-edit-btn">编辑</a-button>
        <span v-if="(record.state != 'WAIT_SUBMIT') && (record.state != 'WAIT_AUDIT')">--</span>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, VSelect } from '@/components'
import { allocLinkagePutList, allocLinkagePutAudit } from '@/api/allocLinkagePut'
import { getTenantList } from '@/api/allocLinkageOut'
export default {
  components: { STable, VSelect },
  data () {
    return {
      tableHeight: 0,
      advanced: false, // 高级搜索 展开/关闭
      time: [], //  入库时间
      queryParam: { //  查询条件
        outTenantSn: undefined, //  调出对象
        allocationType: undefined, //  调拨产品类型
        state: undefined, //  业务状态
        settleState: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      dateFormat: 'YYYY-MM-DD',
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center' },
        { title: '连锁调入单号', scopedSlots: { customRender: 'allocationLinkagePutNo' }, width: 220, align: 'center' },
        { title: '调出对象', dataIndex: 'outTenantName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '总款数', dataIndex: 'productTotalCategory', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'productTotalQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总成本', dataIndex: 'productTotalCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库时间', dataIndex: 'putWarehouseTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调拨产品类型', dataIndex: 'allocationTypeDictValue', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'stateDictValue', width: 100, align: 'center' },
        { title: '财务状态', scopedSlots: { customRender: 'settleState' }, width: 100, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 170, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 80
        }
        // 入库时间
        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 allocLinkagePutList(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
        })
      },
      outTenantSnData: []
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    //  重置
    resetSearchForm () {
      this.queryParam.outTenantSn = undefined
      this.queryParam.allocationType = undefined
      this.queryParam.state = undefined
      this.queryParam.settleState = undefined
      this.time = []
      this.$refs.table.refresh(true)
    },
    //  详情
    handleDetail (row) {
      this.$router.push({ path: `/allocationManagement/chainTransferIn/detail/${row.allocationLinkagePutSn}` })
    },
    //  入库/编辑
    handleEdit (row) {
      this.$router.push({ path: `/allocationManagement/chainTransferIn/edit/${row.id}/${row.allocationLinkagePutSn}` })
    },
    //  审核
    handleExamine (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要审核通过吗？',
        centered: true,
        onOk () {
          allocLinkagePutAudit({ sn: row.allocationLinkagePutSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    // 调出对象
    getPutTenantList () {
      getTenantList({}).then(res => {
        if (res.status == 200) {
          this.outTenantSnData = res.data
        } else {
          this.outTenantSnData = []
        }
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getPutTenantList()
    })
  }
}
</script>
