<template>
  <a-card size="small" :bordered="false" class="productLaunchAuditList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <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="productLaunchAuditList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入产品名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="productLaunchAuditList-code" v-model.trim="queryParam.code" allowClear placeholder="请输入产品编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="审核状态">
                <v-select
                  v-model="queryParam.state"
                  ref="state"
                  id="productLaunchAuditList-state"
                  code="PRODUCT_ONOFFLINE_STATUS"
                  placeholder="请选择审核状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productLaunchAuditList-refresh">查询</a-button>
              <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="productLaunchAuditList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <div style="margin-bottom: 15px">
        <a-button type="primary" v-if="$hasPermissions('B_productLaunchAudit_batchAudit')" id="productLaunchAudit-export" :loading="loading" @click="handleBatchAudit">批量审核</a-button>
        <span style="margin-left: 8px" v-if="$hasPermissions('B_productLaunchAudit_batchAudit')">
          <template v-if="hasSelected">{{ `已选 ${selectedRowKeys.length} 项` }}</template>
        </span>
      </div>
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :row-selection="rowSelection"
        :rowKey="(record) => record.productSn"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1510, y: tableHeight }"
        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.state=='WAIT_ONLINE_AUDIT' && $hasPermissions('B_productLaunchAudit_audit')"
            class="button-warning"
            @click="handleAudit(record)"
            id="productLaunchAuditList-audit-btn">审核</a-button>
          <span v-else>--</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { productOnlineList, productBatchOnlineAudit, productOnlineAudit } from '@/api/product'
export default {
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      queryParam: { //  查询条件
        name: '', //  名称
        code: '', //  编码
        state: undefined //  状态
      },
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '产品名称', dataIndex: 'name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'code', width: 220, align: 'center' },
        { title: '产品分类', scopedSlots: { customRender: 'productType' }, width: 200, align: 'center' },
        { title: '提交时间', dataIndex: 'onlineSubmitTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提交人', dataIndex: 'onlineSubmitPerson', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '审核状态', dataIndex: 'stateDictValue', width: 110, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'onlineAuditTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核人', dataIndex: 'onlineAuditPerson', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      selectedRowKeys: [], // Check here to configure the default column
      loading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 380
        }
        return productOnlineList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          this.disabled = false
          return data
        })
      }
    }
  },
  computed: {
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    rowSelection () {
      return this.$hasPermissions('B_productLaunchAudit_batchAudit') ? {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          this.onSelectChange(selectedRowKeys)
        },
        getCheckboxProps: record => ({
          props: {
            disabled: record.state != 'WAIT_ONLINE_AUDIT'
          }
        })
      } : null
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.queryParam.code = ''
      this.queryParam.state = undefined
      this.$refs.table.refresh(true)
    },
    // 单个审核
    handleAudit (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要上线审核吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          productOnlineAudit({ sn: row.productSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 批量审核
    handleBatchAudit () {
      const _this = this
      if (_this.selectedRowKeys.length < 1) {
        _this.$message.warning('请在列表勾选后再进行批量操作！')
        return
      }
      this.$confirm({
        title: '提示',
        content: '确认要批量上线审核吗？',
        centered: true,
        onOk () {
          _this.loading = true
          _this.spinning = true
          productBatchOnlineAudit(_this.selectedRowKeys).then(res => {
            _this.loading = false
            if (res.status == 200) {
              _this.selectedRowKeys = []
              _this.selectedRows = []
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        },
        onCancel () {
          console.log('Cancel')
        }
      })
    },
    onSelectChange (selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>

<style lang="less">
  .productLaunchAuditList-wrap{
    .sTable{
      margin-top: 20px;
    }
  }
</style>
