<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="产品型号" prop="productModel">
        <el-input
          v-model="queryParams.productModel"
          placeholder="请输入产品型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jarvis:productJdConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jarvis:productJdConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jarvis:productJdConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleInitData"
          v-hasPermi="['jarvis:productJdConfig:init']"
        >初始化数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productJdConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品型号" align="center" prop="productModel" :show-overflow-tooltip="true" />
      <el-table-column label="京东链接" align="center" prop="jdUrl" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link :href="scope.row.jdUrl" target="_blank" type="primary">{{ scope.row.jdUrl }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="佣金（收取）" align="center" prop="commissionReceive" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.commissionReceive ? '¥' + scope.row.commissionReceive : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佣金（支付）" align="center" prop="commissionPay" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.commissionPay ? '¥' + scope.row.commissionPay : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参考售价(直款)" align="center" prop="sellingPriceDirect" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.sellingPriceDirect != null ? '¥' + scope.row.sellingPriceDirect : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参考售价(闲鱼)" align="center" prop="sellingPriceXianyu" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.sellingPriceXianyu != null ? '¥' + scope.row.sellingPriceXianyu : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jarvis:productJdConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jarvis:productJdConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改产品京东配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="产品型号" prop="productModel">
          <el-input v-model="form.productModel" placeholder="请输入产品型号" :disabled="form.productModel && !isAdd" />
        </el-form-item>
        <el-form-item label="京东链接" prop="jdUrl">
          <el-input v-model="form.jdUrl" type="textarea" :rows="3" placeholder="请输入京东链接" />
        </el-form-item>
        <el-form-item label="佣金（收取）" prop="commissionReceive">
          <el-input-number v-model="form.commissionReceive" :precision="2" :step="0.1" :min="0" placeholder="请输入佣金（收取）" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="佣金（支付）" prop="commissionPay">
          <el-input-number v-model="form.commissionPay" :precision="2" :step="0.1" :min="0" placeholder="请输入佣金（支付）" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="参考售价(直款)" prop="sellingPriceDirect">
          <el-input-number v-model="form.sellingPriceDirect" :precision="2" :step="1" :min="0" placeholder="F 单直款渠道默认售价" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="参考售价(闲鱼)" prop="sellingPriceXianyu">
          <el-input-number v-model="form.sellingPriceXianyu" :precision="2" :step="1" :min="0" placeholder="F 单闲鱼标价（系统会×0.984）" style="width: 100%;" />
        </el-form-item>
      </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 { listProductJdConfig, getProductJdConfig, delProductJdConfig, addProductJdConfig, updateProductJdConfig, initDefaultData } from "@/api/jarvis/productJdConfig";

export default {
  name: "ProductJdConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品京东配置表格数据
      productJdConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否为新增
      isAdd: false,
      // 查询参数
      queryParams: {
        productModel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productModel: [
          { required: true, message: "产品型号不能为空", trigger: "blur" }
        ],
        jdUrl: [
          { required: true, message: "京东链接不能为空", trigger: "blur" }
        ],
        commissionReceive: [
          { required: true, message: "佣金（收取）不能为空", trigger: "blur" }
        ],
        commissionPay: [
          { required: true, message: "佣金（支付）不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品京东配置列表 */
    getList() {
      this.loading = true;
      listProductJdConfig(this.queryParams).then(response => {
        this.productJdConfigList = response.data || [];
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productModel: null,
        jdUrl: null,
        commission: 0,
        commissionReceive: 0,
        commissionPay: 0,
        sellingPriceDirect: undefined,
        sellingPriceXianyu: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productModel);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isAdd = true;
      this.open = true;
      this.title = "添加产品京东配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isAdd = false;
      const productModel = row.productModel || this.ids[0];
      getProductJdConfig(productModel).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品京东配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isAdd) {
            addProductJdConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateProductJdConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productModels = row.productModel ? [row.productModel] : this.ids;
      this.$modal.confirm('是否确认删除产品型号为"' + productModels + '"的数据项？').then(() => {
        return delProductJdConfig(productModels.join(','));
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 初始化数据按钮操作 */
    handleInitData() {
      this.$modal.confirm('是否确认初始化默认产品数据？').then(() => {
        return initDefaultData();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("初始化成功");
      }).catch(() => {});
    }
  }
};
</script>

