<template>
  <div>
  <el-card class="box-card">
    <el-row :gutter="10">
      <el-col :span="15">
        <el-input clearable v-model="queryInfo.query" placeholder="请输入订单编号" prefix-icon="el-icon-search" >
          <el-button slot="append" icon="el-icon-search" @click="getPurchaseList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="9">
        <el-button type="primary" @click="openAddPurchaseDialog" icon = "el-icon-circle-plus" >新增</el-button>
        <el-button type="success" @click="check" icon = "el-icon-success">审核</el-button>
        <el-button type="danger" @click="cancel" icon = "el-icon-error">撤销审核</el-button>
      </el-col>
    </el-row>
    <el-table :data="purchaseList.slice((queryInfo.pageNum-1)*queryInfo.pageSize,queryInfo.pageNum*queryInfo.pageSize)"
    style="width: 100%"
    stripe
    border
    fixed
    height="500"
    @selection-change="handleSelectionChange">
        <el-table-column
          fixed="left"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="purchaseId"
          label="采购订单编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="product"
          label="商品名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="barCode"
          label="商品条码"
          width="180">
        </el-table-column>
        <el-table-column
          prop="supplier"
          label="供应商名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="count"
          label="商品总数量"
          width="180">
        </el-table-column>
        <el-table-column
          prop="storehouse"
          label="仓库名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="180">
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="operate"
          label="操作"
          width="180"
          align="center">
          <template slot-scope="scope" >
            <el-button type="primary" icon="el-icon-edit" size="small" @click="open(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="small" @click="deletePurchase(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

     <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
      </el-pagination>

    </el-card>

    <!-- <el-dialog title="添加采购订单" :visible.sync="addPurchaseDialogVisible" width="75%">
       <el-form :model="purchaseForm" :rules="rules" ref="addPurchaseRuleForm" label-width="100px" class="demo-ruleForm" :inline="true">
         <el-form-item label="订单编号" prop="purchaseId">
           <el-input :disabled="true" v-model="purchaseForm.purchaseId"></el-input>
         </el-form-item>
         <el-form-item label="商品名称" prop="product">
           <el-select v-model="purchaseForm.product" placeholder="请选择"  >
               <el-option
                 v-for="product in productList"
                 :key="product.name"
                 :label="product.name"
                 :value="product.name">
               </el-option>
             </el-select>
         </el-form-item>
         <el-form-item label="供应商" prop="supplier">
           <el-select v-model="purchaseForm.supplier" placeholder="请选择"  >
               <el-option
                 v-for="supplier in supplierList"
                 :key="supplier.name"
                 :label="supplier.name"
                 :value="supplier.name">
               </el-option>
             </el-select>
         </el-form-item>
         <el-form-item label="所属仓库" prop="storehouse">
           <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
               <el-option
                 v-for="storehouse in storehouseList"
                 :key="storehouse.name"
                 :label="storehouse.name"
                 :value="storehouse.name">
               </el-option>
             </el-select>
         </el-form-item>
         <el-form-item label="商品数量" prop="count">
            <el-input v-model="purchaseForm.count"></el-input>
         </el-form-item>
       </el-form>

       <div slot="footer" class="dialog-footer">
         <el-button @click="addPurchaseDialogVisible = false">取 消</el-button>
         <el-button type="primary" @click="addPurchase">确 定</el-button>
       </div>
     </el-dialog> -->

     <el-dialog title="添加采购订单" :visible.sync="addPurchaseDialogVisible" width="75%">

      <div class="listconadd">
        <el-form ref="dataForm" :model="purchaseForm" label-position="top" label-width="190px" class="formadd">
          <div class="title" style="display: flex;align-items: center;justify-content: space-between;height:40px">
            <div style="margin-left: 15px;">主体信息</div>
          </div>
          <el-row>
            <el-col :span="3" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">订单日期：</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item class="trightb_item">
                <!-- <span>{{temp.bankName}}</span> -->
                <el-input v-model="purchaseForm.orderNumber" placeholder=""  clearable />
              </el-form-item>
            </el-col>
            <el-col :span="3" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">订单编号：</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="trightb_item" prop="purchaseId">
             <el-input :disabled="true" v-model="purchaseForm.purchaseId"></el-input>
           </el-form-item>
            </el-col>
            <el-col :span="3" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">供货商名称：</span>
              </el-form-item>
            </el-col>
            <el-col :span="5">
            <el-form-item prop="supplier" class="trightb_item">
             <el-select v-model="purchaseForm.supplier" placeholder="请选择"  >
                 <el-option
                   v-for="supplier in supplierList"
                   :key="supplier.name"
                   :label="supplier.name"
                   :value="supplier.name">
                 </el-option>
             </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="3" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">采购申请人：</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item class="trightb_item">
                <el-input v-model="purchaseForm.propr" placeholder="采购申请人" clearable />
              </el-form-item>
            </el-col>

            <el-col :span="3" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">客户名称：</span>
              </el-form-item>
            </el-col>
            <el-col :span="6" class="trightb">
              <el-form-item class="trightb_item">
                <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
                 <el-option
                   v-for="storehouse in storehouseList"
                   :key="storehouse.name"
                   :label="storehouse.name"
                   :value="storehouse.name">
                 </el-option>
               </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="3" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">仓库名称：</span>
              </el-form-item>
            </el-col>
            <el-col :span="5">
            <el-form-item class="trightb_item">
             <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
                 <el-option
                   v-for="storehouse in storehouseList"
                   :key="storehouse.name"
                   :label="storehouse.name"
                   :value="storehouse.name">
                 </el-option>
               </el-select>
             </el-form-item>
            </el-col>
          </el-row>

          <el-collapse v-model="activeNames">
            <el-collapse-item name="1">
              <template slot="title" >
                <span style="margin-left: 15px;">商品信息</span><span class="span" @click.stop="add()">添加</span>
              </template>
              <el-table  :data="purchaseForm.commodityList" border style="width: 100%;"
                :row-style="{height: '40px'}">
                <!-- <el-table-column type="selection" align="center" width="50"/> -->
                <el-table-column label="序号" type="index" width="80" :index="indexMethod" align="center" />
                <el-table-column label="商品名称" prop="name" align="center">
                   <template slot-scope="scope">
                     <el-select v-model="scope.row.product" placeholder="请选择"  >
                         <el-option
                           v-for="product in productList"
                           :key="product.name"
                           :label="product.name"
                           :value="product.name">
                         </el-option>
                       </el-select>
                  </template>
                </el-table-column>
                <el-table-column label="商品编码" prop="number" align="center" />
                <el-table-column label="单位" prop="unit" align="center" />
                <el-table-column label="单价" prop="price" align="center">
                </el-table-column>
                <el-table-column label="重量" prop="weight" align="center">
                </el-table-column>
                <el-table-column label="操作" wid align="center" width="150">
                  <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="doCommoditylDel(scope.$index)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

            </el-collapse-item>
          </el-collapse>
        </el-form>

      </div>

        <div slot="footer" class="dialog-footer">
          <el-button @click="addPurchaseDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addPurchase">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="修改采购订单" :visible.sync="updataPurchaseDialogVisible" width="65%" @close="closeUpdataPurchaseDialog">
         <el-form :model="purchaseForm" :rules="rules" ref="updataPurchaseRuleForm" label-width="100px" class="demo-ruleForm" :inline="true">
           <el-form-item label="订单编号" prop="purchaseId">
             <el-input :disabled="true" v-model="purchaseForm.purchaseId"></el-input>
           </el-form-item>
           <el-form-item label="商品名称" prop="product">
             <el-select v-model="purchaseForm.product" placeholder="请选择"  @change="getProductList">
                 <el-option
                   v-for="product in productList2"
                   :key="product.name"
                   :label="product.name"
                   :value="product.name">
                 </el-option>
               </el-select>
           </el-form-item>
           <el-form-item label="供应商" prop="supplier">
             <el-select v-model="purchaseForm.supplier" placeholder="请选择"  >
                 <el-option
                   v-for="supplier in supplierList"
                   :key="supplier.name"
                   :label="supplier.name"
                   :value="supplier.name">
                 </el-option>
               </el-select>
           </el-form-item>
           <el-form-item label="所属仓库" prop="storehouse">
             <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
                 <el-option
                   v-for="storehouse in storehouseList"
                   :key="storehouse.name"
                   :label="storehouse.name"
                   :value="storehouse.name">
                 </el-option>
               </el-select>
           </el-form-item>
           <el-form-item label="商品数量" prop="count">
              <el-input v-model="purchaseForm.count"></el-input>
           </el-form-item>
         </el-form>

         <div slot="footer" class="dialog-footer">
           <el-button @click="updataPurchaseDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="checkUpdataPurchase">确 定</el-button>
         </div>
       </el-dialog>
  </div>

</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      purchaseList: [],
      activeNames: ['1'],
      addPurchaseDialogVisible: false,
      updataPurchaseDialogVisible: false,
      productList: [],
      productList2: [],
      supplierList: [],
      storehouseList: [],
      selectPurchaseList: [],
      purchaseForm: {
        purchaseId: '',
        product: '',
        barCode: '',
        supplier: '',
        count: '',
        storehouse: '',
        status: '',
        commodityList:[]
      },
      rules: {
        product: [
          { required: true, message: '请输入商品名', trigger: 'blur' }
        ],
        supplier: [
          { required: true, message: '请输入供应商', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        storehouse: [
          { required: true, message: '请输入所属仓库', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.queryInfo.pageSize = val
      this.getPurchaseList()
    },

    handleCurrentChange (val) {
      this.queryInfo.pageNum = val
      this.getPurchaseList()
    },

    async getPurchaseList () {
      const { data: result } = await this.$http.get('/purchase/list', { params: this.queryInfo })
      if (result.status !== 200) return this.$message.error('获取列表失败')
      this.total = result.data.total
      this.purchaseList = result.data.rows
    },

    addPurchase () {
      this.$refs.addPurchaseRuleForm.validate(async validate => {
        if (!validate) return this.$message.error('请填写必填项')
        this.purchaseForm.status = '待审核'
        const { data: result } = await this.$http.post('/purchase/addPurchase', this.purchaseForm)
        if (result.status !== 200) return this.$message.error('添加订单失败')
        this.getPurchaseList()
        this.addPurchaseDialogVisible = false
      })
    },

    async getProductList () {
      const { data: result } = await this.$http.get('/purchase/getProductList', { params: { name: this.purchaseForm.product } })
      if (result.status !== 200) return this.$message.error('获取商品列表失败')
      this.productList = result.data
      this.purchaseForm.barCode = this.productList[0].barCode
    },

    openAddPurchaseDialog () {
      this.addPurchaseDialogVisible = true
      var now = new Date()
      this.purchaseForm.purchaseId = 'IP' + now.getTime()
      console.log('IP' + now.getTime())
      if (this.productList2.length == 0) this.productList2 = this.productList
    },

    async getSupplierList () {
      const { data: result } = await this.$http.get('/purchase/getSupplier')
      if (result.status !== 200) return this.$message.error('获取供应商列表失败')
      this.supplierList = result.data
    },

    async getStorehouseList () {
      const { data: result } = await this.$http.get('/purchase/getStorehouse')
      if (result.status !== 200) return this.$message.error('获取仓库列表失败')
      this.storehouseList = result.data
    },

    closeUpdataPurchaseDialog () {
      this.$refs.updataPurchaseRuleForm.resetFields()
    },

    closeAddPurchaseDialog () {
      this.$refs.addPurchaseRuleForm.resetFields()
    },

    async deletePurchase (purchase) {
      if (purchase.status !== '待审核') return this.$message.error('订单' + purchase.purchaseId + '进行中')
      const result = await this.$confirm('此操作将永久删除订单' + purchase.purchaseId + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (result !== 'confirm') return this.$message.info('用户取消操作')
      const { data: resultDB } = await this.$http.delete(`/purchase/delete/${purchase.purchaseId}`)
      if (resultDB.status !== 200) return this.$message.error('删除订单失败')
      this.getPurchaseList()
      this.$message.success('订单删除成功')
    },

    open (purchase) {
      if (purchase.status !== '待审核') return this.$message.error('订单' + purchase.purchaseId + '进行中')
      this.purchaseForm = purchase
      this.updataPurchaseDialogVisible = true
      if (this.productList2.length == 0) this.productList2 = this.productList
    },

    checkUpdataPurchase () {
      this.$refs.updataPurchaseRuleForm.validate(validate => {
        if (!validate) return this.$message.error('请填写必填项')
        this.updataPurchase(this.purchaseForm)
        this.$message.success('订单修改成功')
        this.updataPurchaseDialogVisible = false
      })
    },

    async updataPurchase (purchase) {
      const { data: result } = await this.$http.put('/purchase/updata', purchase)
      if (result.status !== 200) return this.$message.error('修改订单失败')
      this.getPurchaseList()
    },

    handleSelectionChange (val) {
      this.selectPurchaseList = val
    },

    check () {
      for (var i = 0; i < this.selectPurchaseList.length; i++) {
        if (this.selectPurchaseList[i].status === '待审核') {
          this.selectPurchaseList[i].status = '已审核'
          this.updataPurchase(this.selectPurchaseList[i])
          this.$message.success('订单' + this.selectPurchaseList[i].purchaseId + '审核成功')
        } else if (this.selectPurchaseList[i].status === '已审核') {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '已审核')
        } else {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '进行中')
        }
      }
    },

    cancel () {
      for (var i = 0; i < this.selectPurchaseList.length; i++) {
        if (this.selectPurchaseList[i].status === '已审核') {
          this.selectPurchaseList[i].status = '待审核'
          this.updataPurchase(this.selectPurchaseList[i])
          this.$message.success('订单' + this.selectPurchaseList[i].purchaseId + '撤销审核成功')
        } else if (this.selectPurchaseList[i].status === '待审核') {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '未审核')
        } else {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '进行中')
        }
      }
    },
    doCommoditylDel(){

    },
    add(){

    },
     // 序号
     indexMethod(index) {
        // var pagestart = (this.listQuery.current - 1) * this.listQuery.size
        // var pageindex = index + 1 + pagestart
        return index + 1
      },

  },

  mounted () {
    this.getPurchaseList()
    this.getProductList()
    this.getSupplierList()
    this.getStorehouseList()
  }
}
</script>

<style scoped>
/deep/ .el-collapse-item__header {
    height: 40px;
    font-weight: bold;
    font-size: 16px;
    text-align: left;
    color: #ffffff;
    background-color: #0294d7;
  }

  /deep/ .el-collapse-item__content {
    padding-bottom: 0;
  }
  .trightb {
    display: flex;
    align-items: center;
    text-align: center;
    justify-content: center;
  }

  .trightb_item {
    padding-top: 5px;
  }

  .span {
    margin-left: 50px;
    font-size: 15px;
    font-weight: 400;
  }
  .formadd {
    padding: 10px 40px 0 40px;
    font-size: 16px;
  }
  .formadd .title {
    font-weight: bold;
    font-size: 16px;
    background-color: #0294d7;
    text-align: left;
    color: #ffffff;
  }
  .first_row{
    border-top: 1px solid #e0e3eb;
  }
  .formadd .el-row {
    display: flex;
    flex-wrap: wrap;
    border-left: 1px solid #e0e3eb;
  }
  .formadd  .el-row .el-col {
     border-right: 1px solid #e0e3eb;
     border-bottom: 1px solid #e0e3eb;
     padding: 0 15px;
     min-height: 42px;
     line-height: 1;
  }
  .formadd  .el-row .el-col .el-form-item {
    margin-bottom: 0;
    line-height: 42px;
  }
  .addinputw {
    width: 80%;
    line-height: 42px;
  }
 .el-input__inner {
   height: 36px;
  }
</style>
