<template>
  <div>
    <a-card size="small" :bordered="false" class="productUniversalList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" if="productUniversalList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="productUniversalList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品名称">
                <a-input id="productUniversalList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="通用产品编码">
                <a-input id="productUniversalList-productCommonCode" v-model.trim="queryParam.productCommonCode" allowClear placeholder="请输入通用产品编码"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="通用产品名称">
                  <a-input id="productUniversalList-productCommonName" v-model.trim="queryParam.productCommonName" allowClear placeholder="请输入通用产品名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="产品状态">
                  <v-select code="PRODUCT_STATUS" id="productUniversalList-productState" v-model="queryParam.productState" allowClear placeholder="请选择产品状态"></v-select>
                </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="productUniversalList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productUniversalList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                v-if="$hasPermissions('B_productUniversalExport')"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                id="productUniversalList-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator" v-if="$hasPermissions('B_productUniversal_add')">
          <a-button id="productUniversalList-add" type="primary" @click="handleEdit()">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品分类 -->
          <template slot="productType" slot-scope="text, record">
            <span v-if="record.productCommont.productTypeName2 || record.productCommont.productTypeName3">{{ record.productCommont.productTypeName2 }} {{ record.productCommont.productTypeName3 ? '>' : '' }} {{ record.productCommont.productTypeName3 }}</span>
            <span v-else>--</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_productUniversal_edit')"
              class="button-info"
              @click="handleEdit(record)"
              :id="'productUniversalList-edit-btn'+record.id">编辑</a-button>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_productUniversal_del')"
              class="button-error"
              @click="handleDel(record)"
              :id="'productUniversalList-del-btn'+record.id">删除</a-button>
            <span v-if="!$hasPermissions('B_productUniversal_edit') && !$hasPermissions('B_productUniversal_del')">--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 新增/编辑 -->
      <product-universal-edit-modal v-drag :openModal="openModal" :itemId="itemId" @close="closeModal" @ok="$refs.table.refresh()" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import { exportExcel } from '@/libs/JGPrint.js'
// 组件
import { STable, VSelect } from '@/components'
import productUniversalEditModal from './editModal.vue'
// 接口
import { productUniversalCodeList, productUniversalCodeDel, productUniversalExport } from '@/api/productUniversalCode'
export default {
  name: 'ProductUniversalList',
  mixins: [commonMixin],
  components: { STable, VSelect, productUniversalEditModal },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出loading
      queryParam: { //  查询条件
        productCode: '', // 产品名称
        productName: '', // 产品名称
        productState: undefined, // 产品状态
        productCommonName: '', // 通用产品名称
        productCommonCode: ''// 通用产品编码
      },
      productType: [], // 产品分类
      openModal: false, //  编辑  弹框
      itemId: '', //  当前id
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', align: 'left', width: '20%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品状态', dataIndex: 'product.stateDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '通用产品编码', dataIndex: 'productCommonCode', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '通用产品名称', dataIndex: 'productCommont.name', width: '12%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '通用产品分类', scopedSlots: { customRender: 'productType' }, width: '12%', align: 'left' },
        { title: '通用产品状态', dataIndex: 'productCommont.stateDictValue', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取表格数据 有分页
        return productUniversalCodeList(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
        })
      }
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.queryParam.productState = undefined
      this.queryParam.productCommonName = ''
      this.queryParam.productCommonCode = ''
      this.$refs.table.refresh(true)
    },
    //  编辑
    handleEdit (row) {
      if (row) {
        this.itemId = row.id
      } else {
        this.itemId = null
      }
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          productUniversalCodeDel({ id: row.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  导出
    handleExport () {
      const _this = this
      const params = this.queryParam
      this.spinning = true
      exportExcel(productUniversalExport, params, '通用产品列表', function () {
        _this.spinning = false
      })
    },
    // 配置导出参数
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDHHmmss')
      const fname = '通用产品_' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 240
    }
  },
  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>
