<template>
  <div class="mod-prod-prodTemplateSeries">
    <!-- 搜索相关区域 -->
    <!--<div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" label-width="auto" size="small">
        <div class="input-row">
          <el-form-item prop="seriesName" label="团队组合" class="search-form-item">
            <el-select v-model="searchForm.seriesId" placeholder="请选择所属团体组合" :filterable="true" :clearable="true"
                       :remote-method="getSeriesList" :loading="searchLoading">
              <el-option v-for="series in seriesList" :label="series.seriesName" :value="series.seriesId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange(searchForm)">{{$t('crud.searchBtn')}}</div>
            <div class="default-btn" @click="resetSearchForm('searchForm')">{{$t('shop.resetMap')}}</div>
          </el-form-item>
        </div>
      </el-form>
    </div>-->
    <!-- 列表相关区域 -->
    <div class="main-container">
      <div class="operation-bar">
        <div class="default-btn primary-btn" @click="addOrUpdateHandle()"
        >新增清单</div>
      </div>
      <div class="table-con spec-table">
        <el-table
          ref="specListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%">
          <!-- 系列名称 -->
          <el-table-column label="清单名称" prop="seriesName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.cardName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="seq" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime}}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注信息" prop="seq" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.remark}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" :label="$t('publics.operating')" width="auto">
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div class="default-btn text-btn" @click="viewCardList(scope.row)">查看清单</div>
                <div class="default-btn text-btn" @click="toCardLibPage(scope.row)">去选图</div>
                <div class="default-btn text-btn" @click="addOrUpdateHandle(scope.row)">修改</div>
                </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
        <el-pagination
          v-if="dataList.length"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    <add-or-update v-if="viewCardVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <el-dialog :title="cardUserPurchaseForm.id? '编辑清单':'新增清单'" :close-on-click-modal="false" v-if="addOrUpdateVisible" :visible.sync="addOrUpdateVisible" width="50%">
      <el-form
        :model="cardUserPurchaseForm"
        size="small"
        ref="cardUserPurchaseFormRef"
        label-width="80px">
        <el-form-item prop="cardName" label="清单名称">
          <el-input type="text" v-model="cardUserPurchaseForm.cardName" placeholder="请输入清单名称"></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注信息">
          <el-input type="textarea" v-model="cardUserPurchaseForm.remark" placeholder="可输入备注信息"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="default-btn"  @click="addOrUpdateVisible = false">取消</div>
        <div class="default-btn primary-btn" @click="cardUserPurchaseFormSubmit">确定</div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './card-user-purchase-item-add-or-update'
export default {
  data () {
    return {
      cardUserPurchaseForm: {
        id: null,
        cardName: null,
        remark: null,
      },
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {}, // 搜索
      dataListLoading: false,
      addOrUpdateVisible: false,
      addOrUpdatePersonVisible: false,
      usageList:[],
      seriesList: [],
      searchLoading: false,
      viewCardVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getDataList()
  },
  mounted () {
  },
  methods: {
    cardUserPurchaseFormSubmit(){
      this.$http({
        url: this.$http.adornUrl('/card/cardUserPurchase'),
        method: this.cardUserPurchaseForm.id? 'PUT':'POST',
        data: this.$http.adornData(this.cardUserPurchaseForm)
      }).then(({data}) => {
        this.addOrUpdateVisible = false
        this.$refs.cardUserPurchaseFormRef.resetFields()
        this.refreshChange()
        if(data){
          this.$message({
            message: '提交成功',
            type: 'success',
          })
        }else{
          this.$message({
            message: '提交失败',
            type: 'success',
          })
        }
      }).catch((e) => {
        this.$message({
          message: '提交异常',
          type: 'success',
        })
      })
    },
    getSeriesList (seriesName) {
      this.$http({
        url: this.$http.adornUrl('/prod/prodTemplateSeries/list'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign({
              size: 200,
              seriesName: seriesName
            }
          )
        )
      }).then(({data}) => {
        this.seriesList = data
      })
    },
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/card/cardUserPurchase/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign({
            current: page == null ? this.page.currentPage : page.currentPage,
            size: page == null ? this.page.pageSize : page.pageSize,
          },
            this.searchForm
          )
        )
      }).then(({data}) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
        this.seriesList = this.dataList
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (cardUserPurchase) {
      this.addOrUpdateVisible = true
      if(cardUserPurchase && cardUserPurchase.id){
        this.cardUserPurchaseForm.id = cardUserPurchase.id
        this.cardUserPurchaseForm.cardName = cardUserPurchase.cardName
        this.cardUserPurchaseForm.remark = cardUserPurchase.remark
      }
    },
    // 新增 / 修改
    viewCardList (cardLib) {
      this.viewCardVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(cardLib)
      })
    },
    // 去图集页面选图
    toCardLibPage(cardLib) {
      this.$router.push({
        path: '/pickCard-card-set-lib'
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('admin.isDeleOper') + '?', this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/card/cardLib/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 200,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调
    refreshChange () {
      // this.page.currentPage = 1
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.page.currentPage = 1
      this.searchForm = params
      this.getDataList(this.page)
    },
    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    },
    /**
     * 重置表单
     * @param {String} formName 表单名称
     */
    resetSearchForm (formName) {
      this.$refs[formName].resetFields()
      this.getDataList()
    }
  }
}
</script>
<style lang="scss">
.mod-prod-prodTemplateSeries {
}
</style>
