<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" 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="创建时间">
                <rangeDate ref="rangeCreateDate" :value="createDate" @change="dateCreateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="提货单号">
                <a-input id="billOfLading-pickUpNo" v-model.trim="queryParam.pickUpNo" allowClear placeholder="请输入提货单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="申请人">
                <employee style="width: 100%;" id="billOfLading-Employee" v-model="queryParam.applyPersonSn"></employee>
              </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="billOfLading-state"
                    code="PICK_UP_STATE"
                    placeholder="请选择状态"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="审核时间">
                  <rangeDate ref="rangeExamineDate" @change="dateExamineChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="总部销退单号">
                  <a-input id="billOfLading-salesReturnBillNo" v-model.trim="queryParam.salesReturnBillNo" allowClear placeholder="请输入总部销退单号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="物流点">
                  <a-input id="billOfLading-logisticsPoint" v-model.trim="queryParam.logisticsPoint" allowClear placeholder="请输入物流点"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="发货方">
                  <custList placeholder="请输入客户名称搜索" ref="dealerSubareaScopeList" id="billOfLading-senderSn" @change="custChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="退货仓库">
                  <warehouse ref="warehouse" v-model="queryParam.warehouseSn"></warehouse>
                </a-form-model-item>
              </a-col>
            <!--  <a-col :md="6" :sm="24" v-show="isShowCustomerSearch">
                <a-form-item label="客服">
                  <customerService ref="customerName" v-model="queryParam.bizUserSn"></customerService>
                </a-form-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>
    </a-card>
    <a-card size="small" :bordered="false" class="billOfLading-wrap">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button type="primary" v-if="$hasPermissions('B_newPickUp')" @click="handleAdd">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+64.5+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 提货单号 -->
          <template slot="pickUpNo" slot-scope="text, record">
            <div v-if="$hasPermissions('M_detailPickUp')">
              <span class="link-bule" @click="handleDetail(record)">{{ record.pickUpNo }}</span>
            </div>
            <div v-else>{{ record.pickUpNo }}</div>
          </template>
          <!-- 关联单数 -->
          <template slot="salesReturnNum" slot-scope="text, record">
            <div v-if="record.salesReturnNum">{{ record.salesReturnNum }} 单</div>
            <div v-else></div>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <div>
              <a-button
                size="small"
                type="link"
                class="button-warning"
                v-if="record.state == 'WAIT_AUDIT'&&$hasPermissions('B_auditPickUp')"
                @click="handleEexamine(record)"
                id="billOfLading-eexamine-btn">审核进度</a-button>
              <a-button
                size="small"
                type="link"
                class="button-warning"
                v-if="record.state == 'AUDIT_REJECT'&&$hasPermissions('B_asginEditPickUp')"
                @click="handleAgainEdit(record)"
                id="billOfLading-eexamine-btn">再次编辑</a-button>
              <a-button
                size="small"
                v-if="record.state == 'WAIT_SUBMIT'&&$hasPermissions('B_editPickUp')"
                type="link"
                class="button-info"
                @click="handleEdit(record)"
                id="billOfLading-edit-btn">编辑</a-button>
              <a-button
                size="small"
                v-if="record.state == 'WAIT_SUBMIT'&&$hasPermissions('B_delPickUp')"
                type="link"
                class="button-error"
                @click="handleDel(record)"
                id="billOfLading-del-btn">删除</a-button>
            </div>
          </template>
        </s-table>
      </a-spin>
      <!-- 审核进度 -->
      <verifyModal :openModal="visibleAudit" :itemSn="auditInfo&&auditInfo.pickUpSn" :itemId="auditInfo&&auditInfo.id" @close="visibleAudit=false"></verifyModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
import employee from '../../expenseManagement/expenseReimbursement/employee.js'
import verifyModal from './verifyModal.vue'
import rangeDate from '@/views/common/rangeDate.vue'
import warehouse from '@/views/common/chooseWarehouse.js'
import customerService from '@/views/common/customerService'
import { pickUpQueryPage, pickUpDelete, pickUpAgainEdit } from '@/api/pickUp'
export default {
  name: 'BillOfLadingList',
  mixins: [commonMixin],
  components: {
    STable,
    VSelect,
    custList,
    employee,
    rangeDate,
    verifyModal,
    warehouse,
    customerService
  },
  data () {
    return {
      spinning: false,
      // 高级搜索 展开/关闭
      advanced: true,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      createDate: [], //  创建时间
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        auditBeginDate: '',
        auditEndDate: '',
        pickUpNo: '', // 提货单号
        applyPersonSn: undefined, // 申请人
        salesReturnBillNo: undefined, //  总部销退单号
        logisticsPoint: undefined, // 物流点
        senderSn: undefined, //  发货人
        state: undefined, // 业务状态
        warehouseSn: undefined, // 退货仓库
        bizUserSn: undefined // 客服
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return pickUpQueryPage(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
        })
      },
      visibleAudit: false,
      auditInfo: null,
      spinningAudit: false
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '编号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提货单号', scopedSlots: { customRender: 'pickUpNo' }, width: '10%', align: 'center' },
        { title: '关联销退单', dataIndex: 'salesReturnNum', scopedSlots: { customRender: 'salesReturnNum' }, width: '6%', align: 'center' },
        { title: '申请人', dataIndex: 'applyPersonName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货仓库', dataIndex: 'warehouseName', width: '7%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '主题', dataIndex: 'theme', width: '10%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '物流公司', dataIndex: 'logisticsCompany', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流点', dataIndex: 'logisticsPoint', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货方', dataIndex: 'dealerEntity.dealerName', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收货人', dataIndex: 'receiverName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '运费', dataIndex: 'freight', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '审核时间', dataIndex: 'auditDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'stateDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '6%', align: 'center' }
      ]
      return arr
    }
  },
  methods: {
    //  创建时间  change
    dateCreateChange (date) {
      this.queryParam.beginDate = date[0] ? (date[0]) : ''
      this.queryParam.endDate = date[1] ? (date[1]) : ''
    },
    //  审核时间  change
    dateExamineChange (date) {
      this.queryParam.auditBeginDate = date[0] ? (date[0]) : ''
      this.queryParam.auditEndDate = date[1] ? (date[1]) : ''
    },
    custChange (val) {
      this.queryParam.senderSn = val.key
    },
    // 新增
    handleAdd () {
      this.$store.state.app.tempBillOfData = null
      this.$router.push({ name: 'billOfLadingAdd' })
    },
    // 编辑
    handleEdit (row) {
      this.$router.push({ name: 'billOfLadingEdit', params: { sn: row.pickUpSn, wSn: row.warehouseSn } })
    },
    // 详情
    handleDetail (row) {
      this.$router.push({ name: 'billOfLadingDetail', params: { sn: row.pickUpSn, itemId: row.id } })
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          pickUpDelete({ pickUpSn: row.pickUpSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  审核进度
    handleEexamine (row) {
      this.auditInfo = row
      this.visibleAudit = true
    },
    // 再次编辑
    handleAgainEdit (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '系统将自动生成一个新的提货单，与原单据内容相同。确认再次编辑吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          pickUpAgainEdit({ pickUpSn: row.pickUpSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    resetSearchForm (flag) {
      this.$refs.rangeCreateDate.resetDate()
      if (this.advanced) {
        this.$refs.rangeExamineDate.resetDate()
        this.$refs.dealerSubareaScopeList.resetForm()
      }
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.auditBeginDate = ''
      this.queryParam.auditEndDate = ''
      this.queryParam.pickUpNo = ''
      this.queryParam.senderSn = undefined
      this.queryParam.applyPersonSn = undefined
      this.queryParam.logisticsPoint = undefined
      this.queryParam.salesReturnBillNo = flag ? this.$route.query.billNo : ''
      this.queryParam.state = undefined
      this.queryParam.warehouseSn = undefined
      this.queryParam.bizUserSn = undefined
      this.$refs.table.refresh(true)
      if (!flag) {
        this.$route.query.billNo = ''
      }
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  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.resetSearchForm(!!this.$route.query.billNo)
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.resetSearchForm(!!this.$route.query.billNo)
      this.pageInit()
    }
    if (this.$route.query.billNo) {
      this.resetSearchForm(!!this.$route.query.billNo)
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.setTableH()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .billOfLading-wrap{
    .sTable{
      .badge-con-t{
        .ant-badge-count{
          transform: scale(0.8);
          font-size: 13px;
        }
      }
    }
  }
</style>
