<template>
  <div class="productLaunchAuditList-wrap">
    <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="5" :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="5" :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="提交时间">
                <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24" v-if="queryParam.bizState == 'PASS'">
              <a-form-item label="是否新品">
                <v-select code="FLAG" id="productLaunchAuditList-isNew" v-model="queryParam.newProductFlag" allowClear placeholder="请选择产品是否为新品"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productLaunchAuditList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productLaunchAuditList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <!-- 状态 -->
    <div style="background-color: #fff;">
      <v-select
        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 v-if="$hasPermissions('B_productLaunchAudit_batchAudit')&&queryParam.bizState == 'WAIT'">
          <a-button type="primary" ghost id="productLaunchAudit-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_productLaunchAudit_batchAudit')&&queryParam.bizState == 'WAIT'?{ columnWidth: 40 }:null"
          @rowSelection="rowSelectionFun"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :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="showFlag" slot-scope="text, record">
            <a-switch
              checkedChildren="显示"
              unCheckedChildren="隐藏"
              id="productLaunchAuditList-isEnable"
              v-model="record.isEnable"
              @change="changeFlagHandle(record)" />
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.bizState=='WAIT' && $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>
  </div>
</template>

<script>
import rangeDate from '@/views/common/rangeDate.vue'
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { productLogList, productLogAuditBatch, productLogAudit, updateShowFlag } from '@/api/product'
export default {
  name: 'ProductLaunchAuditList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      time: [],
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        name: '', //  名称
        code: '', //  编码
        bizType: 'ONLINE', // 上线
        bizState: 'WAIT', //  状态
        newProductFlag: undefined// 是否新品
      },
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      loading: false,
      // 加载数据方法 必须为 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
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              data.list[i].isEnable = data.list[i].showFlag + '' === '1'
            }
            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: '15%', align: 'left' },
        { title: '提交时间', dataIndex: 'submitTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提交人', dataIndex: 'submitPerson', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '审核状态', dataIndex: 'bizStateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.queryParam.bizState == 'WAIT') {
        arr.splice(6, 1, { title: '操作', scopedSlots: { customRender: 'action' }, width: '6%', align: 'center' })
      } else if (this.queryParam.bizState != 'WAIT') {
        arr.splice(6, 1, { title: '审核时间', dataIndex: 'auditTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } })
        arr.splice(7, 1, { title: '审核人', dataIndex: 'auditPerson', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true })
        if (this.queryParam.bizState === 'PASS') {
          arr.splice(8, 1, { title: '显示状态', scopedSlots: { customRender: 'showFlag' }, width: '10%', align: 'center' })
        }
      }
      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.code = ''
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.newProductFlag = undefined
      this.time = []
      this.$refs.rangeDate.resetDate(this.time)
      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()
          }
        }
      })
    },
    // 修改显示状态
    changeFlagHandle (record) {
      const _data = {
        productLogSn: record.productLogSn,
        showFlag: record.isEnable ? '1' : '0'
      }
      this.spinning = true
      updateShowFlag(_data).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
        }
        this.$refs.table.refresh()
        this.spinning = false
      })
    },
    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' ? 280 : 250)
    }
  },
  watch: {
    '$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()
    }
  }
}
</script>
<style lang="less">
  .productLaunchAuditList-wrap{
    .sTable{
      margin-top:10px;
    }
    .ant-tabs-bar{
      margin: 0;
    }
  }
</style>
