<template>
  <div class="productOfflineAuditList-wrap">
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="productOfflineAuditList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="提交时间">
                <rangeDate ref="rangeDate" id="productOfflineAuditList-rangeDate" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码/通用编码">
                <a-input id="productOfflineAuditList-queryWord" v-model.trim="queryParam.queryWord" allowClear placeholder="请输入产品编码/通用编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品名称">
                <a-input id="productOfflineAuditList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入产品名称"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="下线选项">
                  <v-select
                    v-model="queryParam.reasonType"
                    ref="reasonType"
                    id="productOfflineAuditList-reasonType"
                    code="OFFLINE_REASON_TYPE"
                    placeholder="请选择下线选项"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="品牌">
                  <ProductBrand id="productOfflineAuditList-productBrandSn" v-model="queryParam.productBrandSn"></ProductBrand>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="产品分类">
                  <productTypeAll placeholder="请选择产品分类" @change="changeProductType" v-model="productType" id="productOfflineAuditList-productType"></productTypeAll>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productOfflineAuditList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productOfflineAuditList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px" id="productOfflineAuditList-advanced">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <!-- 状态 -->
    <div style="background-color: #fff;">
      <v-select
        id="productOfflineAuditList-bizState"
        v-model="queryParam.bizState"
        ref="state"
        showType="tabs"
        @change="resetSearchForm()"
        code="PRODUCT_LOG_BIZ_STATE"
        allowClear></v-select>
    </div>
    <!-- 列表 -->
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <div style="margin-bottom: 10px" v-if="$hasPermissions('B_productOfflineAudit_batchAudit')&&queryParam.bizState == 'WAIT'" >
          <a-button type="primary" ghost id="productOfflineAuditList-export" :loading="loading" @click="handleBatchAudit">批量审核</a-button>
          <span style="margin-left: 10px">
            <template v-if="rowSelectionInfo && rowSelectionInfo.selectedRowKeys.length>0">{{ `已选 ${rowSelectionInfo.selectedRowKeys.length} 项` }}</template>
          </span>
        </div>

        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.productLogSn"
          rowKeyName="productLogSn"
          :row-selection="$hasPermissions('B_productOfflineAudit_batchAudit')&&queryParam.bizState == 'WAIT'?{ columnWidth: 40 }:null"
          @rowSelection="rowSelectionFun"
          :columns="columns"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品分类 -->
          <template slot="productType" slot-scope="text, record">
            <span v-if="record.productTypeName2 || record.productTypeName3">{{ record.productTypeName2 }} {{ record.productTypeName3 ? '>' : '' }} {{ record.productTypeName3 }}</span>
            <span v-else>--</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.bizState=='WAIT' && $hasPermissions('B_productOfflineAudit_audit')"
              class="button-warning"
              @click="handleAudit(record)"
              :id="'productOfflineAuditList-audit-btn'+record.productLogSn">审核</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import rangeDate from '@/views/common/rangeDate.vue'
import { STable, VSelect } from '@/components'
import ProductBrand from '@/views/common/productBrand.js'
import productTypeAll from '@/views/common/productTypeAll.js'
// 组件
import { productLogList, productLogAudit, productLogAuditBatch } from '@/api/product'
export default {
  name: 'ProductOfflineAuditList',
  mixins: [commonMixin],
  components: { STable, VSelect, ProductBrand, productTypeAll, rangeDate },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      loading: false, // 表格加载状态
      time: [], // 提交时间
      //  查询参数
      queryParam: {
        beginDate: '', // 提交开始时间
        endDate: '', // 提交结束时间
        name: '', //  产品名称
        queryWord: '', // 产品编码/通用编码
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        reasonType: undefined, // 下线选项
        bizType: 'OFFLINE', // 下线
        bizState: 'WAIT' //  状态
      },
      productType: [], // 产品分类
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取列表数据  有分页
        return productLogList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.disabled = false
          }
          this.spinning = false
          // 计算表格高度
          this.setTableH()
          return data
        })
      },
      rowSelectionInfo: null// 表格选中数据
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '产品名称', dataIndex: 'name', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'code', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品分类', scopedSlots: { customRender: 'productType' }, width: '12%', align: 'left' },
        { title: '提交时间', dataIndex: 'submitTime', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提交人', dataIndex: 'submitPerson', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '下线选项', dataIndex: 'reasonType', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '通用编码', dataIndex: 'commonCode', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '下线备注', dataIndex: 'submitNote', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '审核状态', dataIndex: 'bizStateDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.queryParam.bizState == 'WAIT') {
        arr.splice(8, 1, { title: '操作', scopedSlots: { customRender: 'action' }, width: '5%', align: 'center' })
      } else {
        arr.splice(8, 1, { title: '审核时间', dataIndex: 'auditTime', width: '8%', align: 'center', customRender: function (text) { return text || '--' } })
        arr.splice(9, 1, { title: '审核人', dataIndex: 'auditPerson', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true })
      }
      return arr
    }
  },
  methods: {
    //   提交时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.queryParam.queryWord = ''
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.queryParam.reasonType = undefined
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.time = []
      this.$refs.rangeDate.resetDate(this.time)
      this.productType = []
      this.$refs.table.refresh(true)
    },
    // 单个审核接口
    singAudit (row, state) {
      const _this = this
      _this.spinning = true
      productLogAudit({ productLogSn: row.productLogSn, 'bizState': state }).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 单个审核弹窗提示
    handleAudit (row) {
      const _this = this
      const modal = this.$confirm({
        title: '提示',
        content: '请点击下方按钮确认操作',
        cancelText: '不通过',
        okText: '通过',
        centered: true,
        closable: true,
        onOk () {
          _this.singAudit(row, 'PASS')
        },
        onCancel (e) {
          if (!e.triggerCancel) {
            _this.singAudit(row, 'REFUSE')
            modal.destroy()
          }
        }
      })
    },
    // 批量审核
    batchAudit (state) {
      const _this = this
      _this.loading = true
      _this.spinning = true
      const params = {
        productLogSnList: _this.rowSelectionInfo && _this.rowSelectionInfo.selectedRowKeys || [],
        bizState: state
      }
      productLogAuditBatch(params).then(res => {
        _this.loading = false
        if (res.status == 200) {
          _this.$refs.table.clearSelected() // 清空表格选中项
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 批量审核弹窗提示
    handleBatchAudit () {
      const _this = this
      if (!_this.rowSelectionInfo || (_this.rowSelectionInfo && _this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        _this.$message.warning('请在列表勾选后再进行批量操作！')
        return
      }
      const modal = this.$confirm({
        title: '提示',
        content: '请点击下方按钮确认操作',
        cancelText: '不通过',
        okText: '通过',
        centered: true,
        closable: true,
        onOk () {
          _this.batchAudit('PASS')
        },
        onCancel (e) {
          if (!e.triggerCancel) {
            _this.batchAudit('REFUSE')
            modal.destroy()
          }
        }
      })
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productTypeSn3 = val[2] ? val[2] : ''
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.rowSelectionInfo = null
      this.$refs.table.clearTable()
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - (this.queryParam.bizState == 'WAIT' ? 283 : 250)
    }
  },
  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">
  .productOfflineAuditList-wrap{
    .sTable{
      margin-top:10px;
    }
    .ant-tabs-bar{
      margin: 0;
    }
  }
</style>
