<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-form-item label="订餐时间:">
         <el-date-picker
         style="margin-top:5px;"
          v-model="dateRange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="餐别:" >
         <el-select v-model="queryParams.mType" style="width: 160px" placeholder="请选择餐别" clearable size="small">
          <el-option :label="item.value"  :value="item.value" v-for="(item,index) in mTypeList" :key="index" />
        </el-select>
      </el-form-item>

      <el-form-item class="right">
        <el-button plain size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
      <br>
      <el-form-item>
        <el-button
          plain
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="adminList" border>
      <el-table-column label="序号" align="left" prop="" show-overflow-tooltip >
        <template slot-scope="scope">
         {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="订餐人" align="left" prop="orderUserName"  show-overflow-tooltip />
      <el-table-column label="订餐人电话" align="left" prop="phone"  show-overflow-tooltip />
      <el-table-column label="订餐时间" align="left" prop="orderTime" show-overflow-tooltip width="160" >
        <template slot-scope="scope">
          <span>{{scope.row.orderTime ? scope.row.orderTime : ''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="餐别" align="left" prop="mType" show-overflow-tooltip >
        <template slot-scope="scope">
          <span >{{scope.row.mType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格(元)" align="left" prop="totalMoney" show-overflow-tooltip >
        <template slot-scope="scope">
          <span>{{Number(scope.row.totalMoney).toFixed(2) || '0.00'}}</span>
        </template>
      </el-table-column>

      <el-table-column label="接单人" align="left" prop="receiveUserName"  show-overflow-tooltip />
      <el-table-column label="接单时间" align="left" prop="receiveTime" show-overflow-tooltip width="160">
         <template slot-scope="scope">
          <span>{{scope.row.receiveTime ? scope.row.receiveTime : ''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="送单人" align="left" prop="sendUserName"  show-overflow-tooltip />
       <el-table-column label="流程状态" align="left" prop="orderStatus" show-overflow-tooltip >
         <template slot-scope="scope">
          <span>{{scope.row.orderStatus == "0" ? "待接单" : scope.row.orderStatus == "1" ? "配餐中" : scope.row.orderStatus == "2" ? "送餐中" :  "已完成"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="left" prop="sendEndTime" show-overflow-tooltip width="160">
        <template slot-scope="scope">
          <span>{{scope.row.sendEndTime ? scope.row.sendEndTime : ''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="详情" align="left" prop="topRadio" show-overflow-tooltip >
        <template slot-scope="scope" >
          <el-button
            type="text"
            class="hongs"
            size="small"
            @click="getOne(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" >
        <template slot-scope="scope" >
          <el-button
            type="text"
            size="small"
            class="hongs"
            :disabled="scope.row.orderStatus != 0"
            @click="handleUpdate(scope.row.id,1)"
          >接单</el-button>
           <el-button
           :disabled="scope.row.orderStatus != 1"
            type="text"
            size="small"
            class="hongs"
            @click="handleUpdate(scope.row.id,2)"
          >送餐</el-button>
           <el-button
           :disabled="scope.row.orderStatus != 2"
            type="text"
            size="small"
            class="hongs"
            @click="handleUpdate(scope.row.id,3)"
          >完成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 对话框 -->
    <el-dialog   :title="title" :visible.sync="open" width="750px" append-to-body :before-close="cancel" :close-on-click-modal="false"  >
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="12"><div class="grid-content bg-purple">订餐人: {{details.row.orderUserName}}</div></el-col>
        <el-col :span="12"><div class="grid-content bg-purple-light">餐别: {{details.row.mType}}</div></el-col>
      </el-row>
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="12"><div class="grid-content bg-purple">送餐地址: {{details.row.address}}</div></el-col>
        <el-col :span="12"><div class="grid-content bg-purple">下单时间: {{details.row.orderTime ? details.row.orderTime : ''}}</div></el-col>
      </el-row>
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="12" style="text-align:left"><div class="grid-content title">菜品清单</div></el-col>
      </el-row>

      <el-table v-loading="loading" :data="details.dishes" height="30vh" stripe style="border:1px solid #dfe6ec;overflow-y:scroll">
        <el-table-column label="序号" align="left" prop="" show-overflow-tooltip >
          <template slot-scope="scope">
          {{scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column label="餐品" align="left" prop="mName"  show-overflow-tooltip />
        <el-table-column label="数量(份)" align="left" prop="mCount" show-overflow-tooltip />
      </el-table>
      <div slot="footer" class="dialog-footer">
         <el-button
            type="primary"
            size="small"
            :disabled="details.row.orderStatus != '0'"
            @click="handleUpdate(details.row.id,1)"
          >接单</el-button>
           <el-button
            type="primary"
            size="small"
            :disabled="details.row.orderStatus != '1'"
            @click="handleUpdate(details.row.id,2)"
          >送餐</el-button>
           <el-button
           :disabled="details.row.orderStatus != '2'"
            type="primary"
            size="small"
            @click="handleUpdate(details.row.id,3)"
          >完成</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import axios from 'axios'
import { getRecord,  updataRecord, dreOrderExport, orderDetails } from "@/api/recharge/cateringManagement";
import {  listRecharge } from "@/api/system/product";
import { getNowTime } from "@/assets/js/dataFormate.js";
export default {
  name: "admin",
  data() {
    return {
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "订单详情",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        mType: undefined,
        startTime:undefined,
        endTime:undefined,
      },
      details:{
        row:{},
        dishes:[]
      },
      mTypeList:[
        {value:"早餐"},
        {value:"午餐"},
        {value:"晚餐"},
        {value:"宵夜"},
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      if(this.dateRange.length>0){
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }
      getRecord(this.queryParams).then(response => {
          this.adminList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ids: undefined,
        name: undefined,
        topChannelName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 详情 */
    getOne(row) {
      this.details.row = row
      orderDetails({orderId:row.id}).then(response => {
        this.details.dishes = response.data
        this.open = true
      });
    },
    /** 修改订单状态 */
    handleUpdate(id,status){
      if(JSON.stringify(this.$store.state.user.user) !="{}"){
        let time = getNowTime()
        let data = {
            id:id,
            orderStatus:status,
            receiveUserName:undefined,
            receiveUserId:undefined,
            receiveTime:undefined,
            sendUserName:undefined,
            sendUserId:undefined,
            sendTime:undefined,
            sendEndTime:undefined,
        }
        if(status == "1"){
              data.receiveUserName = this.$store.state.user.user.userName
              data.receiveUserId = this.$store.state.user.user.userId
              data.receiveTime = time
          }
          if(status == "2"){
              data.sendUserName= this.$store.state.user.user.userName
              data.sendUserId = this.$store.state.user.user.userId
              data.sendTime = time
          }
          if(status == "3"){
              data.sendEndTime = time
          }
          updataRecord(data).then(response=>{
            if(status == "1"){
              this.msgSuccess("订单状态已变更为接单");
            }
            if(status == "2"){
              this.msgSuccess("订单状态已变更为送餐中");
            }
            if(status == "3"){
              this.msgSuccess("订单状态已变更为完成");
            }
            this.open = false
            this.getList()
          })
      }else{
        this.msgError("该账号不具备centerUserId参数,无法对配餐管理做修改，请改为人员账号登录");
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.startTime = undefined;
      this.queryParams.endTime = undefined;
      this.queryParams.mType = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      dreOrderExport(queryParams).then(response =>{
        // this.exportLoading = false;
        // localStorage.setItem('fileExportId', response.data)
        // this.$router.push({path:'/recharge/fileExport'})
        setTimeout(()=>{
          listRecharge({current:1,size:1,id:response.id}).then(res=>{
            var a = document.createElement("a"); //创建一个<a></a>标签
            a.href = res.data.records[0].url; // 给a标签的href属性值加上地址，注意，这里是绝对路径，不用加 点.
            a.download = "配餐记录" + getNowTime(); //设置下载文件文件名，这里加上.xlsx指定文件类型，pdf文件就指定.fpd即可
            a.style.display = "none"; // 障眼法藏起来a标签
            document.body.appendChild(a); // 将a标签追加到文档对象中
            a.click(); // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
            a.remove();
            this.exportLoading = false;
          })
        },1000)
      })

    },
  }
};
</script>
<style scoped>
.row-bg .grid-content{
  margin:10px 0;
}
.grid-content.title{
  font-size: 16px;
  font-weight: 700;
}
</style>
