<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('dingdanorder.订单号')" prop="ddId">
        <el-input
          v-model="queryParams.ddId"
          :placeholder="$t('dingdanorder.请输入订单号')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('dingdanorder.用户Id')" prop="userId">
        <el-input
          v-model="queryParams.userId"
          :placeholder="$t('dingdanorder.请输入用户Id')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('dingdanorder.订单类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('dingdanorder.请选择订单类型')" clearable>
          <el-option
            v-for="dict in dict.type.sys_order_leixing"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('dingdanorder.创建时间')">
        <el-date-picker
          v-model="queryParams.dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('dingdanorder.开始日期')"
          :end-placeholder="$t('dingdanorder.结束日期')"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('dingdanorder.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('dingdanorder.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >{{$t('dingdanorder.修改')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >{{$t('dingdanorder.导出')}}</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center"/> -->

      <el-table-column :label="$t('dingdanorder.订单号')" align="center" prop="ddId"/>
      <el-table-column :label="$t('dingdanorder.创建时间')" align="center" prop="cretim" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cretim, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('dingdanorder.订单金额')" align="center" prop="amount" />
      <el-table-column :label="$t('dingdanorder.商品价格')" align="center" prop="priceAll" />
      <el-table-column :label="$t('dingdanorder.用户留言')" align="center" prop="appealCount" />
      <el-table-column :label="$t('dingdanorder.客服处理')" align="center" prop="kefuState">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.kefuState==0" >{{generateTitle('正常')}}</el-tag>
          <el-tag v-if="scope.row.kefuState==1" type="success">{{generateTitle('已处理')}}</el-tag>
          <el-tag v-if="scope.row.kefuState==2" type="danger">{{generateTitle('未处理')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('dingdanorder.操作')" 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-hasPermi="['system:order:edit']"
          >{{$t('dingdanorder.修改')}}</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >删除</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"
    />

    <!-- 添加或修改posorder对话框 -->
    <el-dialog title="订单详情" :visible.sync="open" width="800px" append-to-body>
      <el-steps class="dibujvli" :active="orxiangq.state-4" finish-status="success">
         <el-step :title="$t('route.已完成')"></el-step>
         <el-step :title="$t('route.申请退款')"></el-step>
         <el-step :title="$t('route.同意退款')"></el-step>
         <el-step :title="$t('route.拒绝退款')"></el-step>
         <el-step :title="$t('route.客服接入')"></el-step>
       </el-steps>
      <div class="dibujvli">
         <h3>{{$t('dingdanorder.出货门店')}}</h3>
         <div class="hengxlan">
            <image-preview :src="store.image" width="80px" height="80px"/>
            <div class="jiange">
              <div class="minz">{{store.posName}}</div>
              <div>{{$t('dingdanorder.店面地址')}}:{{store.area}}{{store.address}}</div>
              <div>{{$t('dingdanorder.配送距离')}}:{{orxiangq.jvli}}km,{{$t('dingdanorder.配送费')}}:{{orxiangq.freight}}</div>
            </div>
         </div>
       </div>
      <div class="hxlan">
         <div class="youjg">
           <div>{{$t('dingdanorder.订单用户')}}</div>
           <div class="hengxlan">
             <image-preview :src="dduser.avatar" width="50px" height="50px"/>
             <div class="jiange">
               <div :class="dduser.vip==null||dduser.vip==undefined?'minz':'minz hs'">{{dduser.nickName}}</div>
               <div>{{dduser.phone}}</div>
             </div>
           </div>
         </div>
        <div class="youjg">
           <div>{{$t('dingdanorder.订单商家')}}</div>
           <div class="hengxlan">
             <image-preview :src="shus.avatar" width="50px" height="50px"/>
             <div class="jiange">
               <div class="minz">{{shus.nickName}}</div>
               <div>{{shus.phone}}</div>
             </div>
           </div>
         </div>
        <div class="youjg">
           <div>{{$t('dingdanorder.配送骑手')}}</div>
           <div class="hengxlan">
             <image-preview :src="qisus.avatar" width="50px" height="50px"/>
             <div class="jiange">
               <div class="minz">{{qisus.nickName}}</div>
               <div>{{qisus.phone}}</div>
             </div>
           </div>
         </div>
       </div>
       <div class="dingbu">
         <div class="hengxlan">
           <h3 class="texjc youjg">{{$t('dingdanorder.订单金额')}}:{{orxiangq.amount}}</h3>
           <h3 class="texjc youjg">{{$t('dingdanorder.预约配送时间')}}:{{orxiangq.delryTime||'————'}}</h3>
           <h3 class="texjc">{{orxiangq.type==2?generateTitle('堂食'):orxiangq.type==1?generateTitle('自取'):generateTitle('外送')}}</h3>
         </div>
         <div class="dibujvli" v-if="useradd">
           <div class="hengxlan texjc xias">
             <div>{{$t('dingdanorder.收货人')}}:{{useradd.name}}</div>
             <div class="jiange">{{$t('dingdanorder.联系电话')}}:{{useradd.phone}}</div>
           </div>
           <div>
             <div>{{$t('dingdanorder.地区')}}:{{useradd.country}}{{useradd.city}}{{useradd.area}}</div>
             <div>{{$t('dingdanorder.收货地址')}}:{{useradd.address}}</div>
           </div>
         </div>
         <div>
           <div class="hengxlan dibujvli" v-for="item in food">
              <image-preview :src="item.image" width="70px" height="70px"/>
              <div class="jiange">
                <div class="texjc">{{item.name}}</div>
                <div>{{$t('dingdanorder.价格')}}:{{item.price+item.otherPrice}} |  {{$t('dingdanorder.数量')}}:{{item.number}} |  {{$t('dingdanorder.小计')}}:{{item.number*(item.price+item.otherPrice)}}</div>
                <div class="hengxlan">
                  <div>{{$t('dingdanorder.规格')}}:</div>
                  <div v-if="item.ask.length==0">--</div>
                  <div class="guige" v-for="em in item.ask">{{em}}</div>
                </div>
              </div>
           </div>
         </div>
       </div>
       <!-- <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'165px':'165px'">
         <el-form-item :label="$t('dingdanorder.订单状态')" prop="state">
           <el-radio-group v-model="form.state">
             <el-radio
               v-for="dict in dict.type.sys_order_type"
               :key="dict.value"
               :label="parseInt(dict.value)"
             >{{generateTitle(dict.label)}}</el-radio>
           </el-radio-group>
         </el-form-item>
       </el-form> -->
       <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'165px':'165px'">
         <el-form-item :label="$t('dingdanorder.客服处理')" prop="kefuState">
           <el-radio-group v-model="form.kefuState">
             <el-radio key="0" :label="0">{{generateTitle('正常')}}</el-radio>
             <el-radio key="1" :label="1">{{generateTitle('已处理')}}</el-radio>
             <el-radio key="2" :label="2">{{generateTitle('未处理')}}</el-radio>
           </el-radio-group>
         </el-form-item>
         <el-form-item :label="$t('dingdanorder.处理情况')" prop="kefuContent">
           <el-input
             type="textarea"
             :rows="2"
             placeholder="请输入处理情况"
             v-model="form.kefuContent">
           </el-input>
         </el-form-item>
       </el-form>
       <Appeal ></Appeal>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('dingdanorder.确定')}}</el-button>
        <el-button @click="cancel">{{$t('dingdanorder.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Appeal from "./index.vue"
import { listShOrder3,listOrder, getOrder, delOrder, addOrder, updateOrder,getvip,getorderhout } from "@/api/system/order";
import {generateTitle} from '../../../utils/i18.js'
import Bus from '../../../utils/Bus.js'
import { number } from "echarts";
export default {
  components:{
      Appeal
  },
  name: "Order",
  dicts: ['sys_order_leixing', 'sys_order_type'],

  data() {
    return {
      loding:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // posorder表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      xqlan:false,
      userType:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 日期范围
        dateRange: "",
        ddId: null,
        shId: null,
        mdId: null,
        cretim: null,
        shdzId: null,
        userId: null,
        amount: null,
        state: 4,
        type: null,
        delryTime: null
      },
      // 表单参数
      form: {},
      orxiangq:{}, //订单详请
      store:{},
      dduser:{},
      shus:{},
      qisus:{},
      useradd:null,
      food:[],
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    guanbi(){
      this.xqlan =false
    },
    /** 查询posorder列表 */
    getList() {
      var type = this.$route.query.type;
      console.log("type============");
      console.log(type);
      if(type != undefined){
        this.userType = type;
      }
      this.loading = true;
      listShOrder3(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ddId: null,
        shId: null,
        mdId: null,
        cretim: null,
        shdzId: null,
        userId: null,
        amount: null,
        remarks: null,
        state: null,
        type: null,
        delryTime: null,
        kefuState:null,
        kefuContent:null,
        logo: null,
        posName: 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 = "添加posorder";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      this.loding = true;
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        getorderhout(id).then(response => {
          console.log("订单信息",response.data);
          this.orxiangq = response.data;
          this.store = response.data.store==null?{}:response.data.store;
          this.dduser = response.data.user==null?{}:response.data.user;
          this.shus = response.data.shanghu==null?{}:response.data.shanghu;
          this.qisus = response.data.qsuser==null?{}:response.data.qsuser;
          this.food = response.data.food==null?[]:response.data.food;
          this.useradd = response.data.shaddress;
          this.loding = false;
          this.xqlan = true;
          Bus.$emit('order',{ddid:response.data.ddId,type:this.userType});
          getvip(response.data.user.userId).then(res=>{
            this.dduser.vip = res.data;
             console.log("会员信息",res.data);

          })

        });
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('dingdanorder.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('dingdanorder.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除posorder编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
  .dingbu{
    margin-top: 20px;
  }
  .dibujvli{
    margin-bottom: 20px;
  }
  .xias{
    margin-bottom: 10px;
  }
  .hxlan{
    display: flex;
    align-items: center;
  }
  .youjg{
    margin-right: 40px;
  }
  .guige{
    padding: 3px 10px;
    background-color: #ff557f;
    color: #ffffff;
    border-radius: 8px;
    margin-right: 10px;
  }
  .hengxlan{
    display: flex;
    align-items: center;
    margin-top: 10px;
  }
  .jiange{
    margin-left: 10px;
  }
  .texjc{
    font-weight: bold;
  }
  .hs{
    color: #ff5500;
  }
  .minz{
    font-size: 15px;
    font-weight: bold;
    margin-bottom: 10px;
  }
</style>
