<template>
  <div class="mod-prod-prodTemplate">
    <!-- 搜索相关区域 -->
    <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="templateSeriesId" label="团队组合">
            <el-select v-model="searchForm.templateSeriesId" clearable filterable>
              <el-option
                      v-for="node in seriesList"
                      :key="node.seriesId"
                      :label="node.seriesName"
                      :value="node.seriesId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="templateSide" label="正背面">
            <el-select v-model="searchForm.templateSide" clearable filterable>
              <el-option :key="1" label="正面" :value="'Front'"></el-option>
              <el-option :key="2" label="背面" :value="'Back'"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="templateUsage" label="用途">
            <el-select v-model="searchForm.templateUsage" clearable filterable>
              <el-option
                v-for="node in templateUsageList"
                :key="node.value"
                :label="node.label"
                :value="node.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="joinCommonLib" label="是否入库">
            <el-select v-model="searchForm.joinCommonLib" clearable filterable>
              <el-option :key="0" label="未入库" :value="0"></el-option>
              <el-option :key="1" label="已入库" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange()">{{ $t('crud.searchBtn') }}</div>
            <div class="default-btn" @click="resetForm()">{{ $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()">{{$t("crud.addTitle")}}</div>
        <div class="default-btn primary-btn" v-if="multipleSelection.length > 0" @click="openClassifyDialog()">归类</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"
          @selection-change="handleSelectionChange"
          style="width: 100%">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="templatePic"
                  label="图片"
                  width="300">
            <template slot-scope="scope">
              <el-image
                  style="width: 100px; height: 100px"
                  :src="resourcesUrl + scope.row.templatePic"
                  v-if="scope.row.templatePic"
                  fit="fill"
                  @click="previewPic(scope.row)"
              />
            </template>
          </el-table-column>
          <!-- 模板下风格的系列，比如二次元风格的海贼王系列 -->
          <el-table-column label="团队组合" prop="templateSeries" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.templateSeries}}</span>
            </template>
          </el-table-column>
          <el-table-column label="图片用途" prop="usageList" align="center">
            <template slot-scope="scope">
              <div class="table-cell-text">{{ scope.row.usageList != null? scope.row.usageList.join('/'): "" }}</div>
            </template>
          </el-table-column>
          <!-- 模板面，Front：正面，Back：背面 -->
          <el-table-column label="正/背面" prop="templateSide" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.templateSide == 'Front'">正面</span>
              <span v-if="scope.row.templateSide == 'Back'">背面</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status" align="center">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
              <el-tag type="danger" v-if="scope.row.status == 0">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="seq" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.seq}}</span>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="seq" align="center">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.joinCommonLib === 1">已加入</el-tag>
              <el-tag type="danger" v-else>未加入</el-tag>
            </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="addOrUpdateHandle(scope.row.templateId)"
                     >{{$t("crud.updateBtn")}}</div>
                <div class="default-btn text-btn" @click.stop="deleteHandle(scope.row.templateId)"
                     >{{$t("text.delBtn")}}</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>
    <el-dialog :visible.sync="previewPicDialogVisible" :modal="false" title="图片预览" width="30%" top="3vh">
      <el-image :src="previewPicUrl" alt=""  style="width: 100%; height: 100%"/>
    </el-dialog>
    <el-dialog :visible.sync="classifyVisible" :modal="false" title="图片分类" width="30%" top="3vh">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" label-width="auto" size="small">
        <!-- 表单项 -->
        <div class="input-row">
          <el-form-item prop="seriesId" label="团队组合">
            <el-select v-model="classifyData.seriesId" clearable filterable @change="selectSeries">
              <el-option
                v-for="node in seriesList"
                :key="node.seriesId"
                :label="node.seriesName"
                :value="node.seriesId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="personId" label="爱豆" class="search-form-item">
            <el-select v-model="classifyData.personId" placeholder="请选择爱豆" clearable filterable>
              <el-option v-for="person in personList" :label="person.personName" :value="person.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="cardSide" label="正背面">
            <el-select v-model="classifyData.cardSide" clearable filterable>
              <el-option :key="1" label="正面" :value="'Front'"></el-option>
              <el-option :key="2" label="背面" :value="'Back'"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="cardSide" label="卡类型">
            <el-select v-model="classifyData.cardType" clearable filterable>
              <el-option :key="1" label="3寸小卡" :value="'3CXK'"></el-option>
              <el-option :key="2" label="3寸拍立得" :value="'3CFP'"></el-option>
              <el-option :key="3" label="明信片" :value="'MXP'"></el-option>
              <el-option :key="4" label="书签" :value="'SQ'"></el-option>
              <el-option :key="5" label="大方卡" :value="'FK'"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="cardSide" label="年份">
            <el-select v-model="classifyData.picYear" clearable filterable>
              <el-option :key="1" label="2025年" :value="'2025'"></el-option>
              <el-option :key="2" label="2024年" :value="'2024'"></el-option>
              <el-option :key="3" label="2024年以前" :value="'2024年以前'"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button class="default-btn" @click="classifyVisible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
          <el-button class="default-btn primary-btn" type="primary" @click="classifySubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
// import AddOrUpdate from './prodTemplate-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {
        templateStyleId: null,
        templateSeriesId: null,
        templateUsage: null
      }, // 搜索
      dataListLoading: false,
      addOrUpdateVisible: false,
      seriesList: [],
      styleList: [],
      usageList:[],
      personList:[],
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      previewPicUrl: null,
      previewPicDialogVisible: false,
      templateUsageList:[{label:'小卡', value:'YXK'},{label:'3寸花式拍立得', value:'FP'}
                        ,{label:'4寸花式拍立得', value:'4CFP'},{label:'5寸花式拍立得', value:'5CFP'}
                        ,{label:'明信片', value:'MXP'},{label:'票根', value:'PG'},{label:'书签', value:'SQ'}
                        ,{label:'竖版四宫格', value:'SBSGG'},{label:'田字四宫格', value:'TZSGG'},{label:'小方卡', value:'XFK'},
                        {label:'3寸仿真拍立得', value:'3CFZFP'},{label:'4寸仿真拍立得', value:'4CFZFP'},{label:'5寸仿真拍立得', value:'5CFZFP'}],
      multipleSelection: [],
      classifyVisible: false,
      classifyData:{}
    }
  },
  components: {
    // AddOrUpdate
  },
  created () {
    this.getDataList()
    this.getSeriesList()
  },
  mounted () {
  },
  methods: {
    openClassifyDialog(){
      this.classifyVisible = true
    },
    selectSeries(value){
      this.personList = []
      this.getPersonList(value)
    },
    classifySubmit(){
      if(this.multipleSelection.length === 0){
        return
      }else{
        this.classifyData.prodTemplateList = this.multipleSelection
      }
      this.$http({
        url: this.$http.adornUrl('/prod/prodTemplate/saveBatchToCardCommonLib'),
        method: 'post',
        data: this.$http.adornData(this.classifyData)
      }).then(({data}) => {
        this.$message({
          message: this.$i18n.t('publics.operation'),
          type: 'success',
          duration: 200,
          onClose: () => {
            this.classifyVisible = false
          }
        })
      })
    },
    getPersonList (seriesId) {
      this.$http({
        url: this.$http.adornUrl('/cardLib/cardPerson/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign({
              size: 100,
            seriesId: seriesId
            },
          )
        )
      }).then(({data}) => {
        this.personList = data.records
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    previewPic(row){
      this.previewPicUrl = this.resourcesUrl + row.templatePic
      this.previewPicDialogVisible = true;
    },
    getSeriesList () {
      this.$http({
        url: this.$http.adornUrl('/prod/prodTemplateSeries/page'),
        method: 'get',
        params: this.$http.adornParams(
                Object.assign({
                          size: 100,
                          shopId: 1
                        },
                )
        )
      }).then(({data}) => {
        this.seriesList = data.records
      })
    },
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/prod/prodTemplate/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,
            shopId: 1,
          },
            this.searchForm
          )
        )
      }).then(({data}) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    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('/prod/prodTemplate/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 100,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    resetForm () {
      this.searchForm = {
        templateStyleId: null,
        templateSeriesId: null
      }
    },
    // 刷新回调
    refreshChange () {
      this.getDataList(this.page)
    },
    searchChange () {
      // this.searchForm = params
      this.getDataList(this.page)
    },
    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style lang="scss">
.mod-prod-prodTemplate {
}
</style>
