<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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="['mall:mallOrderBack: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="['mall:mallOrderBack: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="['mall:mallOrderBack:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['mall:mallOrderBack:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallOrderBackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="订单ID" align="center" prop="orderId" />-->
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="会员名称" align="center" prop="memberUserName" />
      <el-table-column label="退款原因" align="center" prop="reason" />
      <el-table-column label="状态" align="center" prop="status" >
      <template slot-scope="scope">
        <dict-tag :options="statusOptions" :value="scope.row.status"/>
      </template>
      </el-table-column>
      <el-table-column label="拒绝原因" align="center" prop="refuse" />
      <el-table-column label="退款金额" align="center" prop="price" />
      <el-table-column label="申请退款时间" align="center" prop="createAt" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status === '0'"
            v-hasPermi="['mall:mallOrderBack:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallOrderBack:remove']"
          >删除</el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-view"
          @click="handleCheckDetail(scope.row)"
        >查看详情</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"
    />

    <!-- 添加或修改退款单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退款原因" prop="reason">
          <el-input v-bind:readonly="1" v-model="form.reason" placeholder="请输入退款原因" />
        </el-form-item>
        <el-form-item label="处理结果" prop="status">
          <el-radio-group v-model="form.status">
<!--            <el-radio-->
<!--              v-for="dict in statusOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictValue"-->
<!--            >{{dict.dictLabel}}</el-radio>-->
            <el-radio label="0">退款申请中</el-radio>
            <el-radio label="1">同意退款</el-radio>
            <el-radio label="3">拒绝退款</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="拒绝原因" prop="refuse" v-if="form.status==3">

          <el-input v-model="form.refuse" placeholder="请输入拒绝原因" />
        </el-form-item>
        <el-form-item label="退款金额" prop="price">
          <el-input v-bind:readonly="1" v-model="form.price" placeholder="请输入退款金额" />
        </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>

    <!--    退货订单详情-->
    <el-dialog  :visible.sync="menuRoleVisible" append-to-body>
      <el-form label-width="80px">
        <el-row>
          <el-col :span="parseInt(12)">
            <el-form-item label="收件人" prop="receiverName">
              <el-input v-model="form.receiverName" placeholder="请输入收件人" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="parseInt(12)">
            <el-form-item label="电话" prop="receiverPhone">
              <el-input v-model="form.receiverPhone" placeholder="请输入电话" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="parseInt(8)">
            <el-form-item label="省份" prop="receiverProvince">
              <el-input v-model="form.receiverProvince" placeholder="请输入省份" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="parseInt(8)">
            <el-form-item label="城市" prop="receiverCity">
              <el-input v-model="form.receiverCity" placeholder="请输入城市" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="parseInt(8)">
            <el-form-item label="区县" prop="receiverArea">
              <el-input v-model="form.receiverArea" placeholder="请输入区县" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="地址" prop="receiverAddress">
            <el-input v-model="form.receiverAddress" placeholder="请输入地址" disabled />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="parseInt(12)">
            <el-form-item label="退货总价" prop="totalPrice">
              <el-input v-model="form.totalPrice" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="parseInt(12)">
            <el-form-item label="退货总数" prop="totalCount">
              <el-input v-model="form.totalCount" placeholder="" disabled />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <order-back-goods v-if="menuRoleVisible" :page-type="1" :order-back-id="this.orderGoodsOrdeBackrId"  ref="OrderBackGoods"></order-back-goods>
    </el-dialog>
  </div>
</template>

<script>
import { listMallOrderBack, getMallOrderBack, delMallOrderBack, addMallOrderBack, updateMallOrderBack, exportMallOrderBack } from "@/api/mall/mallOrderBack";
import {getMallOrderOrder, getOrderBackInfo} from "@/api/mall/mallOrderOrder";
import OrderGoods from "@/views/mall/mallOrderOrder/orderGoods";
import OrderBackGoods from "./orderBackGoods";

export default {
  name: "MallOrderBack",
  components:{
    OrderBackGoods
  },
  data() {
    return {
      menuRoleVisible : false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款单表格数据
      mallOrderBackList: [],
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "请选择处理结果", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("mall_order_back").then(response => {
      this.statusOptions = response.data;
  });
  },
  methods: {
    handleCheckDetail(row){
      let orderBackId = row.id;
      this.form = {}
      let orderId = 0;
      getOrderBackInfo(orderBackId).then(response => {
        this.form = response.data;
        orderId = response.data.id;
      });
      this.menuRoleVisible = true;
      this.orderGoodsOrdeBackrId = orderBackId
    },

    /** 查询退款单列表 */
    getList() {
      this.loading = true;
      listMallOrderBack(this.queryParams).then(response => {
        this.mallOrderBackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        reason: null,
        status: "0",
        refuse: null,
        refundNo: null,
        price: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退款单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMallOrderBack(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退款单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMallOrderBack(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallOrderBack(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除退款单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMallOrderBack(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有退款单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMallOrderBack(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
