<template>
  <div class="detail-container">
    <el-page-header @back="goBack" :content="listItem ? '编辑' : '新增'"></el-page-header>

    <div class="main-title">
      <div class="title">基本信息</div>
    </div>

    <el-form
      ref="mainForm"
      :model="mainForm"
      :rules="mainFormRules"
      label-width="100px"
      size="small"
      label-position="right"
    >
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="单据编号" prop="id">
            <el-input v-model="mainForm.id" placeholder="系统自动生成" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8" style="height: 51px">
          <el-form-item label="申请日期" prop="orderTime">
            <el-date-picker
              v-model="mainForm.orderTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 100%"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="状态" prop="examineStatus">
            <el-select v-model="mainForm.examineStatus" style="width: 100%" disabled>
              <el-option v-for="(item, index) in statusList" :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="经销商编码" prop="customerNumber">
            <el-input v-model="mainForm.customerNumber" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="16">
          <el-form-item label="经销商名称" prop="customerName">
            <el-input v-model="mainForm.customerName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="销售单号" prop="orderNo">
            <el-input v-model="mainForm.orderNo" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="销售单类型" prop="orderType">
            <el-select v-model="mainForm.orderType" style="width: 100%" disabled>
              <el-option v-for="(item, index) in orderTypeList" :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="调拨类型" prop="type">
            <el-select v-model="mainForm.type" style="width: 100%" disabled>
              <el-option v-for="(item, index) in typeList" :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="调出库存组织" prop="requsitionOut">
            <el-input v-model="mainForm.requsitionOut" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="调入库存组织" prop="requsitionIn">
            <el-input v-model="mainForm.requsitionIn" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="调拨方向" prop="direction">
            <el-input v-model="mainForm.direction" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="制单人" prop="createBy">
            <el-input v-model="mainForm.createBy" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="8">
          <el-form-item label="制单日期" prop="createTime">
            <el-date-picker
              v-model="mainForm.createTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 100%"
              disabled
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :xs="12" :sm="6" :lg="6">
          <el-form-item label="调出仓库" prop="correspondId">
            <el-select
              v-model="mainForm.correspondId"
              placeholder="请选择调出仓库"
              style="width: 100%"
              filterable
              @change="changeWarehouse1"
            >
              <el-option v-for="item in warehouseList1" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="12" :sm="6" :lg="6">
          <el-form-item label="调出仓位" prop="stockId">
            <el-select v-model="mainForm.stockId" placeholder="请调出仓位" style="width: 100%" filterable>
              <el-option v-for="item in positionList1" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="12" :sm="6" :lg="6">
          <el-form-item label="调入仓库" prop="inCorrespondId">
            <el-select
              v-model="mainForm.inCorrespondId"
              placeholder="请选择调入仓库"
              style="width: 100%"
              filterable
              @change="changeWarehouse2"
            >
              <el-option v-for="item in warehouseList2" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="12" :sm="6" :lg="6">
          <el-form-item label="调入仓位" prop="inStockId">
            <el-select v-model="mainForm.inStockId" placeholder="请调入仓位" style="width: 100%" filterable>
              <el-option v-for="item in positionList2" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="mainForm.remark" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

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

    <div class="table" style="margin-top: 20px">
      <el-table
        :data="mainForm.results"
        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="materialOldNumber"
          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="totalAmount"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="调出仓库" min-width="100" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.correspondName }}</template>
        </el-table-column>
        <el-table-column align="center" label="调出仓位" min-width="100" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.stockName }}</template>
        </el-table-column>
        <el-table-column align="center" label="调入仓库" min-width="100" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.inCorrespondName }}</template>
        </el-table-column>
        <el-table-column align="center" label="调入仓位" min-width="100" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.inStockName }}</template>
        </el-table-column>
        <el-table-column align="center" label="调出库存状态" min-width="110" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.stockStatusType }}</template>
        </el-table-column>
        <el-table-column align="center" label="调入库存状态" min-width="110" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.inStockStatusType }}</template>
        </el-table-column>
        <el-table-column align="center" label="调出货主" min-width="100" show-overflow-tooltip>
          <template slot-scope="">{{ mainForm.requsitionOut }}</template>
        </el-table-column>
        <el-table-column align="center" label="调入货主" min-width="100" show-overflow-tooltip>
          <template slot-="scope">{{ mainForm.requsitionIn }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="备注"
          prop="remark"
          min-width="160"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
    </div>

    <div class="page-footer">
      <div class="footer" :class="classObj">
        <el-button type="primary" @click="clickSubmitForm('SAVE')">保 存</el-button>
        <el-button type="primary" @click="clickSubmitForm('WAIT')">提交审核</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 { getDetail, editData, getWarehouseList } from '@/api/supply/direct'

export default {
  name: 'DirectForm',
  componentName: 'DirectForm',
  props: ['listItem'],
  data() {
    return {
      statusList: [
        { label: '已保存', value: 'SAVE' },
        { label: '待审核', value: 'WAIT' },
        { label: '审核通过', value: 'OK' },
        // { label: '审核驳回', value: 'FAIL' },,
        { label: '已关闭', value: 'CLOSE' }
      ],
      orderTypeList: [
        { label: '商用工程订单', value: 'TRADE' },
        { label: '家用工程订单', value: 'HOME' },
        { label: '零售单', value: 'RETAIL' },
        { label: '销售政策单', value: 'RETAIL_POLICY' }
      ],
      typeList: [{ label: '组织内调拨', value: 1 }],
      mainForm: {},
      mainFormRules: {
        orderTime: [{ required: true, message: '请选择单据日期', trigger: 'change' }],
        remark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
        correspondId: [{ required: true, message: '请选择调出仓库', trigger: 'change' }],
        stockId: [{ required: true, message: '请选择调出仓位', trigger: 'change' }],
        inCorrespondId: [{ required: true, message: '请选择调入仓库', trigger: 'change' }],
        inStockId: [{ required: true, message: '请选择调入仓位', trigger: 'change' }]
      },
      goodsList: [],

      warehouseList1: [],
      warehouseList2: [],
      positionList1: [],
      positionList2: []
    }
  },

  watch: {
    'mainForm.correspondId': {
      handler(newValue, oldValue) {
        if (newValue) {
          let obj = this.warehouseList1.find(o => o.id == newValue)
          this.mainForm.correspondName = obj.name
          this.positionList1 = obj.kingDeeStocks
        }
      },
      immediate: true
    },
    'mainForm.inCorrespondId': {
      handler(newValue, oldValue) {
        if (newValue) {
          let obj = this.warehouseList2.find(o => o.id == newValue)
          this.mainForm.inCorrespondName = obj.name
          this.positionList2 = obj.kingDeeStocks
        }
      },
      immediate: true
    },
    'mainForm.stockId': {
      handler(newValue, oldValue) {
        if (newValue) {
          let obj = this.positionList1.find(o => o.id == newValue)
          this.mainForm.stockName = obj.name
          this.mainForm.stockStatusType = obj.defStockStatusName
        }
      },
      immediate: true
    },
    'mainForm.inStockId': {
      handler(newValue, oldValue) {
        if (newValue) {
          let obj = this.positionList2.find(o => o.id == newValue)
          this.mainForm.inStockName = obj.name
          this.mainForm.inStockStatusType = obj.defStockStatusName
        }
      },
      immediate: true
    }
  },

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

  async created() {
    await this.getWarehouseList()
    if (this.listItem) {
      this.getDetail()
    }
  },

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

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

    // 获取仓库列表
    async getWarehouseList() {
      const result = new Promise((resolve, reject) => {
        getWarehouseList({
          pageNum: 1,
          pageSize: -1
        }).then(res => {
          this.warehouseList1 = res.data.records
          this.warehouseList2 = res.data.records
          resolve(res.data.records)
        })
      })
      return result
    },

    // 更改调出仓库
    changeWarehouse1() {
      this.mainForm.stockId = ''
    },

    // 更改调入仓库
    changeWarehouse2() {
      this.mainForm.inStockId = ''
    },

    // 保存
    clickSubmitForm(status) {
      this.$refs.mainForm.validate(valid => {
        if (valid) {
          let {
            id,
            orderTime,
            correspondId,
            correspondName,
            stockId,
            stockName,
            inCorrespondId,
            inCorrespondName,
            inStockId,
            inStockName,
            remark
          } = this.mainForm
          let params = {
            id,
            orderTime,
            correspondId,
            correspondName,
            stockId,
            stockName,
            inCorrespondId,
            inCorrespondName,
            inStockId,
            inStockName,
            remark
          }
          params.examineStatus = status
          editData(params).then(res => {
            this.$successMsg('编辑成功')
            this.goBack()
          })
        }
      })
    }
  }
}
</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>
