<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :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="storeId" width="100px" />
      <el-table-column label="商品订单号" align="center" prop="orderSn" />
      <el-table-column label="配送单号" align="center" prop="deliverySn" />
      <el-table-column label="所属门店" align="center" prop="storeName" />
      <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" />
      <el-table-column label="联系方式" align="center" prop="userPhone" />
      <el-table-column label="订单状态" align="center" prop="deliveryStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deliveryStatus == dict.value" v-for="dict in dict.type.delivery_status" :key="dict.code"> {{ dict.label }}</el-tag>
        </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" />
      <el-table-column label="配送员电话" align="center" prop="transporterPhone" />

<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['settlement:store:rate:edit']">修改</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </Page>

    <!-- 添加或修改结算费率对话框 -->
    <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-col :span="24">
            <el-form-item label="工厂结算费率" prop="factoryRate">
              <el-input-number v-model="form.factoryRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
        <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>
