<template>
  <div class="app-container">

    <div class="top-taber" style="margin-top: 10px">
      <el-radio-group v-model="otaType" size="small" @change="handleQuery">
        <el-radio-button label="1">美团</el-radio-button>
        <el-radio-button label="2">携程</el-radio-button>
      </el-radio-group>
    </div>

    <el-row :gutter="10" class="mb8" style="margin-top: 30px">
      <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="商品名称" align="center" prop="goodsName" />
      <el-table-column label="类型" align="center" prop="goodsName" />
      <el-table-column label="市场价" align="center" prop="otaPrice">
        <template slot-scope="scope">
          <span>￥{{ scope.row.otaPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售价格" align="center" prop="otaPrice">
        <template slot-scope="scope">
          <span>￥{{ scope.row.otaPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算价" align="center" prop="otaPrice">
        <template slot-scope="scope">
          <span>￥{{ scope.row.otaPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status != '1'"
            @click="handleUpdate(scope.row,scope.index)"
            v-hasPermi="['otaMr:otaMr:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status != '1'"
            @click="handleDelete(scope.row,scope.index)"
            v-hasPermi="['otaMr:otaMr:delete']"
          >删除</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>


    <el-dialog
      title="填写密钥"
      :visible.sync="visibleStatus"
      width="600px"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <div class="dialog">
        <el-form :model="otaForm" ref="otaForm" :rules="otaRules" label-width="120px">
          <el-form-item label="供应商id" prop="name">
            <el-input
              v-model="otaForm.name"
              placeholder=""
              clearable
              style="width: 100%;"
            />
          </el-form-item>
          <el-form-item label="client id" prop="otaKey">
            <el-input
              v-model="otaForm.otaKey"
              placeholder=""
              clearable
              style="width: 100%;"
            />
          </el-form-item>
          <el-form-item label="client secret" prop="otaValue">
            <el-input
              v-model="otaForm.otaValue"
              placeholder=""
              clearable
              style="width: 100%;"
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visibleStatus = false">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm"
          v-loading.fullscreen.lock="otaLoading"
          element-loading-text="提交中..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
        >
          <span v-if="otaLoading">提交中...</span>
          <span v-else>保存</span>
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { pageList, deleteById, getOtaConfig, otaSave } from '@/api/otaMr/otaMr'
import addAndEdit from "./dialog/addAndEdit.vue";
import dataEdit from "./dialog/dataEdit.vue";

export default {
  name: "agreement",
  dicts: ['agreement_type'],
  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,
        ota: '1'
      },
      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;
      this.queryParams.ota = this.otaType
      pageList(this.addDateRange(this.queryParams, this.dateRange))
      .then(response => {
          this.dataList = response.data.rows;
          this.total = response.data.total;
          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(() => {});
    },
    /** 查看按钮操作 */
    handleKey(obj, type) {
      this.visibleStatus = true
      this.visibleType = type;
      this.otaForm = {};
      this.getOtaConfigApi();
    },
    /** 查询列表 */
    getOtaConfigApi() {
      getOtaConfig({ota: this.otaType})
        .then(response => {
          let dataList = response.data;
          dataList.forEach(item => {
            this.$set(this.otaForm, item.otaKey, item.otaValue);
          })
        });
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    submitForm() {
      this.$refs["otaForm"].validate(async (valid) => {
        if (valid) {
          try {
            let postList = [];
            for (const key in this.otaForm) {
              postList.push({
                ota: this.otaType,
                otaKey: key,
                otaValue: this.otaForm[key]
              })
            }
            this.otaLoading = true;
            const { code } = await otaSave(postList);
            if (code === 200) {
              this.$message.success("操作成功!");
              this.getList();
              this.visibleStatus = false;
            }
          } catch (error) {
          } finally {
            this.otaLoading = false;
          }
        }
      });
    },
  }
};
</script>
