<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名" prop="companyId">
          <el-select filterable  v-model="queryParams.companyId" placeholder="请选择公司名"  @change="companyChange" clearable size="small">
              <el-option
                v-for="item in companys"
                :key="item.companyId"
                :label="item.companyName"
                :value="item.companyId"
              />
        </el-select>
      </el-form-item>

     <el-form-item >
          <treeselect style="width: 220px" :clearable="false"  v-model="queryParams.deptId " clearable  :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
     </el-form-item>

      <el-form-item label="订单编号" prop="orderCode">
        <el-input  v-model="queryParams.orderCode"  placeholder="请输入订单编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="companyUserNickName">
        <el-input
          v-model="queryParams.companyUserNickName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select   v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small" >
         <el-option
                v-for="item in orderOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="退款类型" prop="refundType">
       <el-select v-model="queryParams.refundType" placeholder="请选择退款类型" clearable size="small">
               <el-option v-for="dict in refundTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
       </el-select>
      </el-form-item>

      <el-form-item label="店铺" prop="storeId">
       <el-select v-model="queryParams.storeId" placeholder="店铺" clearable size="small">
               <el-option v-for="dict in storeOPtions"  :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
       </el-select>
      </el-form-item>

      <el-form-item label="售后状态" prop="salesStatus">
        <el-select v-model="queryParams.salesStatus" placeholder="订单状态" clearable size="small">
                <el-option v-for="dict in afterSalesStatusOptions"  :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>

      <el-form-item label="退回单号" prop="deliverySn">
          <el-input
            v-model="queryParams.deliverySn"
            placeholder="物流/退回物流单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"/>
       </el-form-item>

      <el-form-item label="用户电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入用户电话" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="收货电话" prop="moble">
        <el-input v-model="queryParams.moble" placeholder="请输入收货电话" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="提交时间" prop="createTime">
        <el-date-picker
          style="width:205.4px"
          clearable size="small"
          v-model="createTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:storeAfterSales:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs type="card" v-model="actName" @tab-click="handleClickX">
      <el-tab-pane label="全部订单" name="10"></el-tab-pane>
      <el-tab-pane v-for="(item,index) in salesStatusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table height="570" v-loading="loading" border :data="salesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderCode" width="180px"/>
      <el-table-column label="店铺" align="center" prop="storeName" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="所属员工" align="center" prop="companyUserNickName" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="疗法名称" align="center" prop="packageName" />
      <el-table-column label="退款类型" align="center" prop="refundType" >
            <template slot-scope="scope">
              <dict-tag :options="refundTypeOptions" :value="scope.row.refundType"/>
            </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status"  width="130px">
        <template slot-scope="scope">
          <dict-tag :options="salesStatusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="售后状态" align="center" prop="salesStatus" >

        <template slot-scope="scope">
          <dict-tag :options="afterSalesStatusOptions" :value="scope.row.salesStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
        <template slot-scope="scope">
          <dict-tag :options="orderOptions" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="reasons"  width="100px"/>
      <el-table-column label="说明" align="center" prop="explains" />
      <el-table-column label="物流单号" align="center" prop="deliveryId" />
      <el-table-column label="物流退回单号" align="center" prop="deliverySn" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120px">
        <template slot-scope="scope">
           <el-button
            size="mini"
            type="text"
            @click="handledetails(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-drawer
        :with-header="false"
        size="75%"
         :title="show.title" :visible.sync="show.open">
     <storeAfterSalesDetails  ref="Details" />
   </el-drawer>
  </div>
</template>

<script>
import { listSales, getSales, delSales, addSales, updateSales, exportSales } from "@/api/his/storeAfterSales";
import {listStore} from "@/api/his/storeProduct";
import storeAfterSalesDetails from '@/views/components/his/storeAfterSalesDetails.vue';
import { getCompanyList } from "@/api/company/company";
import { treeselect } from "@/api/company/companyDept";
import Treeselect from "@riophae/vue-treeselect";
import { getTask } from "@/api/common";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  watch: {
    // 监听deptId
    'deptId': 'currDeptChange'
  },
  name: "Sales",
  components: { storeAfterSalesDetails,Treeselect},
  data() {
    return {
      actName:"10",
      show:{
              title:"售后详情",
              open:false,
            },
      // 遮罩层
      loading: true,
      companyId:undefined,
      deptId:undefined,
      companys:[],
      deptOptions:[],
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      dateRange:[],
      createTimeRange:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后记录表格数据
      salesList: [],
      storeOPtions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: null,
        orderCode: null,
        refundAmount: null,
        refundType: null,
        reasons: null,
        explains: null,
        explainImg: null,
        deliveryCode: null,
        deliverySn: null,
        deliveryName: null,
        status: null,
        salesStatus: null,
        orderStatus: null,
        isDel: null,
        userId: null,
        consignee: null,
        phoneNumber: null,
        phone:null,
        address: null,
        companyId: null,
        companyUserId: null,
        isPackage: null,
        packageJson: null,
        moble:null,
      },
      // 表单参数
      form: {},
      orderOptions:[],
      refundTypeOptions:[],
      salesStatusOptions:[],
      afterSalesStatusOptions:[],
    };
  },
  created() {
    this.getList();
    getCompanyList().then(response => {
      this.companys = response.data;
         if(this.companys!=null&&this.companys.length>0){
          this.companyId=this.companys[0].companyId;
          this.getTreeselect();
        }
        this.companys.push({companyId:"-1",companyName:"无"})
    });
    this.getstoreList();
    this.getDicts("sys_order_status").then(response => {
        this.orderOptions = response.data;
      });
    this.getDicts("sys_refund_type").then(response => {
        this.refundTypeOptions = response.data;
      });
    this.getDicts("sys_sales_status").then(response => {
        this.salesStatusOptions = response.data;
      });
    this.getDicts("sys_after_sales_status").then(response => {
        this.afterSalesStatusOptions = response.data;
      });
  },
  methods: {
    getstoreList() {
      listStore().then(response => {
        this.storeOPtions = response.rows;
      });
    },
    handleClickX(tab, event) {
     if(tab.name=="10"){
       this.queryParams.status=null;
     }else{
       this.queryParams.status=tab.name;
     }
      this.handleQuery();
    },
    handledetails(row){
         this.show.open=true;
         setTimeout(() => {
              this.$refs.Details.getDetails(row.id,row.nickName,row.storeName);
         }, 1);
     },

    /** 查询售后记录列表 */
    getList() {
      this.loading = true;
      if(this.createTimeRange!=null&&this.createTimeRange.length==2){
        this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
      }
      else{
        this.queryParams.createTimeRange=null;
      }
      listSales(this.queryParams).then(response => {
        this.salesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        orderId: null,
        refundAmount: null,
        refundType: null,
        reasons: null,
        explains: null,
        explainImg: null,
        deliveryCode: null,
        deliverySn: null,
        deliveryName: null,
        status: 0,
        salesStatus: 0,
        orderStatus: 0,
        createTime: null,
        isDel: null,
        userId: null,
        consignee: null,
        phoneNumber: null,
        address: null,
        companyId: null,
        companyUserId: null,
        isPackage: null,
        packageJson: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.createTime=null;
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
      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
      getSales(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) {
            updateSales(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSales(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 delSales(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
		var that=this;
      if(this.createTimeRange!=null&&this.createTimeRange.length==2){
        this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
      }
      else{
        this.queryParams.createTimeRange=null;
      }
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有售后记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSales(queryParams);
        }).then(response => {
          if(response.code==200){
          that.msgSuccess(response.msg);
          that.taskId=response.data;
          that.time=setInterval(function(){
            //查订单
            getTask(that.taskId).then(res => {
              if(res.data.status==1){
                that.exportLoading = false;
                clearTimeout(that.time)
                that.time=null;
                that.download(res.data.fileUrl);
              }
            });
          },10000);
        }
        }).catch(() => {});
    },
    getTreeselect() {
        var that=this;
        var param={companyId:this.companyId}
        treeselect(param).then((response) => {
          this.deptOptions = response.data;
          if(response.data!=null&&response.data.length>0){
            //this.queryParams.deptId=response.data[0].id;
          }
        });
    },
    companyChange(val){
      this.companyId=val;
      this.getTreeselect();
    },
     currDeptChange(val){
          this.queryParams.deptId=val;
          this.getList();
    },
  }
};
</script>
