<template>
  <el-card :class="selectStatusDiscount ? 'box-cardTwo' : 'box-card'">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="事业部" prop="subsidiaryName">
        <el-input placeholder="请输入" v-model="queryParams.subsidiaryName" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="变更状态" prop="types">
        <el-select v-model="queryParams.types" placeholder="请选择" style="width: 100%" @change="handleQuery">
          <el-option v-for="item in situation" :key="item.id" :label="item.dictValue" :value="item.dictKey"> </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="订单号" prop="contractCode">
        <el-input placeholder="请输入" v-model="queryParams.contractCode" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="万里牛单号" prop="tradeNo">
        <el-input placeholder="请输入" v-model="queryParams.tradeNo" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status" v-if="!selectStatus">
        <el-select v-model="queryParams.status" placeholder="请选择" style="width: 100%" @change="handleQuery">
          <el-option v-for="item in orderStatus" :key="item.id" :label="item.dictValue" :value="item.dictKey"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结算状态" prop="settlementStatus" v-if="!selectStatus">
        <el-select v-model="queryParams.settlementStatus" placeholder="请选择" style="width: 100%" @change="handleQuery">
          <el-option v-for="item in settlementStatus" :key="item.id" :label="item.dictValue" :value="item.dictKey"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间">
        <el-form-item>
          <el-button size="mini" :class="selectBtn === 1 ? 'select' : ''" @click="handleNearTime(1)">近7天</el-button>
          <el-button size="mini" :class="selectBtn === 2 ? 'select' : ''" @click="handleNearTime(2)">近30天</el-button>
        </el-form-item>
        <el-form-item label="">
          <el-col :span="11">
            <el-date-picker
              type="datetime"
              placeholder="开始日期"
              v-model="queryParams.beginTime"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
          <el-col :span="2" style="text-align: center">-</el-col>
          <el-col :span="11">
            <el-date-picker
              type="datetime"
              placeholder="结束日期"
              v-model="queryParams.endTime"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="handleQuery" class="searchBtn">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div style="width: 100%; position: relative">
      <!-- <div style="position: absolute; right: 0; top: -34px; width: 120px; text-align: center">
        <el-button type="primary" size="mini" @click="openOperationLog = true" v-db-click>操作日志</el-button>
      </div> -->
      <el-table
        :data="tableList"
        :cell-style="{ padding: '0' }"
        :row-style="{ height: '35px' }"
        header-row-class-name="tableHeader"
        v-loading="loading"
        element-loading-background="rgba(0, 0, 0, 0.3)"
      >
        <el-table-column label="事业部" prop="subsidiaryName" width="140" />
        <el-table-column label="订单号" prop="contractCode" width="180">
          <template slot-scope="scope">
            <div>
              <span style="cursor: pointer; color: #409eff" @click="clickCode(scope.row)"> {{ scope.row.contractCode }}</span>
              <span class="aaa" v-if="scope.row.aftermarketId" @click="clickAftermarketDetails(scope.row.aftermarketId)" v-db-click>
                <span style="display: block; color: #ffffff; height: 20px; line-height: 20px; text-align: center">售后</span>
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="万里牛订单号" prop="tradeNo" width="140" />
        <el-table-column label="快递单号" prop="waybillNo" width="140" />
        <el-table-column label="订单状态" prop="status" width="100">
          <template slot-scope="scope">
            <span> {{ showStatus(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结算状态" prop="settlementStatus" width="100">
          <template slot-scope="scope">
            <span> {{ showSettleStatus(scope.row.settlementStatus) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="税率" align="center" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.taxRate">{{ Number(scope.row.taxRate) }}%</span>
            <span v-else>0%</span>
          </template>
        </el-table-column>
        <el-table-column label="订单总金额 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span style="color: #409eff">{{ moneyFormat(scope.row.contractAmount, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="产品总金额 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.productPriceAmount, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="定制加工费 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.processPrice, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="代发费 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.deliverGoodsPriceAmount, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="快递包材费 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.expressPriceAmount, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="包装人工费 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.expensePriceAmount, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="包材费 ¥" align="right" width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.packingMaterialAmount, 2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="交期" prop="deliveryTime" align="center" width="140" />
        <el-table-column label="下单时间" prop="createTime" align="center" width="140" />
        <el-table-column label="发货时间" prop="shipmentTime" align="center" width="140" />
        <el-table-column label="收货人" prop="harvester" width="100" />
        <el-table-column label="收货人电话" prop="harvesterPhone" width="140" />
        <el-table-column label="收货人地址" prop="harvestAddress" min-width="240">
          <template slot-scope="scope">
            <div>
              {{ scope.row.harvesterCountryName }}, {{ scope.row.harvesterCityName }}, {{ scope.row.harvesterArea }},
              {{ scope.row.harvestAddress }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" v-if="!(selectStatus || selectStatusDiscount)" @click="clickTaxRate(scope.row)" v-db-click>税率</el-button>
            <el-button type="text" v-if="selectStatus || selectStatusDiscount" @click="clickSelect(scope.row)" v-db-click>选择</el-button>
            <el-button
              type="text"
              v-if="(scope.row.status == '0' || scope.row.status == '10' || scope.row.status == '20') && !(selectStatus || selectStatusDiscount)"
              @click="clickDelete(scope.row)"
              v-db-click
              >删除</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" />
    </div>

    <el-dialog title="修改税率" v-if="open" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="税率 (%):" prop="taxRate">
          <el-input-number
            size="small"
            style="width: 100%"
            v-model="form.taxRate"
            placeholder="请输入税率"
            :controls="false"
            :min="0"
            :max="100"
            :precision="2"
          />
        </el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="submitForm" v-db-click>确 定</el-button>
          <el-button @click="open = false" v-db-click>取 消</el-button>
        </div>
      </el-form>
    </el-dialog>

    <el-dialog title="操作日志" v-if="openOperationLog" :visible.sync="openOperationLog" width="50%" append-to-body>
      <OperationLog :operationLogKey="operationLogKey"></OperationLog>
      <div style="text-align: center; margin-top: 20px">
        <el-button @click="openOperationLog = false" v-db-click>关 闭</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import * as API from '@/api/shengde/group/order/orderManagement.js'
import { mapGetters } from 'vuex'
import OperationLog from '@/components/shengde/operationLog/index'

export default {
  props: {
    selectStatus: Boolean,
    selectStatusDiscount: Boolean,
  },
  components: { OperationLog },
  data() {
    return {
      loading: false,
      selectBtn: null,
      settlementStatus: [
        {
          id: '0',
          dictValue: '未结算',
          dictKey: '0',
        },
        {
          id: '2',
          dictValue: '已对账',
          dictKey: '2',
        },
        {
          id: '1',
          dictValue: '已结算',
          dictKey: '1',
        },
      ],
      orderStatus: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subsidiaryName: '',
        contractCode: '',
        tradeNo: '',
        status: '',
        beginTime: '',
        endTime: '',
        queryType: '0',
      },
      tableList: [],
      total: null,
      open: false,
      form: {
        contractIds: '',
        taxRate: '',
      },
      rules: {
        taxRate: [{ required: true, message: '请输入税率', trigger: 'blur' }],
      },
      openOperationLog: false,
      operationLogKey: 10,
    }
  },
  created() {
    if (this.selectStatus) {
      this.queryParams.queryType = '1'
    }
    this.orderStatus = this.dictData.filter((item) => item.code === 'order_status')[0].children
  },
  mounted() {
    this.handleNearTime(2)
  },
  computed: mapGetters(['dictData']),
  methods: {
    getList() {
      this.loading = true
      API.getList(this.queryParams).then(
        (res) => {
          this.tableList = res.data.data.records
          this.total = res.data.data.total
          this.loading = false
        },
        (err) => {
          console.log('getList:' + err)
          this.loading = false
        }
      )
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleNearTime(2)
    },
    showStatus(type) {
      if (type !== '') return this.feedbackLabel(this.orderStatus, type.toString())
    },
    showSettleStatus(type) {
      if (type !== '') return this.feedbackLabel(this.settlementStatus, type.toString())
    },
    handleNearTime(status) {
      this.selectBtn = status
      if (status === 1) {
        //近7天
        let obj = this.getDays(6)
        this.queryParams.beginTime = obj.startTime
        this.queryParams.endTime = obj.endTime
      } else if (status === 2) {
        //近30天
        let obj = this.getDays(29)
        this.queryParams.beginTime = obj.startTime
        this.queryParams.endTime = obj.endTime
      }
      this.handleQuery()
    },
    clickCode(row) {
      if (row.dataResource === 0) {
        this.$router.push({
          path: '/shengde/subsidiary/order/management/OrderDetail',
          query: {
            id: row.id,
          },
        })
      } else if (row.dataResource === 1) {
        this.$router.push({
          path: '/shengde/subsidiary/order/management/OrderDetail1',
          query: {
            id: row.id,
          },
        })
      }
    },
    clickSelect(row) {
      this.$emit('clickSelect', row)
    },
    clickDelete(row) {
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          API.deleteContract({ id: row.id }).then(() => {
            this.msgSuccess('删除成功!')
            this.getList()
          })
        })
        .catch(() => {})
    },
    clickAftermarketDetails(id) {
      this.$router.push({
        path: '/afterSales/transactionDetails',
        query: {
          afterSalesId: id,
        },
      })
    },
    clickTaxRate(item) {
      this.form = {
        contractIds: item.id,
        taxRate: item.taxRate,
      }
      this.open = true
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          API.batchEditTaxRate(this.form).then(() => {
            this.msgSuccess('修改完成')
            this.open = false
            this.getList()
          })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.box-card {
  height: calc(100vh - 110px);
  overflow-y: auto;
}
.box-cardTwo {
  height: calc(100vh - 110px - 10px);
  overflow-y: auto;
}
::v-deep {
  .el-input__inner {
    height: 28px;
    line-height: 28px;
  }
  .el-button {
    border-radius: 0;
  }
  .miniBtn .el-button {
    padding: 0px !important;
  }
  .tableHeader th {
    background-color: #edf0f5;
    height: 35px;
    padding: 0;
  }
  .head .el-form-item__label {
    padding: 0;
    color: #940819;
  }
  .head .el-form-item__content {
    min-width: 0;
  }
  .el-input-number {
    .el-input__inner {
      text-align: left !important;
    }
  }
}
.searchBtn {
  background: #20b2aa;
  color: #fff;
  border: 1px solid #20b2aa;
}
.select {
  background: #02a7f0;
  color: #fff;
  border: 1px solid #02a7f0;
}
.aaa {
  border-radius: 10px;
  height: 20px;
  width: 40px;
  display: inline-block;
  background: #f30303;
  vertical-align: top;
  cursor: pointer;
  margin-left: 16px;
}
</style>
