<template>
  <div class="detail-container">
    <el-page-header content="审批" @back="goBack" />

    <div class="main-title">
      <div class="title">订单信息 <span style="margin-left: 20px">经销商名称：{{ detailData.customerName }}</span></div>
    </div>

    <div class="diy-table-1">
      <el-row>
        <el-col :span="8" class="item">
          <div class="label">订单号</div>
          <div class="value">{{ detailData.id }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">订单日期</div>
          <div class="value">{{ detailData.theTime }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">单据状态</div>
          <div class="value">{{ detailData.examineStatus | statusFilter }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">业务员</div>
          <div class="value">{{ detailData.k3ServiceName }}</div>
        </el-col>
        <el-col :span="16" class="item">
          <div class="label">文件编号</div>
          <div class="value">
            <el-input v-model="detailData.fileNo" placeholder="填写文件编号" clearable />
            <!-- {{ detailData.fileNo }} -->
          </div>
        </el-col>
        <el-col :span="24" class="item">
          <div class="label">备注</div>
          <div class="value">
            <template v-if="userName">
              <el-input v-model="detailData.remark" placeholder="填写备注" clearable />
            </template>
            <template v-else>
              {{ detailData.remark }}
            </template>
          </div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">制单人</div>
          <div class="value">{{ detailData.createName }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">审核人</div>
          <div class="value">{{ detailData.examineName }}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">关闭人</div>
          <div class="value">{{ detailData.closeName }}</div>
        </el-col>
        <el-col :span="24" class="item">
            <div class="label">是否直调</div>
            <div class="value">{{ detailData.isAllDirect?'是':'否' }}</div>
          </el-col>
      </el-row>
    </div>

    <div class="main-title">
      <div class="title">货品信息</div>
    </div>

    <div class="table" style="margin-top: 20px">
      <el-table
        :data="detailData.retailOrderItemList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        stripe
        max-height="400"
        show-summary
        :summary-method="$getSummaries"
      >
        <el-table-column align="center" label="序号" type="index" width="50" />
        <el-table-column
          align="left"
          label="销售类型"
          prop="saleTypeName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          align="left"
          label="物料编码"
          prop="materialCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          align="left"
          label="产品编码"
          prop="materialOldNumber"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          align="left"
          label="产品名称"
          prop="materialName"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          align="left"
          label="规格型号"
          prop="specification"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column align="left" label="单位" prop="unit" min-width="100" show-overflow-tooltip />
        <el-table-column
          align="left"
          label="文件编号"
          prop="fileNo"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column align="right" label="单价" prop="price" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.price | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column align="right" label="数量" prop="qty" min-width="100" show-overflow-tooltip />
        <el-table-column align="center" label="退订数量" prop="retiredQty" min-width="100" show-overflow-tooltip />

        <el-table-column align="right" label="订单金额" prop="totalAmount" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.totalAmount | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="返利类型"
          prop="customerWalletName2"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column align="right" label="返利金额" prop="payRebateAmount" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.payRebateAmount | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column align="right" label="返利比例" min-width="100" prop="rebateRate" show-overflow-tooltip />

        <el-table-column align="right" label="格力折扣" prop="totalDiscAmount" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.totalDiscAmount | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="现金钱包"
          prop="customerWalletName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column align="right" label="实付金额" prop="payAmount" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.payAmount | numToFixed }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="是否直调" prop="isDirectTransfer" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.isDirectTransfer ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column
          align="right"
          label="直调数量"
          prop="directTransferQty"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          align="right"
          label="原订单数量"
          prop="oldQty"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          align="left"
          label="备注"
          prop="remark"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column align="right" label="税率" prop="tax" min-width="100" show-overflow-tooltip />
      </el-table>
    </div>

    <div class="main-title">
      <div class="title">审批信息</div>
    </div>
    <div class="diy-table-1">
      <el-row :gutter="0">
        <el-col :span="12" class="item">
          <div class="label">审批人</div>
          <div class="value">{{ userName }}</div>
        </el-col>
        <el-col :span="12" class="item">
          <div class="label">审批结果</div>
          <div class="value">
            <el-radio-group v-model="mainForm.status">
              <el-radio :label="true">通过</el-radio>
              <el-radio :label="false">驳回</el-radio>
            </el-radio-group>
          </div>
        </el-col>
        <el-col :span="24" class="item">
          <div class="label">审批说明</div>
          <div class="value">
            <el-input v-model="mainForm.remark" placeholder="请输入内容" />
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="page-footer">
      <div class="footer">
        <el-button type="primary" :loading="formLoading" @click="clickSubmitForm">{{
          formLoading ? '提交中 ...' : '提 交'
        }}</el-button>
        <el-button @click="goBack">返回列表</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getDetail, examineData } from '@/api/supply/retail'

export default {
  name: 'RetailExamine',
  componentName: 'RetailExamine',
  filters: {
    statusFilter(val) {
      const statusList = [
        { label: '已保存', value: 'SAVE' },
        { label: '待审核', value: 'WAIT' },
        { label: '审核通过', value: 'OK' }
        // { label: '审核驳回', value: 'FAIL' },,
      ]
      const obj = statusList.find(o => o.value == val)
      return obj ? obj.label : ''
    }
  },
  props: ['listItem'],
  data() {
    return {
      detailData: {},
      userName: JSON.parse(localStorage.getItem('supply_user')).nickName,

      formLoading: false,
      mainForm: {
        status: true,
        remark: ''
      }
    }
  },

  created() {
    this.getDetail()
  },

  methods: {
    // 返回列表
    goBack() {
      this.$emit('backListFormDetail')
    },

    // 获取详情
    getDetail() {
      getDetail({ id: this.listItem.id }).then(res => {
        if (res.data.retailOrderItemList) {
          res.data.retailOrderItemList.forEach(item => {
            item.sums1 = ['directTransferQty', 'qty', 'refundableQty', 'oldQty']
            item.sums2 = ['totalAmount', 'payAmount', 'price', 'payRebateAmount', 'discAmount', 'totalDiscAmount']
          })
        }
        this.detailData = res.data
      })
    },

    // 提交审批
    clickSubmitForm() {
      this.formLoading = true
      examineData({
        id: this.listItem.id,
        examineStatus: this.mainForm.status ? 'OK' : 'FAIL',
        examineRemark: this.mainForm.remark,
        remark: this.detailData.remark,
        fileNo: this.detailData.fileNo
      })
        .then(res => {
          this.$successMsg('审批成功')
          this.goBack()
        })
        .finally(res => {
          this.formLoading = false
        })
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  width: 100%;
  height: 100%;
}
.main-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  height: 60px;
  border-bottom: 1px solid #dcdfe6;
  margin-bottom: 20px;
  .title {
    font-size: 16px;
    font-weight: 600;
    padding-left: 10px;
  }
}
</style>
