<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="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.createTime}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">单据状态</div>
          <div class="value">{{detailData.status | statusFilter}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">经销商编号</div>
          <div class="value">{{detailData.customerNumber}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">经销商名称</div>
          <div class="value">{{detailData.customerName}}</div>
        </el-col>
        <el-col :span="8" class="item">
          <div class="label">仓库</div>
          <div class="value">{{detailData.correspondName}}</div>
        </el-col>
        <el-col :span="24" 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.createBy}}</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"></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="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="stockIds" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-select v-model="scope.row.stockIds" placeholder="请选择发货仓库" size="small" multiple filterable @change="changeWarehouse(scope.$index)">
              <el-option :label="item.name" :value="item.id" v-for="(item, index) in positionList" :key="index"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" label="库存数" prop="stockNumber" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="销售订单号" prop="retailOrderId" min-width="180" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="产品编码" prop="materialCode" min-width="120" 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="invoiceNum" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="审批数量" prop="approvalNumber" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input v-model="scope.row.approvalNumber" size="small" type="number"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" prop="remark" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" size="small"></el-input>
          </template>
        </el-table-column>
      </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="examineForm.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="examineForm.remark" placeholder="请输入内容"></el-input></div>
        </el-col>
      </el-row>
    </div>
    
    <div class="page-footer">
      <div class="footer" :class="classObj">
        <el-button type="primary" @click="clickSubmitForm" :loading="formLoading">{{ formLoading ? '提交中 ...' : '提 交' }}</el-button>
        <el-popconfirm
          title="确定关闭吗？"
          @onConfirm="goBack"
          style="margin-left: 10px;"
        >
          <el-button slot="reference">关 闭</el-button>
        </el-popconfirm>
      </div>
    </div>

  </div>
</template>

<script>
import { getApplyDetail, examineApply, getPositionList, checkStock } from "@/api/supply/apply";

export default {
  name: 'ApplyExamine',
  componentName: 'ApplyExamine',
  props: ['listItem'],
  filters: {
    statusFilter(val) {
      const statusList = [
        { label: '已保存', value: 1 },
        { label: '待审核', value: 2 },
        { label: '审核通过', value: 3 },
        { label: '审核驳回', value: 4 },
      ];
      let obj = statusList.find(o => o.value == val);
      return obj ? obj.label : ''
    }
  },
  data() {
    return {
      detailData: {},
      goodsList: [],
      userName: JSON.parse(localStorage.getItem("supply_user")).nickName,

      formLoading: false,
      examineForm: {
        status: true,
        remark: '',
      },
      positionList: [],
    }
  },

  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened
      }
    },
  },

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

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

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

        res.data.orders.forEach((item, index) => {
          if(item.stockIds && item.stockIds.length > 0) {
            item.stockIds = item.stockIds.map((it, idx) => {
              return it.id;
            });
          }
        });

        this.goodsList = res.data.orders;
        this.getPositionList();
      })
    },

    // 获取仓位列表
    getPositionList() {
      getPositionList({
        correspondId: this.detailData.correspondId
      }).then((res) => {
        this.positionList = res.data;
      })
    },

    // 修改仓位
    changeWarehouse(index) {
      if(this.goodsList[index].stockIds && this.goodsList[index].stockIds.length > 0) {
        checkStock({
          materialId: this.goodsList[index].materialId,
          StockIds: this.goodsList[index].stockIds.join(',')
        }).then(res => {
          this.goodsList[index].stockNumber = res.data;
        })
      }else {
        this.goodsList[index].stockNumber = 0;
      }
    },

    // 提交审批
    clickSubmitForm() {
      for(let i=0; i<this.goodsList.length; i++) {
        if(this.goodsList[i].stockIds && this.goodsList[i].stockIds.length < 1) {
          this.$errorMsg('请选择发货仓库');
          return;
        }
        if(this.goodsList[i].stockNumber < 1) {
          this.$errorMsg('库存不足');
          return;
        }
        if(this.goodsList[i].approvalNumber > this.goodsList[i].stockNumber) {
          this.$errorMsg('审批数量不可大于库存数');
          return;
        }
        if(this.goodsList[i].approvalNumber > this.goodsList[i].invoiceNum) {
          this.$errorMsg('审批数量不可大于申请数量');
          return;
        }
      }

      this.formLoading = true;
      examineApply({
        id: this.listItem.id,
        status: this.examineForm.status ? 3 : 4,
        approvalRemark: this.examineForm.remark,
        orders: this.goodsList
      }).then(res => {
        this.$successMsg('审批成功');
        this.goBack();
        this.$parent.getList();
      }).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;
    }
  }

  ::v-deep input::-webkit-outer-spin-button,
  ::v-deep input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  ::v-deep input[type='number'] {
    -moz-appearance: textfield;
  }
</style>