<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="120px" prop="orgId">
        <el-select v-if="userInfoVO.userType == '00'" v-model="queryParams.storeId" placeholder="请选择门店" clearable filterable>
          <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryParams.orderSn" placeholder="订单编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryParams.mobile" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.clothOrderType" placeholder="请选择订单类型" clearable filterable>
          <el-option label="收衣" :value="0" />
          <el-option label="送衣" :value="1" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item>
        <el-select v-model="queryParams.deliveryStatus" placeholder="请选择订单状态" clearable filterable>
          <el-option v-for="dict in dict.type.delivery_status" :key="dict.code" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-date-picker style="width: 250px" clearable v-model="queryTimeRange" type="daterange"
                        value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" @change="getQueryTimeRange"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <Page uri="/mapi/order/delivery/list" :request-params="queryParams" ref="pagination">
      <el-table-column label="编号" align="center" prop="id"  width="55"/>
      <el-table-column label="配送单号" align="center" prop="deliverySn"  min-width="130"/>
      <el-table-column label="商品订单号" align="center" prop="orderSn" min-width="130"/>
      <el-table-column label="所属门店" align="center" prop="storeName"  min-width="130"/>
      <el-table-column label="配送方式" align="center" prop="deliveryWay">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deliveryWay == 0" type="primary"> {{ '三方' }}</el-tag>
          <el-tag v-if="scope.row.deliveryWay == 1" type="primary"> {{ '自配送' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="clothOrderType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.clothOrderType == 0" type="success"> {{ '收衣' }}</el-tag>
          <el-tag v-if="scope.row.clothOrderType == 1" type="primary"> {{ '送衣' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="地址" align="center" prop="userAddress" show-overflow-tooltip  min-width="200"/>
      <el-table-column label="联系方式" align="center" prop="userPhone"  min-width="110"/>
      <el-table-column label="订单状态" align="center" prop="deliveryStatus">
        <template slot-scope="scope">
          <template v-for="dict in dict.type.delivery_status">
            <el-tag v-if="scope.row.deliveryStatus == dict.value" :key="dict.code"> {{ dict.label }}</el-tag>
          </template>
        </template>
      </el-table-column>
<!--      <el-table-column label="取货码" align="center" prop="fetchCode" />-->
<!--      <el-table-column label="收货码" align="center" prop="recvCode" />-->
      <el-table-column label="配送员" align="center" prop="transporterName" >
        <template slot-scope="scope">
          {{ scope.row.transporterName || '--' }}
        </template>
      </el-table-column>
      <el-table-column label="配送员电话" align="center" prop="transporterPhone"  min-width="110">
        <template slot-scope="scope">
          {{ scope.row.transporterPhone || '--' }}
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime"   min-width="150"/>
      <!-- <el-table-column label="接单时间" align="center" prop="acceptTime"   min-width="150">
        <template slot-scope="scope">
          {{ scope.row.acceptTime || '--' }}
        </template>
      </el-table-column> -->
<!--      <el-table-column label="完成时间" align="center" prop="finishTime" width="140" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="100">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >查看</el-button>
        </template>
      </el-table-column>
    </Page>

    <!-- 添加或修改结算费率对话框 -->
    <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="配送单号">{{ form.deliverySn }}</el-descriptions-item>
        <el-descriptions-item label="商品订单号">{{ form.orderSn }}</el-descriptions-item>
        <el-descriptions-item label="配送方式">
          <el-tag v-if="form.deliveryWay == 0" type="primary"> {{ '三方' }}</el-tag>
          <el-tag v-if="form.deliveryWay == 1" type="primary"> {{ '自配送' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="订单类型">
          <el-tag v-if="form.clothOrderType == 0" type="success">收衣</el-tag>
          <el-tag v-if="form.clothOrderType == 1" type="primary">取衣</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ form.userName }}</el-descriptions-item>
        <el-descriptions-item label="地址">{{ form.userAddress }}</el-descriptions-item>
        <el-descriptions-item label="联系方式">{{ form.userPhone }}</el-descriptions-item>
        <el-descriptions-item label="订单状态">
          <template  v-for="dict in dict.type.delivery_status">
            <el-tag :key="dict.code" v-if="form.deliveryStatus == dict.value">{{ dict.label }}</el-tag>
          </template>
        </el-descriptions-item>
        <!-- <el-descriptions-item label="取货码">{{ form.fetchCode }}</el-descriptions-item> -->
        <!-- <el-descriptions-item label="收货码">{{ form.recvCode }}</el-descriptions-item> -->
        <el-descriptions-item label="配送员">{{ form.transporterName || '--' }}</el-descriptions-item>
        <el-descriptions-item label="配送员电话">{{ form.transporterPhone || '--' }}</el-descriptions-item>
        <el-descriptions-item label="下单时间">{{ form.createTime }}</el-descriptions-item>
        <!-- <el-descriptions-item label="接单时间">{{ form.acceptTime }}</el-descriptions-item> -->
        <!-- <el-descriptions-item label="完成时间">{{ form.finishTime }}</el-descriptions-item> -->
      </el-descriptions>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import { getDeliveryInfo } from '@/api/order/delivery'
import { allOrg } from '@/api/system/store'

export default {
  name: 'StoreRate',
  dicts: ['delivery_status'],
  data() {
    return {
      orgList: [],
      storeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算费率表格数据
      rateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        storeId: null,
        orderSn: null,
        mobile: null,
        clothOrderType: null,
        deliveryStatus: null,
        startDate: null,
        endDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateRange: [{ required: true, message: '时间范围不能为空', trigger: 'blur' }],
      },
      queryTimeRange: [],
      userInfoVO: null,
    }
  },
  created() {
    this.userInfoVO = this.getUserInfo()
    allOrg().then((res) => {
      this.orgList = res.data
      this.orgList.forEach((org) => {
        if (org.sourceType !== '01') {
          this.storeList.push(org)
        }
      })
    })
    this.getList()
  },
  methods: {
    /** 查询结算费率列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryRate: 0,
        materialRate: 0,
        cardDepositRate: 0,
        welfareDepositRate: 0,
        passwordDepositRate: 0,
        otherConsumeRate: 0,
        aliPayRate: 0,
        wxPayRate: 0,
        managementRate: 0,
        washRate: 0,
        startDate: null,
        endDate: null,
        dateRange: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams = {}
      this.queryTimeRange = []
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      if (id) {
        getDeliveryInfo(id).then((response) => {
          this.form = response.data
          this.form.dateRange = [response.data.startDate, response.data.endDate]
          this.open = true
        })
      } else {
        this.open = true
      }
      this.form.storeId = row.storeId
      this.title = '查看配送单明细'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // if (this.form.id != null) {
          //   updateStoreRate(this.form).then((response) => {
          //     this.$modal.msgSuccess('修改成功')
          //     this.open = false
          //     this.getList()
          //   })
          // } else {
          //   saveStoreRate(this.form).then((response) => {
          //     this.$modal.msgSuccess('新增成功')
          //     this.open = false
          //     this.getList()
          //   })
          // }
        }
      })
    },
    // 时间选择
    selectDateRange() {
      if (this.form.dateRange != null) {
        this.form.startDate = this.form.dateRange[0]
        this.form.endDate = this.form.dateRange[1]
      }
    },
    getQueryTimeRange() {
      if (this.queryTimeRange != null) {
        this.queryParams.startDate = this.queryTimeRange[0]
        this.queryParams.endDate = this.queryTimeRange[1]
      }
    }
  }
}
</script>
