<template>
  <div class="app-container">
    <div v-show="show">
      <el-form :model="queryParams"
               ref="queryForm"
               :inline="true"
               v-show="showSearch"
               label-width="80px"
               size="small"
               class="ry_form"
      >
        <el-row>
          <el-form-item label="商品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入商品名称"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="分类" prop="" >
            <product-category-select v-model="queryParams.productCategoryId" placeholder="请选择分类"/>
          </el-form-item>
          <el-form-item label="申请场馆" prop="storeName">
            <el-input
              v-model="queryParams.storeName"
              placeholder="请输入场馆名称"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="商品编码" prop="outProductId">
            <el-input
              v-model="queryParams.outProductId"
              placeholder="请输入商品编码"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item style="float: right">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="申请状态" prop="applyStatus">
            <el-select v-model="queryParams.applyStatus" placeholder="请选择申请状态" clearable size="small">
              <el-option
                v-for="dict in applyStatus"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="展示状态" prop="showFlag">
            <DictRadio v-model="queryParams.showFlag" @change="handleQuery" size="small"
                       :radioData="showFlag"/>
          </el-form-item>
        </el-row>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <span style="color: #999; font-size: 12px;margin-left: 20px " >
            * 展示状态切换为定时任务，根据申请展示时间自动展示或结束展示，无需手动操作
        </span>
      </el-row>
      <el-table
        v-loading="loading"
        :data="pmsProductShowList"
        border
        :header-cell-style="{'text-align': 'center'}"
        :cell-style="{'text-align': 'center'}"
      >
        <el-table-column prop="productName" label="商品信息"  show-overflow-tooltip >
          <template #default="{ row }">
            <span>商品名称：{{ row.productName}}</span><br/>
            <span>商品编号：{{ row.outProductId}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="tenantName" label="租户场馆信息"  show-overflow-tooltip >
          <template #default="{ row }">
            <span>租户名称：{{ row.tenantName}}</span><br/>
            <span>场馆名称：{{ row.storeName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="productCategoryName" label="上架分类" width="180" show-overflow-tooltip>
          <template #default="{ row }">
            <span>默认：场馆商品</span><br/>
            <span>商城上架分类：{{ row.productCategoryName?row.productCategoryName:"待定" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="showFlag" label="展示标识"  align="center" >
          <template #default="{ row }">
            <el-tag :type="row.showFlag === '1' ? 'success' : 'info'" size="small">
              {{ row.showFlag === '1' ? '展示' : '不展示' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="applyStatus" label="申请状态"  align="center">
          <template #default="{ row }">
            <el-tag
              v-if="row.applyStatus === '0'"
              type="warning"
              size="small"
            >待审核</el-tag>
            <el-tag
              v-else-if="row.applyStatus === '1'"
              type="success"
              size="small"
            >审核通过</el-tag>
            <el-tag
              v-else-if="row.applyStatus === '2'"
              type="danger"
              size="small"
            >审核拒绝</el-tag>
            <el-tag
              v-else-if="row.applyStatus === '3'"
              type="primary"
              size="small"
            >展示中</el-tag>
            <el-tag
              v-else-if="row.applyStatus === '4'"
              type="info"
              size="small"
            >展示期结束</el-tag>
            <span v-else>{{ row.applyStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="showStartTime" label="展示开始时间" width="120" align="center" >
          <template #default="{ row }">
            <span>{{ row.showStartTime?row.showStartTime.substr(0,10):"" }}</span><br/>
          </template>
        </el-table-column>
        <el-table-column prop="showEndTime" label="展示结束时间" width="120" align="center" >
        <template #default="{ row }">
          <span>{{ row.showEndTime?row.showEndTime.substr(0,10):"" }}</span><br/>
        </template>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间"  align="center" >
          <template #default="{ row }">
            <span>{{ row.applyTime?row.applyTime.substr(0,10):"" }}</span><br/>
            <span>{{ row.applyTime?row.applyTime.substr(11,8):"" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="reviewTime" label="审核时间"  align="center" >
          <template #default="{ row }">
            <span>{{ row.reviewTime?row.reviewTime.substr(0,10):"" }}</span><br/>
            <span>{{ row.reviewTime?row.reviewTime.substr(11,8):"" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="展示排序"  align="center" />
        <el-table-column label="操作" class-name="small-padding fixed-width">
          <template  #default="{ row }">
            <el-button
              v-if="row.applyStatus === '0' || row.applyStatus === '2'  "
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(row)"
            >操作
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="productDetail(row)"
            >商品详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <InBody v-show="total>0">
        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </InBody>
    </div>
    <!-- 添加或修改品牌管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-one">
        <el-row style="padding-bottom: 10px">
          <el-form-item label="商品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="商品名称" disabled="disabled"/>
          </el-form-item>
          <el-form-item label="场馆名称" prop="storeName">
            <el-input v-model="form.storeName" placeholder="场馆名称" disabled="disabled"/>
          </el-form-item>
          <el-form-item label="展示时间" prop="" disabled="disabled">
            {{this.form.showStartTime?form.showStartTime.substring(0,10):""}}
            至
            {{this.form.showEndTime?form.showEndTime.substring(0,10):""}}
          </el-form-item>
          <hr style="color: #999999"/>
        </el-row>
        <el-row>
          <el-form-item label="审核结果" prop="applyStatus">
            <DictRadio v-model="form.applyStatus"  size="small"
                       :radioData="applyStatusFlag"/>
          </el-form-item>
        </el-row>
        <el-row v-if="form.applyStatus==1">
          <el-form-item label="展示分类" prop="showCategory" >
            <product-category-select v-model="form.showCategory" placeholder="请选择分类"/>
          </el-form-item>

          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" />
          </el-form-item>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listProductShow, refuseProductShow,passProductShow} from "@/api/pms/productShow";
import {mapGetters} from "vuex";
import ProductCategorySelect from "@/views/components/ProductCategorySelect.vue";
import ProductSelect from "@/views/mall/publishProduct/productSelect.vue";
import {number} from "echarts/lib/export";


export default {
  name: "publishProduct",
  components: {ProductSelect,ProductCategorySelect},
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applyStatus: [
          {required: true, message: "选择审核结果", trigger: "change"},
        ],
      },
      show: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      pmsProductShowList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

      },
      showFlag:[
        {
        value: '0',
        label: '未展示'
      }, {
        value: '1',
        label: '展示'
      }],
      applyStatusFlag:[ {
        value: '1',
        label: '审核通过'
      },  {
        value: '2',
        label: '审核拒绝'
      },
      ],
      applyStatus:[
        {//申请状态
        value: '0',
        label: '待审核'
      }, {
        value: '1',
        label: '审核通过'
      },
        {
          value: '2',
          label: '审核拒绝'
        },
        {
          value: '3',
          label: '展示中'
        },
        {
          value: '4',
          label: '展示期结束'
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  computed:{
    ...mapGetters(['userId']),
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      const query = {...this.queryParams, pageNum: undefined, pageSize: undefined};
      const pageReq = {page: pageNum - 1, size: pageSize};

      listProductShow(query, pageReq).then(response => {

        const { content, totalElements } = response
        this.pmsProductShowList = content;
        this.total =Number(totalElements) ;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "审核";
      this.form =  JSON.parse(JSON.stringify(row));
    },
    productDetail(row){
      this.$router.push({ path: '/product/edit', query: { id: row.productId ,readOnly:true} });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applyStatus == 2) {
            refuseProductShow(this.form).then(response => {
              this.$modal.msgSuccess("审核拒绝");
              this.open = false;
              this.getList();
            });
          }
          if (this.form.applyStatus == 1)
          {
            this.form.showCategory= Number(this.form.showCategory);
            passProductShow(this.form).then(response => {
              this.$modal.msgSuccess("审核通过");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel(){
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
    },
  }
};
</script>
