<template>
  <div class="s-page">
    <el-page-header @back="goBack" :content="title"></el-page-header>
    <el-divider></el-divider>
    <el-card class="box-card" v-if="formType != 0">
      <div class="order-main">
        <el-row>
          <el-col :span="24">
            <div class="order-main-title">
              <span>订单编号：{{ formData.id }}</span>
              <span>下单时间：{{ formData.createTime }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <div style="display: flex">
              <div>
                <div class="order-main-status">
                  <span>{{ filterStatus(formData.status) }}</span>
                </div>
                <div class="order-main-opt-btn">
                  <el-button
                    v-if="formData.status === 'WAIT'"
                    type="primary"
                    @click="submitOrder()"
                    style="margin-right: 10px"
                    >确定租赁</el-button
                  >
                  <el-button
                    v-if="formData.status === 'LEASE' || formData.status === 'OVER'"
                    @click="isRecover = true"
                    type="primary"
                    slot="reference"
                    style="margin-right: 10px"
                    >确认回收</el-button
                  >
                  <el-popconfirm title="确定取消订单吗？" @confirm="cancelOrder()" v-if="formData.status === 'WAIT'">
                    <el-button slot="reference" type="info" plain style="margin-right: 10px">取消订单</el-button>
                  </el-popconfirm>
                  <el-button
                    v-if="formData.status === 'LEASE'"
                    @click="isRepair = true"
                    type="primary"
                    style="margin-right: 10px"
                    >报修</el-button
                  >
                  <el-button
                    v-if="formData.status === 'LEASE' || formData.status === 'OVER'"
                    @click="isRelet = true"
                    type="primary"
                    style="margin-right: 10px"
                    >续租</el-button
                  >
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="14" style="padding-top: 10px">
            <el-steps
              :active="
                formData.status == 'WAIT'
                  ? 1
                  : formData.status == 'LEASE'
                  ? 2
                  : formData.status == 'OVER'
                  ? 3
                  : formData.status == 'RECOVER'
                  ? 5
                  : 0
              "
              align-center
            >
              <el-step title="买家下单" :description="formData.createTime"></el-step>
              <el-step title="商家确认" :description="formData.confirmTime ? formData.confirmTime : ''"></el-step>
              <el-step title="租赁中" :description="formData.deliverTime ? formData.deliverTime : ''"></el-step>
              <el-step title="已到期-待回收" :description="formData.overTime ? formData.overTime : ''"></el-step>
              <el-step title="已到期-已回收" :description="formData.overTime ? formData.overTime : ''"></el-step>
            </el-steps>
          </el-col>
        </el-row>
      </div>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>客户信息</span>
      </div>
      <div class="mymain-container">
        <el-form ref="formData" :rules="rules" :model="formData" label-width="110px" size="small" label-position="left">
          <el-row :gutter="20" justify="start">
            <el-col :span="8">
              <el-form-item label="客户姓名" prop="userName" :required="true">
                <el-input
                  type="text"
                  :disabled="formType != 0 && formData.status != 'WAIT'"
                  v-model="formData.userName"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="客户联系电话" prop="userMobile" :required="true">
                <zj-select
                  v-model="formData.userInfo"
                  blurNoMatchInputBorderColor=""
                  :blurNoMatchText="true"
                  :filter-method="userMobileInput"
                  :disabled="formType != 0 && formData.status != 'WAIT'"
                  value-key="userId"
                  filterable
                  @change="changeUser"
                  @blur="userBlur"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <zj-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.mobile + '(' + item.nickName + ')'"
                    :value="item"
                  >
                  </zj-option>
                </zj-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="客户地址" class="is-required">
                <el-row>
                  <el-col :span="5" style="margin-right: 12px">
                    <el-select
                      v-model="formData.provinceObj"
                      :disabled="formType != 0 && formData.status != 'WAIT'"
                      filterable
                      value-key="id"
                      placeholder="请选择"
                      @change="
                        val => {
                          formData.provinceId = val.id
                          formData.province = val.name
                          formData.cityObj = null
                          formData.cityId = ''
                          formData.city = ''
                          formData.areaObj = null
                          formData.areaId = ''
                          formData.area = ''
                          formData.streetObj = null
                          formData.streetId = ''
                          formData.street = ''
                          this.lbsAmapRegion('cityList', val.id)
                        }
                      "
                    >
                      <el-option
                        v-for="(item, index) in provinceList"
                        :key="index"
                        :label="item.name"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="5" style="margin-right: 12px">
                    <el-select
                      v-model="formData.cityObj"
                      :disabled="formType != 0 && formData.status != 'WAIT'"
                      filterable
                      value-key="id"
                      placeholder="请选择"
                      @change="
                        val => {
                          formData.cityId = val.id
                          formData.city = val.name
                          formData.areaObj = null
                          formData.areaId = ''
                          formData.area = ''
                          formData.streetObj = null
                          formData.streetId = ''
                          formData.street = ''
                          this.lbsAmapRegion('areaList', val.id)
                        }
                      "
                    >
                      <el-option
                        v-for="(item, index) in cityList"
                        :key="index"
                        :label="item.name"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="5" style="margin-right: 12px">
                    <el-select
                      v-model="formData.areaObj"
                      :disabled="formType != 0 && formData.status != 'WAIT'"
                      filterable
                      value-key="id"
                      placeholder="请选择"
                      @change="
                        val => {
                          formData.areaId = val.id
                          formData.area = val.name
                          formData.streetObj = null
                          formData.streetId = ''
                          formData.street = ''
                          this.lbsAmapRegion('streetList', val.id)
                        }
                      "
                    >
                      <el-option
                        v-for="(item, index) in areaList"
                        :key="index"
                        :label="item.name"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="8">
                    <el-select
                      v-model="formData.streetObj"
                      :disabled="formType != 0 && formData.status != 'WAIT'"
                      filterable
                      value-key="id"
                      placeholder="请选择"
                      @change="
                        val => {
                          formData.streetId = val.id
                          formData.street = val.name
                        }
                      "
                    >
                      <el-option
                        v-for="(item, index) in streetList"
                        :key="index"
                        :label="item.name"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="" prop="address">
                <el-row :gutter="20">
                  <el-col :span="22">
                    <el-input
                      type="text"
                      v-model="formData.address"
                      :disabled="formType != 0 && formData.status != 'WAIT'"
                      placeholder="详细地址"
                    ></el-input>
                  </el-col>
                  <el-col :span="2">
                    <!-- <geographicalPosi :disabled="formType!=0 && formData.status != 'WAIT'"
											:formData="this.formData" @selectPosi="selectAddress" /> -->
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="4"
                  v-model="formData.remark"
                  :disabled="formType != 0 && formData.status != 'WAIT'"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="租赁合同"
                prop="fileUrl"
                :rules="[{ required: true, message: `请上传租赁合同`, trigger: 'change' }]"
              >
                <ImageUpload
                  :fileList="formData.fileUrl"
                  :limit="1"
                  :isEdit="formType == 0 || formData.status == 'WAIT'"
                />
              </el-form-item>
            </el-col>
            <template v-if="formData.status == 'RECOVER'">
              <el-col :span="8">
                <el-form-item label="回收操作人" prop="recoverBy">
                  <el-input type="text" disabled v-model="formData.recoverBy" placeholder="请输入"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="回收操作时间" prop="recoverTime">
                  <el-input type="text" disabled v-model="formData.recoverTime" placeholder="请输入"></el-input>
                </el-form-item>
              </el-col>
            </template>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card class="box-card" v-if="formType == 0">
      <div slot="header" class="clearfix">
        <span>商品信息</span>
      </div>
      <el-button size="small" v-if="formType == 0" type="primary" @click="add()">添加</el-button>
      <div class="table">
        <el-form ref="formData1" :model="formData1" label-width="0" size="small" label-position="left">
          <el-table :data="formData1.dataList" element-loading-text="Loading" border fit highlight-current-rowstripe>
            <el-table-column prop="categoryName" align="center" label="*商品分类">
              <template slot-scope="scope">
                <el-form-item :prop="'dataList.' + scope.$index + '.categoryName'">
                  <el-input type="text" v-model="scope.row.categoryName" disabled placeholder="请选择分类"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="*商品名称" align="center">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.goods'"
                  :rules="[{ required: true, message: `请选择商品`, trigger: 'change' }]"
                >
                  <el-select
                    v-model="scope.row.goods"
                    @change="changeGoods($event, scope.row)"
                    value-key="id"
                    :disabled="isEdit != scope.$index || formType == 2"
                    placeholder="请选择"
                    style="width: 100%"
                  >
                    <el-option v-for="item in goodsList" :key="item.id" :label="item.name" :value="item"> </el-option>
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="goodsUnit" align="center" label="单位"></el-table-column>
            <el-table-column prop="" align="center" label="*数量">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.qty'"
                  :rules="[{ required: true, message: `请输入数量`, trigger: 'blur' }]"
                >
                  <el-input
                    type="number"
                    v-model="scope.row.qty"
                    :disabled="isEdit != scope.$index || formType == 2"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="depositPrice" align="center" label="订金"></el-table-column>
            <el-table-column prop="leasePrice" align="center" label="租价"></el-table-column>
            <el-table-column prop="startDate" align="center" label="*起租日期">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.startDate'"
                  :rules="[{ required: true, message: `请选择日期`, trigger: 'blur' }]"
                >
                  <el-date-picker
                    v-model="scope.row.startDate"
                    type="date"
                    @change="
                      e => {
                        getPrice(scope.row)
                      }
                    "
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="endDate" align="center" label="*到期日期">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.endDate'"
                  :rules="[{ required: true, message: `请选择日期`, trigger: 'blur' }]"
                >
                  <el-date-picker
                    v-model="scope.row.endDate"
                    type="date"
                    @change="
                      e => {
                        getPrice(scope.row)
                      }
                    "
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="depositAmount" align="center" label="*押金">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.depositAmount'"
                  :rules="[{ required: true, message: `请输入押金`, trigger: 'blur' }]"
                >
                  <el-input type="number" v-model="scope.row.depositAmount" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="renewalPrice" align="center" label="*续期单价">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.renewalPrice'"
                  :rules="[{ required: true, message: `请输入押金`, trigger: 'blur' }]"
                >
                  <el-input
                    type="number"
                    v-model="scope.row.renewalPrice"
                    @input="
                      e => {
                        getPrice(scope.row)
                      }
                    "
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="leaseAmount" align="center" label="总租金"></el-table-column>
            <el-table-column label="操作" align="right" width="140">
              <template slot-scope="scope" v-if="formType != 2">
                <el-button size="mini" type="primary" v-if="scope.$index != isEdit" @click="isEdit = scope.$index"
                  >编辑</el-button
                >
                <el-button size="mini" type="danger" @click="del(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
    </el-card>
    <el-card class="box-card" v-if="formType != 0">
      <div class="table">
        <el-form ref="formData3" :model="formData3" label-width="0" size="small" label-position="left">
          <el-table :data="formData3.dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
            <el-table-column align="center" label="*商品分类" width="300">
              <template slot-scope="scope">
                <div style="display: flex">
                  <div class="image">
                    <img :src="$showImgUrl(scope.row.categoryUrl)" style="height: 80px; width: 80px" />
                  </div>
                  <div class="main" style="display: flex; justify-content: center; flex-direction: column">
                    <span>{{ scope.row.goodsLeaseName }}</span>
                    <span>x{{ scope.row.qty }}</span>
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="depositPrice" align="center" label="订金"></el-table-column>
            <el-table-column prop="leasePrice" align="center" label="租价"></el-table-column>
            <el-table-column prop="qty" align="center" label="数量"></el-table-column>
            <el-table-column prop="startDate" align="center" label="*起租日期">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.startDate'"
                  :rules="[{ required: true, message: `请选择日期`, trigger: 'blur' }]"
                >
                  <el-date-picker
                    v-model="scope.row.startDate"
                    type="date"
                    @change="
                      e => {
                        getPrice(scope.row)
                      }
                    "
                    :disabled="formData.status != 'WAIT'"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="endDate" align="center" label="*到期日期">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.endDate'"
                  :rules="[{ required: true, message: `请选择日期`, trigger: 'blur' }]"
                >
                  <el-date-picker
                    v-model="scope.row.endDate"
                    type="date"
                    @change="
                      e => {
                        getPrice(scope.row)
                      }
                    "
                    :disabled="formData.status != 'WAIT'"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="realEndDate" align="center" label="实际到期日期">
              <template slot-scope="scope">
                {{ scope.row.realEndDate ? scope.row.realEndDate.substring(0, 10) : '' }}
              </template>
            </el-table-column>
            <el-table-column prop="depositAmount" align="center" label="*押金">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.depositAmount'"
                  :rules="[{ required: true, message: `请输入押金`, trigger: 'blur' }]"
                >
                  <el-input
                    type="number"
                    v-model="scope.row.depositAmount"
                    :disabled="formData.status != 'WAIT'"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="" align="center" label="*续期单价">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'dataList.' + scope.$index + '.renewalPrice'"
                  :rules="[{ required: true, message: `请输入押金`, trigger: 'blur' }]"
                >
                  <el-input
                    type="number"
                    v-model="scope.row.renewalPrice"
                    @input="
                      e => {
                        getPrice(scope.row)
                      }
                    "
                    :disabled="formData.status != 'WAIT'"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="leaseAmount" align="center" label="总租金"></el-table-column>
            <el-table-column prop="renewalLeaseAmount" align="center" label="续期租金"></el-table-column>
          </el-table>
        </el-form>
      </div>
    </el-card>
    <el-card class="box-card" v-if="formType != 0">
      <div slot="header" class="clearfix">
        <span>租赁记录</span>
      </div>
      <div class="table">
        <el-table :data="formData.records" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column prop="status" label="租赁类型" align="center">
            <template slot-scope="scope">
              {{ scope.row.orderType == 'FIRST' ? '初次租赁' : '续租' }}
            </template>
          </el-table-column>
          <el-table-column prop="payValue" label="订金" align="center"></el-table-column>
          <el-table-column prop="" align="center" label="租赁开始时间">
            <template slot-scope="scope">
              {{ scope.row.startDate ? scope.row.startDate.substring(0, 10) : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="" align="center" label="租赁结束时间">
            <template slot-scope="scope">
              {{ scope.row.endDate ? scope.row.endDate.substring(0, 10) : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createBy" align="center" label="操作人"></el-table-column>
          <el-table-column prop="createTime" align="center" label="操作时间"></el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-card class="box-card" v-if="formType != 0">
      <div slot="header" class="clearfix">
        <span>报修记录</span>
      </div>
      <div class="table">
        <el-table :data="formData.orderBases" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column prop="id" label="报修单号" align="center"></el-table-column>
          <el-table-column prop="createDate" label="提交时间" align="center"></el-table-column>
          <el-table-column prop="appointmentTime" label="上门时间" align="center"></el-table-column>
          <el-table-column prop="overDate" label="完工时间" align="center"></el-table-column>
          <el-table-column prop="updateBy" align="center" label="操作人"></el-table-column>
          <el-table-column prop="updateTime" align="center" label="操作时间"></el-table-column>
        </el-table>
      </div>
    </el-card>
    <div class="page-footer">
      <div class="footer">
        <el-button v-if="formType == 0" size="small" type="primary" @click="submit()">提交</el-button>
        <el-button size="small" type="info" @click="goBack">返回</el-button>
      </div>
    </div>
    <el-dialog title="报修" :visible.sync="isRepair" width="30%" :close-on-click-modal="false">
      <el-form ref="repairForm" :model="repairForm" label-position="left">
        <el-form-item
          label="上门时间"
          prop="date"
          :rules="[{ required: true, message: `请选择上门时间`, trigger: 'blur' }]"
        >
          <el-date-picker v-model="repairForm.date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isRepair = false">取 消</el-button>
        <el-button type="primary" @click="confirmRepair()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="续租" :visible.sync="isRelet" width="30%" :close-on-click-modal="false">
      <el-form ref="reletForm" :model="reletForm" label-position="left">
        <el-form-item
          label="实际到期日期"
          prop="date"
          :rules="[{ required: true, message: `请选择日期`, trigger: 'blur' }]"
        >
          <el-date-picker v-model="reletForm.date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isRelet = false">取 消</el-button>
        <el-button type="primary" @click="confirmRelet">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="确定回收" :visible.sync="isRecover" width="30%" :close-on-click-modal="false">
      <el-form ref="recoverForm" :model="recoverForm" label-position="left">
        <el-form-item
          label="实际到期日期"
          prop="date"
          :rules="[{ required: true, message: `请选择日期`, trigger: 'blur' }]"
        >
          <el-date-picker v-model="recoverForm.date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isRecover = false">取 消</el-button>
        <el-button type="primary" @click="confirmRecover">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { lbsAmapRegion } from '@/api/common.js'
import geographicalPosi from '@/components/geographicalPosi/index.vue'
import ImageUpload from '@/components/file-upload'
import {
  getGoods,
  getDetail,
  add,
  confirmOrder,
  confirmRecover,
  confirmRelet,
  confirmRepair,
  getTypeList,
  getUserList,
  getPrice
} from '@/api/tenancyOrder'
export default {
  components: {
    geographicalPosi,
    ImageUpload
  },
  props: ['id', 'title', 'formType'],
  data() {
    return {
      formData1: {
        dataList: []
      },
      formData3: {
        dataList: []
      },
      repairForm: {
        date: ''
      },
      reletForm: {
        date: ''
      },
      recoverForm: {
        date: ''
      },
      goodsList: [],
      userList: [],
      is_submit: true,
      isRelet: false,
      isRepair: false,
      isRecover: false,
      formData: {
        userInfo: null,
        userName: '',
        userId: '',
        userMobile: '',
        remark: '',
        provinceObj: null,
        province: '',
        provinceId: '',
        cityObj: null,
        city: '',
        cityId: '',
        areaObj: null,
        area: '',
        areaId: '',
        streetObj: null,
        street: '',
        streetId: '',
        address: '',
        lat: '',
        lng: '',
        records: [],
        fileUrl: []
      },
      provinceList: [],
      cityList: [],
      areaList: [],
      streetList: [],
      isEdit: 0,
      timer: '',
      companyName: JSON.parse(localStorage.getItem('greemall_user')).companyName,
      rules: {
        address: [
          {
            required: true,
            message: '请选择客户地址',
            trigger: 'change'
          }
        ],
        userMobile: [
          {
            required: true,
            message: '请输入客户电话',
            trigger: 'blur'
          },
          {
            required: true,
            message: '请输入客户电话',
            trigger: 'change'
          },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
            message: '电话号码格式不正确',
            trigger: 'blur'
          },
          ,
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
            message: '电话号码格式不正确',
            trigger: 'change'
          }
        ],
        userName: [
          {
            required: true,
            message: '请输入客户姓名',
            trigger: 'blur'
          },
          {
            required: true,
            message: '请输入客户姓名',
            trigger: 'change'
          }
        ]
      },
      rules1: {
        price: [
          {
            pattern: /^[1-9]\d*(\.\d+)?$|^0\.[1-9]\d*$/,
            message: '请输入大于0的数字',
            trigger: 'blur'
          }
        ]
      },
      typeId: ''
    }
  },
  computed: {
    filterStatus() {
      return function (type) {
        let status = {
          WAIT: '等待商家确认',
          WAIT_START: '待开始',
          LEASE: '租赁中',
          OVER: '已过期-待回收',
          RECOVER: '已过期-已回收'
        }
        return status[type]
      }
    }
  },
  created() {
    if (this.id) {
      this.getDetail()
    }
    this.lbsAmapRegion('provinceList', 0)
    this.getGoods()
    this.getTypeList()
  },
  methods: {
    // 返回
    goBack() {
      this.$emit('back')
    },
    getDetail() {
      getDetail({
        id: this.id
      }).then(res => {
        this.formData = {
          ...res.data,
          address: res.data.userAddress,
          fileUrl: res.data.fileUrl ? [{ url: res.data.fileUrl }] : [],
          provinceObj: { id: res.data.provinceId },
          cityObj: { id: res.data.cityId },
          areaObj: { id: res.data.areaId },
          streetObj: { id: res.data.streetId },
          userInfo:
            res.data.userId && res.data.payType == 'LINE'
              ? { userId: res.data.userId, userMobile: res.data.userMobile, userName: res.data.userName }
              : res.data.userMobile
        }
        this.formData3.dataList = res.data.items
        this.getinitlbslist()
        this.getUserList()
      })
    },
    getGoods() {
      getGoods({
        pageNum: 1,
        pageSize: -1,
        status: true
      }).then(res => {
        this.goodsList = res.data.records
      })
    },
    getPrice(item) {
      if (!item.startDate || !item.endDate) {
        return false
      }
      getPrice({
        startDate: item.startDate,
        endDate: item.endDate,
        isRenewal: this.formData.status == 'WAIT' || !this.formData.status ? false : true,
        price: this.formData.status == 'WAIT' || !this.formData.status ? item.leasePrice : item.renewalPrice,
        qty: item.qty
      }).then(res => {
        if (this.formData.status == 'WAIT' || !this.formData.status) {
          item.leaseAmount = res.data
        } else {
          item.renewalLeaseAmount = (res.data * 100 + this.formData.totalAmount * 100) / 100
        }
      })
    },
    getUserList(value) {
      getUserList({
        pageNum: 1,
        pageSize: -1,
        params: [{ param: 'a.mobile', compare: 'like', value }]
      }).then(res => {
        this.userList = res.data.records
      })
    },
    userBlur(e) {
      this.formData.userMobile = e.target.value
      this.formData.userInfo = e.target.value
      this.$forceUpdate()
    },
    changeUser(e) {
      this.formData.userMobile = e.mobile
      this.formData.userName = e.nickName
      this.formData.userId = e.userId
    },
    changeGoods(e, item) {
      item.categoryId = e.categoryId
      item.categoryName = e.categoryName
      item.categoryUrl = e.categoryUrl
      item.goodsLeaseName = e.name
      item.goodsLeaseId = e.id
      item.goodsUnit = e.unit
      item.depositPrice = e.depositPrice
      item.leasePrice = e.leasePrice
    },
    userMobileInput(e) {
      if (e && e.length > 3) {
        this.getUserList(e)
      }
    },
    add() {
      this.$refs.formData1.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          console.log(111)
          if (this.formData1.dataList.length != 0) {
            this.isEdit = 0
          }
          this.formData1.dataList.unshift({
            goods: null,
            categoryId: '',
            categoryName: '',
            categoryUrl: '',
            qty: '',
            goodsLeaseName: '',
            goodsLeaseId: '',
            goodsUnit: '',
            depositPrice: null,
            leasePrice: null,
            startDate: '',
            endDate: '',
            realEndDate: '',
            depositAmount: null,
            renewalPrice: null,
            leaseAmount: null
          })
        }
      })
    },
    del(index) {
      if (index <= 1) {
        this.isEdit == 0
      } else {
        this.isEdit -= 1
      }
      this.formData1.dataList.splice(index, 1)
    },
    submit() {
      const that = this
      this.$refs.formData.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          if (this.formData1.dataList.length == 0) {
            return this.$message.warning('请添加商品信息！')
          } else {
            this.$refs.formData1.validate((valid, invalidFields, errLabels) => {
              if (valid) {
                add({
                  userId: this.formData.userId,
                  userName: this.formData.userName,
                  userMobile: this.formData.userMobile,
                  fileUrl: this.formData.fileUrl.length > 0 ? this.formData.fileUrl[0].url : '',
                  items: this.formData1.dataList,
                  remark: this.formData.remark,
                  province: this.formData.province,
                  provinceId: this.formData.provinceId,
                  city: this.formData.city,
                  cityId: this.formData.cityId,
                  area: this.formData.area,
                  areaId: this.formData.areaId,
                  street: this.formData.street,
                  streetId: this.formData.streetId,
                  userAddress: this.formData.address,
                  lat: this.formData.lat,
                  lng: this.formData.lng
                }).then(res => {
                  if (res.code == 200) {
                    this.$message.success('提交成功！')
                    this.goBack()
                  }
                })
              }
            })
          }
        }
      })
    },
    //确定租赁
    submitOrder() {
      this.$refs.formData.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          this.$refs.formData3.validate((valid, invalidFields, errLabels) => {
            if (valid) {
              confirmOrder({
                userName: this.formData.userName,
                userMobile: this.formData.userMobile,
                fileUrl: this.formData.fileUrl.length > 0 ? this.formData.fileUrl[0].url : '',
                items: this.formData3.dataList,
                remark: this.formData.remark,
                province: this.formData.province,
                provinceId: this.formData.provinceId,
                city: this.formData.city,
                cityId: this.formData.cityId,
                area: this.formData.area,
                areaId: this.formData.areaId,
                street: this.formData.street,
                streetId: this.formData.streetId,
                userAddress: this.formData.address,
                lat: this.formData.lat,
                lng: this.formData.lng,
                id: this.formData.id
              }).then(res => {
                if (res.code == 200) {
                  this.$message.success('提交成功！')
                  this.getDetail()
                }
              })
            }
          })
        }
      })
    },
    //取消订单
    cancelOrder() {
      cancelOrder({
        id: this.id
      }).then(res => {
        if (res.code == 200) {
          this.$message.success('取消订单成功！')
          this.getDetail()
        }
      })
    },
    getTypeList() {
      getTypeList({
        pageNum: 1,
        pageSize: -1
      }).then(res => {
        if (res.code == 200) {
          res.data.records.forEach(item => {
            if (item.orderType == 'REPAIR') {
              this.typeId = item.id
            }
          })
        }
      })
    },
    //报修
    confirmRepair() {
      let productList = [
        {
          mainId: this.formData.items[0].categoryId,
          mainName: this.formData.items[0].categoryName,
          num: this.formData.items[0].qty,
          imgUrl: this.formData.items[0].categoryUrl
        }
      ]

      this.$refs.repairForm.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          confirmRepair({
            companyWechatId: this.formData.companyWechatId,
            appointmentTime: this.repairForm.date + ' 00:00:00',
            saleOrderId: this.id,
            orderSmallType: this.typeId,
            province: this.formData.province,
            provinceId: this.formData.provinceId,
            city: this.formData.city,
            cityId: this.formData.cityId,
            area: this.formData.area,
            areaId: this.formData.areaId,
            street: this.formData.street,
            streetId: this.formData.streetId,
            address: this.formData.address,
            orderProducts: productList,
            isZl: true
          }).then(res => {
            if (res.code == 200) {
              this.isRepair = false
              this.$message.success('报修成功！')
              this.getDetail()
            }
          })
        }
      })
    },
    //确认续租
    confirmRelet() {
      this.$refs.reletForm.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          confirmRelet({
            RealEndDate: this.reletForm.date + ' 00:00:00',
            id: this.id
          }).then(res => {
            if (res.code == 200) {
              this.isRelet = false
              this.$message.success('确认续租成功！')
              this.getDetail()
            }
          })
        }
      })
    },
    //确认回收
    confirmRecover() {
      this.$refs.recoverForm.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          confirmRecover({
            RealEndDate: this.recoverForm.date + ' 00:00:00',
            id: this.id
          }).then(res => {
            if (res.code == 200) {
              this.isRecover = false
              this.$message.success('确认回收成功！')
              this.getDetail()
            }
          })
        }
      })
    },
    lbsAmapRegion(name, pid) {
      lbsAmapRegion({ pid }).then(res => {
        this[name] = res.data
      })
    },
    getinitlbslist() {
      // 初始化请求省市区街道下拉选项数据
      lbsAmapRegion({
        pid: 0
      }).then(res => {
        this.provinceList = res.data
        // 创建工单时获取ip地址定位赋值
        if (!this.id && this.$IpAdd.province) {
          var item = this.provinceList.find(item => item.name === this.$IpAdd.province)
          if (item) {
            this.formData.provinceId = item.id
            this.formData.province = item.name
          }
        }
        if (this.formData.provinceId) {
          lbsAmapRegion({
            pid: this.formData.provinceId
          }).then(res => {
            this.cityList = res.data
            // 创建工单时获取ip地址定位赋值
            if (!this.id && this.$IpAdd.city) {
              var item2 = this.cityList.find(item => item.name === this.$IpAdd.city)
              if (item2) {
                this.formData.cityId = item2.id
                this.formData.city = item2.name
              }
            }
            if (this.formData.cityId) {
              lbsAmapRegion({
                pid: this.formData.cityId
              }).then(res => {
                this.areaList = res.data
              })
            }
            if (this.formData.areaId) {
              lbsAmapRegion({
                pid: this.formData.areaId
              }).then(res => {
                this.streetList = res.data
              })
            }
          })
        }
      })
    },
    selectAddress(data) {
      this.formData.lng = data.center[0]
      this.formData.lat = data.center[1]
      // 获取定位的省市区街道
      var { province, city, district, township } = data.data.addressComponent
      // 获取选中省名称id
      var { id, name } = this.provinceList.find(item => item.name === province)
      this.formData.provinceId = id
      this.formData.province = name
      // 请求市选项
      lbsAmapRegion({
        pid: this.formData.provinceId
      }).then(res => {
        // 赋值市选项
        this.cityList = res.data
        // 获取选中市名称id
        var { id, name } = res.data.find(item => item.name === city)
        this.formData.cityId = id
        this.formData.city = name
        // 请求区选项
        lbsAmapRegion({
          pid: this.formData.cityId
        }).then(res => {
          // 赋值区选项
          this.areaList = res.data
          // 获取选中区名称id
          var { id, name } = res.data.find(item => item.name === district)
          this.formData.areaId = id
          this.formData.area = name
          // 请求街道选项
          lbsAmapRegion({
            pid: this.formData.areaId
          }).then(res => {
            // 赋值街道选项
            this.streetList = res.data
            // 获取选中街道名称id
            var { id, name } = res.data.find(item => item.name === township)
            this.formData.streetId = id
            this.formData.street = name
            // 赋值GPS详细地址
            this.formData.address = data.name
          })
        })
      })
    }
  }
}
</script>

<style scoped="scoped" lang="scss">
.s-page {
  padding: 20px;
  background-color: #ffffff;
}

.page-footer {
  height: 70px;
}

.order-main {
  .order-main-title {
    font-size: 14px;
  }

  .order-main-title span {
    margin-right: 15px;
  }

  .order-main-status {
    padding: 15px 0;
    font-size: 24px;
    color: #409eff;
  }

  .order-main-opt-btn {
    padding: 15px 0;
  }
}

.order-receive-info {
  margin: 15px 0;
  padding: 15px;
  background: #f5f7fa;

  :first-child div span {
    padding-right: 15px;
  }

  .el-row {
    padding-top: 15px;
  }
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  background: #fff;
  padding: 15px 40px;
  box-sizing: border-box;
  transition: all 0.28s;
  text-align: right;
  box-shadow: 0 2px 5px 0 rgb(0 0 0 / 50%), 0 2px 5px 0 rgb(0 0 0 / 10%);

  &.hideSidebar {
    margin-left: 54px;
    width: calc(100vw - 54px);
  }

  &.openSidebar {
    margin-left: 210px;
    width: calc(100vw - 210px);
  }

  .tips {
    font-size: 12px;
    color: red;
    margin-top: 10px;
  }
}
</style>
