<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
              type="primary"
              plain
              icon="el-icon-key"
              size="mini"
              @click="handleKey"
              v-hasPermi="['otaMr:otaMr:key']"
            >填写密钥</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['otaMr:otaMr:add']"
        >添加商品</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" border>
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <el-table-column label="门店POIID" align="center" prop="poiId" />
      <el-table-column label="商品类目ID" align="center" prop="categoryId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tiktok_category" :value="scope.row.categoryId"/>
        </template>
      </el-table-column>
      <el-table-column label="演出厅" align="center" prop="performHallName" />
      <el-table-column label="剧目名称" align="center" prop="performName" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="时刻信息" align="center" prop="goodsName1">
        <template slot-scope="scope">
          <span>{{ scope.row.times }}</span>
        </template>
      </el-table-column>
      <el-table-column label="抖音审核状态" align="center" prop="process">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tiktok_process" :value="scope.row.process"/>
        </template>
      </el-table-column>
      <el-table-column label="商品上架/下架" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tiktok_online" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="最新上架时间" align="center" prop="goodsName3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handlePush(scope.row,scope.index)"
            v-if="(scope.row.status == 0 || scope.row.status == 1) && scope.row.process == 4"
            v-hasPermi="['otaMr:tiktok:push']"
          >{{ scope.row.status == 0 ? '上线' : '下线' }}</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleInventory(scope.row,scope.index)"
            v-if="scope.row.status == 1"
            v-hasPermi="['otaMr:tiktok:push']"
          >日历库存推送</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row,scope.index)"
            v-hasPermi="['otaMr:tiktok:scheduling']"
          >排期推送</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑弹框 -->
    <add-and-edit
      ref="addAndEdit"
      :dict="dict"
      @getList="getList"
    />

    <!-- 编辑商品弹框 -->
    <data-edit
      ref="dataEdit"
      @getList="getList"
      :dict="dict"></data-edit>

  </div>
</template>

<script>

import { pageList, updateById,stockUpdateById } from '@/api/otaMr/tiktok'
import addAndEdit from "./dialog/addAndEdit.vue";
import dataEdit from "./dialog/dataEdit.vue";

export default {
  name: "agreement",
  dicts: ['tiktok_process','tiktok_online','tiktok_category'],
  components: { addAndEdit, dataEdit },
  data() {
    return {
      // 遮罩层
      loading: true,
      otaLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: null,
      // 弹出层标题
      title: "",

      otaType: '1',

      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      visibleStatus: false,
      newObj: {},
      visibleType: '',
      otaForm: {},
      otaRules: {
        name: [{ required: true, message: "请输入供应商id", trigger: ["change","blur"] }],
        otaKey: [{ required: true, message: "请输入client id", trigger: ["change","blur"] }],
        otaValue: [{ required: true, message: "请输入client secret", trigger: ["change","blur"] }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      pageList(this.addDateRange(this.queryParams, this.dateRange))
      .then(response => {
          this.dataList = response.data.rows;
          this.total = response.data.total?Number(response.data.total):0;
          this.loading = false;
        }
      ).catch(()=>{
        this.dataList = []
        this.total = 0;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.dataList = [];
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs["addAndEdit"].openDialog("新增数据", this.queryParams);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs["dataEdit"].openDialog("修改数据", row);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除商品名称为"' + row.goodsName + '"的数据项？').then(function() {
        return deleteById(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 推送 */
    handlePush(row) {
      this.$modal.confirm(`是否确认${ row.status == 0 ? '上线': row.status == 1 ?'下线':''}此商品吗？`).then(function() {
        return updateById({
          id: row.id,
          status: row.status == 0 ? 1 : 0
        });
      }).then(() => {
        this.$modal.msgSuccess(`${ row.status == 0 ? '上线': row.status == 1 ?'下线':''}成功`);
        this.getList();
      }).catch(() => {

      });
    },
    handleInventory(row) {
      this.$modal.confirm(`是否确认推送此商品的日历库存？`).then(function() {
        return stockUpdateById({
          id: row.id,
        });
      }).then(() => {
        this.$modal.msgSuccess(`推送成功`);
        this.getList();
      }).catch(() => {

      });
    },
  }
};
</script>
