<template>
  <a-card size="small" :bordered="false" class="bulkReturnGoodsList-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-input id="bulkReturnGoodsList-sparePartsReturnNo" v-model.trim="queryParam.sparePartsReturnNo" allowClear placeholder="请输入散件退货单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="创建时间">
              <rangeDate ref="rangeDate" @change="dateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="供应商">
              <a-select id="bulkReturnGoodsList-customerTypeSn" v-model="queryParam.customerTypeSn" placeholder="请选择供应商">
                <a-select-option v-for="item in custTypeList" :value="item.id" :key="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="单据状态">
                <v-select code="CHECK_ENABLE_STATE" id="bulkReturnGoodsList-satelliteFlag" v-model="queryParam.satelliteFlag" allowClear placeholder="请选择"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="结算状态">
                <v-select code="CHECK_ENABLE_STATE" id="bulkReturnGoodsList-satelliteFlag" v-model="queryParam.satelliteFlag" allowClear placeholder="请选择"></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="bulkReturnGoodsList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="bulkReturnGoodsList-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>
    <!-- 操作按钮 -->
    <div class="table-operator">
      <a-button id="bulkReturnGoodsList-add" type="primary" class="button-error" @click="handleAdd()">新增</a-button>
    </div>
    <!-- 合计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div slot="message">共 <strong>6</strong> 条记录，总数量合计 <strong>14</strong> ，总金额合计 <strong>￥14</strong> </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1310, y: tableHeight }"
      bordered>
      <!-- 是否转销售单 -->
      <template slot="invenstoryNum" slot-scope="text, record">
        <span>{{ record.invenstoryNum == 1 ? '是' : '否' }}</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" class="button-info" @click="handleEdit(record)" id="bulkReturnGoodsList-edit-btn">编辑</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleDetail(record)" id="bulkReturnGoodsList-detail-btn">详情</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" @click="handleAdopt(record)" id="bulkReturnGoodsList-isAdopt-btn">{{ record.adopt == 0 ? '通过' : '不通过' }}</a-button>
        <a-divider type="vertical" style="margin: 0;" />
        <a-button size="small" type="link" class="button-error" @click="handleDel(record)" id="bulkReturnGoodsList-del-btn">删除</a-button>
      </template>
    </s-table>
    <!-- 选择基本信息弹框 -->
    <basic-info-modal :openModal="openModal" @ok="handleOk" @close="openModal=false" />
  </a-card>
</template>

<script>
import { sparePartsRetList } from '@/api/sparePartsRet'
import { STable, VSelect } from '@/components'
import basicInfoModal from './basicInfoModal.vue'
import rangeDate from '@/views/common/rangeDate.vue'
export default {
  components: { STable, VSelect, basicInfoModal, rangeDate },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        sparePartsReturnNo: '', //  散件退货单号
        supplierSn: undefined, //  供应商
        settleState: undefined, //  结算状态
        state: undefined //  单据状态
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '散件退货单号', dataIndex: 'sparePartsReturnNo', width: 140, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center' },
        { title: '供应商', dataIndex: 'supplierName', align: 'center', customRender: function (text) { return text || '--' } },
        { 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: 'state', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结算状态', dataIndex: 'settleState', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 230, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 275
        }
        return sparePartsRetList(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
        })
      },
      custTypeList: [], //  供应商下拉
      openModal: false // 基本信息弹框是否显示
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.sparePartsReturnNo = ''
      this.queryParam.supplierSn = undefined
      this.queryParam.settleState = undefined
      this.queryParam.state = undefined
      this.$refs.table.refresh(true)
    },
    //  基本信息  保存
    handleOk () {
      this.$router.push({ path: `/bulkManagement/bulkReturnGoods/add` })
    },
    //  新增
    handleAdd () {
      this.openModal = true
    },
    //  编辑
    handleEdit (row) {
      this.$router.push({ path: `/bulkManagement/bulkReturnGoods/edit/${row.id}` })
    },
    //  详情
    handleDetail (row) {
      this.$router.push({ path: `/bulkManagement/bulkReturnGoods/detail/${row.id}` })
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          // warehouseDel({
          //   id: row.id
          // }).then(res => {
          //   if (res.status == 200) {
          //     _this.$message.success(res.message)
          //     _this.$refs.table.refresh()
          //   }
          // })
        }
      })
    },
    //  是否通过
    handleAdopt (row) {
      const _this = this
      const content = '确认要审核' + row.adopt ? '不' : '' + '通过吗？'
      this.$confirm({
        title: '提示',
        content: content,
        centered: true,
        onOk () {
          // warehouseDel({
          //   id: row.id
          // }).then(res => {
          //   if (res.status == 200) {
          //     _this.$message.success(res.message)
          //     _this.$refs.table.refresh()
          //   }
          // })
        }
      })
    }
  }
}
</script>
