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

    <div class="main-title">
      <div class="title">提货进度</div>
    </div>
    <div class="progress-container">
      <el-progress :text-inside="true" :stroke-width="26" :percentage="progress || 0"></el-progress>
    </div>

    <div class="main-title">
      <div class="title">订单信息</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="16" class="item">
          <div class="label">备注</div>
          <div class="value">{{detailData.remark}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">业务员</div>
          <div class="value">{{detailData.serviceName}}</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.createTime}}</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.examineTime}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">关闭人</div>
          <div class="value">{{detailData.closeName}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">关闭日期</div>
          <div class="value">{{detailData.closeTime}}</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">
        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column align="center" label="销售类型" prop="saleTypeName" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="产品编码" prop="materialCode" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="产品名称" prop="materialName" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="规格型号" prop="specification" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="单位" prop="unit" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="单价" prop="price" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="数量" prop="qty" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="订单金额" prop="totalAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="返利钱包" prop="customerWalletName2" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="返利金额" prop="rebateAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="格力折扣" prop="totalDiscAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="现金钱包" prop="customerWalletName" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="实付金额" prop="payAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="是否直调" prop="isDirectTransfer" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.isDirectTransfer ? '是':'否'}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="直调数量" prop="directTransferQty" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="原订单数量" prop="oldQty" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="备注" prop="remark" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="税率" prop="tax" min-width="100" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>

    <div v-if="isExamine">
      <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">{{detailData.examineName}}</div>
          </el-col>
          <el-col :span="12" class="item">
            <div class="label">审批结果</div>
            <div class="value">{{detailData.examineStatus | statusFilter}}</div>
          </el-col>
          <el-col :span="24" class="item">
            <div class="label">审批说明</div>
            <div class="value">{{detailData.examineRemark}}</div>
          </el-col>
        </el-row>
      </div>
    </div>
    
    <div class="page-footer">
      <div class="footer" :class="classObj">
        <el-button type="primary" @click="openDeliverDialog" :disabled="detailData.examineStatus !== 'OK'">直调发货</el-button>
        <el-button @click="goBack">关 闭</el-button>
      </div>
    </div>

    <el-dialog title="直调发货" :visible.sync="isShowDeliverDialog" width="80%">
      <el-form ref="deliverForm" :model="deliverForm" :rules="deliverFormRules" label-width="80px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="8" :sm="8" :lg="8" style="height: 51px;">
            <el-form-item label="发货日期" prop="date">
              <el-date-picker
                v-model="deliverForm.date"
                type="date"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="8" :sm="8" :lg="8">
            <el-form-item label="发货仓库" prop="warehouse">
              <el-select v-model="deliverForm.warehouse" placeholder="请选择发货仓库" style="width: 100%" filterable @change="changeWarehouse">
                <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="8" :sm="8" :lg="8">
            <el-form-item label="选择仓位" prop="position">
              <el-select v-model="deliverForm.position" placeholder="请选择仓位" style="width: 100%" filterable>
                <el-option v-for="item in positionList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="table" style="margin-top: 20px">
        <el-table :data="goodsList" element-loading-text="Loading" border fit highlight-current-row stripe max-height="400">
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column align="center" label="销售类型" prop="saleTypeName" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="产品编码" prop="materialCode" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="产品名称" prop="materialName" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="规格型号" prop="specification" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="单位" prop="unit" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="订单数量" prop="qty" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="发货数量" prop="hasSendQty" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="直调数量" prop="adjustNum" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input v-model="scope.row.adjustNum" size="small" type="number"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="单价" prop="price" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="金额" prop="totalAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="返利" prop="rebateAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="实付金额" prop="payAmount" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="备注" prop="remark" min-width="160" show-overflow-tooltip></el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDeliverDialog">取 消</el-button>
        <el-button type="primary" @click="submitDeliverForm">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getDetail, getWarehouseList, adjustDeliver } from "@/api/supply/retail";

export default {
  name: 'RetailDetail',
  componentName: 'RetailDetail',
  props: ['listItem'],
  filters: {
    statusFilter(val) {
      const statusList = [
        { label: '已保存', value: 'SAVE' },
        { label: '待审核', value: 'WAIT' },
        { label: '审核通过', value: 'OK' },
        { label: '审核驳回', value: 'FAIL' },
      ];
      let obj = statusList.find(o => o.value == val);
      return obj ? obj.label : ''
    }
  },
  data() {
    return {
      detailData: {},

      isShowDeliverDialog: false,
      deliverForm: {
        date: '',
        warehouse: '',
        position: '',
      },
      deliverFormRules: {
        date: [
          { required: true, message: '请选择发货日期', trigger: 'change' }
        ],
        warehouse: [
          { required: true, message: '请选择发货仓库', trigger: 'change' }
        ],
        position: [
          { required: true, message: '请选择仓位', trigger: 'change' }
        ],
      },
      warehouseList: [],
      positionList: [],

      goodsList: [],
    }
  },

  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened
      }
    },
    isExamine() {
      return this.detailData.examineStatus === 'OK' || this.detailData.examineStatus === 'FAIL'
    },
    progress() {
      if(this.detailData.totalQty == 0) return 0;
      return (this.detailData.hasSendQty * 100) / (this.detailData.totalQty * 100);
    }
  },

  created() {
    this.getDetail();
  },

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

    // 获取详情
    getDetail() {
      getDetail({id: this.listItem.id}).then(res => {
        this.detailData = res.data;
      })
    },

    // 获取仓库列表
    getWarehouseList() {
      getWarehouseList({
        pageNum: 1,
        pageSize: -1,
      }).then(res => {
        this.warehouseList = res.data.records;
      })
    },

    // 更改仓库
    changeWarehouse() {
      this.deliverForm.position = '';
      let obj = this.warehouseList.find(o => o.id == this.deliverForm.warehouse);
      this.positionList = obj.kingDeeStocks;
    },

    // 打开 直调发货
    openDeliverDialog() {
      this.isShowDeliverDialog = true;
      this.goodsList = this.detailData.retailOrderItemList;
      this.getWarehouseList();
    },

    // 关闭 直调发货
    closeDeliverDialog() {
      this.isShowDeliverDialog = false;
    },

    // 提交 直调发货
    submitDeliverForm() {
      this.$refs.deliverForm.validate((valid) => {
        if (valid) {
          for(let i=0; i<this.goodsList.length; i++) {
            if(!this.goodsList[i].adjustNum) {
              this.$errorMsg('请输入直调数量');
              return;
            }
          }
          let goodsList = this.goodsList.map((item) => {
            return {
              itemId: item.id,
              qty: item.adjustNum,
            }
          });
          let params = {
            orderNo: this.listItem.id,
            orderDate: this.deliverForm.date + ' 00:00:00',
            correspondId: this.deliverForm.warehouse,
            stockId: this.deliverForm.position,
            directItems: goodsList
          }
          adjustDeliver(params).then(res => {
            this.$successMsg();
            this.isShowDeliverDialog = false;
            this.getDetail();
          })
        }
      })
    },

  }
}
</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;
    }
  }
  .progress-container {
    display: flex;
    .el-progress {
      width: 500px;
    }
  }
</style>