<template>
  <a-card size="small" :bordered="false" class="shelfSetList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <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="模板名称">
                <a-input id="shelfPlList-templateName" v-model.trim="queryParam.templateName" allowClear placeholder="请输入模板名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="模板类型">
                <v-select
                  id="shelfPlList-sysFlag"
                  v-model="queryParam.sysFlag"
                  code="SHELF_TEMPLATE_SYS_FLAG"
                  placeholder="请选择模板类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="shelfPlList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="shelfPlList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button v-if="orgData.length<20" type="primary" class="button-error" @click="addTemp" id="shelfPlList-addTemp">新增</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 模板名称 -->
        <template slot="templateName" slot-scope="text, record">
          <span class="table-td-link" id="shelfPlList-links" @click="handleDetail(record)">{{ record.templateName }}</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <div>
            <a-button
              :id="'shelfPlList-edit-'+record.id"
              size="small"
              v-if="record.sysFlag==0"
              type="link"
              class="button-primary"
              @click="addTemp(record)">更改名称</a-button>
            <a-button
              v-if="orgData.length<20"
              :id="'shelfPlList-copy-'+record.id"
              size="small"
              type="link"
              class="button-primary"
              @click="handleCopy(record)">复制</a-button>
            <a-button
              :id="'shelfPlList-del-'+record.id"
              size="small"
              v-if="record.sysFlag==0"
              type="link"
              class="button-error"
              @click="handleCancel(record)">删除</a-button>
          </div>
        </template>
      </s-table>
    </a-spin>
    <!-- 基础设置弹框 -->
    <basic-info-modal :openModal="openModal" :nowData="nowData" @ok="saveOk" @close="openModal=false" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import basicInfoModal from './basicInfoModal.vue'
import commonModal from '@/views/common/commonModal.vue'
import { shelfTemplateList, shelfTemplateCopy, shelfTemplateDel } from '@/api/shelfTemplate'
export default {
  name: 'ShelfProductTemplList',
  components: { STable, VSelect, basicInfoModal, commonModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        templateName: '', // 模板名称
        sysFlag: undefined // 模板类型
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '模板类型', dataIndex: 'sysFlagDictValue', width: '20%', align: 'center' },
        { title: '模板名称', scopedSlots: { customRender: 'templateName' }, width: '50%', align: 'left', ellipsis: true },
        { title: '产品款数', dataIndex: 'productCategory', width: '15%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '11%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfTemplateList(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
          this.orgData = data.list
          return data
        })
      },
      orgData: [], // 原始列表数据
      openModal: false, // 打开新增编辑弹框
      nowData: null // 当前操作数据
    }
  },
  methods: {
    // 新增/修改模板
    addTemp (row) {
      this.nowData = row || {}
      this.openModal = true
    },
    // 模板详情设置
    handleDetail (data) {
      this.$router.push({ name: 'shelfProductTemplSetting', params: { sn: data.templateSn } })
    },
    // 复制模板
    handleCopy (record) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '复制选择的内置模板，将在列表生成一条相同的模板，可进行编辑！是否确认生成？',
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          shelfTemplateCopy({
            templateSn: record.templateSn
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.resetSearchForm()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 删除模板
    handleCancel (record) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除模板后，模板信息将全部清除，是否确认删除？',
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          shelfTemplateDel({
            templateSn: record.templateSn
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 保存成功
    saveOk () {
      this.$refs.table.refresh(true)
      this.openModal = false
    },
    // 重置
    resetSearchForm () {
      this.queryParam.templateName = ''
      this.queryParam.sysFlag = undefined
      this.$refs.table.refresh(true)
    },
    // 表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    },
    // 初始化页面
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
