<template>
  <div class="fullHeight whiteBlock">
    <div style="padding: 15px 15px 0">
      <p class="pageTitle">部门或外加工管理</p>
      <el-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        size="medium"
      >
       
        <el-form-item
          label="名称"
          prop="materialName"
        >
          <el-input
            v-model="searchForm.materialName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="联系人"
          prop="materialName"
        >
          <el-input
            v-model="searchForm.materialName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
       
        <el-form-item
          label="联系电话"
          prop="materialName"
        >
          <el-input
            v-model="searchForm.materialName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
       
        <el-form-item>
          <el-button type="primary" plain @click="getList">{{
            $t('message.energyManage.waybill.query')
          }}</el-button>
          <el-button type="warning" plain @click="refresh">{{
            $t('message.deviceManage.refresh')
          }}</el-button>
          <el-button type="primary" size="medium" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List">
      
        <el-table-column
          prop="wayBillNumber"
          label="名称"
          align="center"
        ></el-table-column>
       
        <el-table-column
          prop="materialSpecifications"
          align="center"
          label="联系人"
        ></el-table-column>
        <el-table-column
          prop="computeUnit"
          align="center"
          label="电话"
        ></el-table-column>
        <el-table-column
          prop="wayBillNum"
          label="备注"
          align="center"
        >
        </el-table-column>
        
        <el-table-column
          :label="$t('message.deviceManage.operation')"
          align="center"
        >
          <template slot-scope="scope">
            <div class="tableBtns">
              <div @click="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>{{ $t('message.deviceManage.edit') }}</span>
              </div>
              <div @click="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>{{ $t('message.deviceManage.delete') }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="pagInfo.pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pagInfo.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(pagInfo.total)"
        background
      ></el-pagination>
    </div>
    <el-dialog
      :modal-append-to-body="false"
      @close="close"
      :title="
        Popup.type == 'edit'
          ? $t('message.energyManage.material.edit')
          : $t('message.energyManage.material.add')
      "
      :visible.sync="Popup.show"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-width="120px"
          class="dialogFormBox"
        >
          
         
          <el-form-item
            label="仓库名称"
            prop="wayBillNumber"
          >
            <el-input
              v-model="addEditForm.wayBillNumber"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="仓库位置"
            prop="wayBillNum"
          >
            <el-input
              v-model="addEditForm.wayBillNum"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="负责人"
            prop="carNumber"
          >
            <el-input
              v-model="addEditForm.carNumber"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="负责人电话"
            prop="carNumber"
          >
            <el-input
              v-model="addEditForm.carNumber"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="备注"
            prop="carNumber"
          >
            <el-input
              v-model="addEditForm.carNumber"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
        
       
          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              @click="Popup.show = false"
              icon="el-icon-circle-close"
              size="medium"
              >{{ $t('message.deviceManage.cancel') }}
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submit"
              size="medium"
              >{{ $t('message.deviceManage.save') }}
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addmaterialPurchaseWayBillApi,
  editmaterialPurchaseWayBillApi,
  deletematerialPurchaseWayBillApi,
  materialPurchaseWayBillPageApi,
  selectProcurementContractMaterialListApi,
  selectProjectCooperationUnitListApi,
  getMaterialPurchaseInfoApi,
  materialPurchaseOrderPageApi
} from '@/assets/js/api/materialManage'
export default {
  mounted() {
    this.getFeedUnitList()
    this.getPurchaseOrderList()
    this.getList()
  },
  data() {
    return {
      addEditRules: {
        purchaseOrderId: [
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'change'
          }
        ],
        materialId: [
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'change'
          }
        ],
        wayBillNumber: [
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'change'
          }
        ],
        wayBillNum: [
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'change'
          }
        ],
        carNumber: [
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'change'
          }
        ],
        deliveryTime: [
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.carManage.required'),
            trigger: 'change'
          }
        ]
      },
      addEditForm: {
        carNumber: '',
        deliveryTime: '',
        materialId: '',
        projectSn: '',
        purchaseOrderId: '',
        wayBillNum: '',
        wayBillNumber: ''
      },
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      Popup: {
        type: 'add',
        show: false
      },
      allCompanyList: [],
      contractList: [],
      feedUnitList: [],
      searchForm: {
        materialName: '',
        enterpriseId: ''
      },
      purchaseOrderList: [],
      orderMaterialList: []
    }
  },
  methods: {
    //获取供料单位列表
    getFeedUnitList() {
      let data = {
        projectSn: this.$store.state.projectSn,
        cooperationType: 1
      }
      selectProjectCooperationUnitListApi(data).then((res) => {
        this.feedUnitList = res.result
      })
    },
    //获取采购订单列表
    getPurchaseOrderList() {
      let json = {
        projectSn: this.$store.state.projectSn,
        pageNo: 1,
        pageSize: 100,
        state: 2
      }
      materialPurchaseOrderPageApi(json).then((result) => {
        this.purchaseOrderList = result.result.records
      })
    },
    //获取订单详细
    getPurchaseOrderDetail() {
      getMaterialPurchaseInfoApi({
        purchaseOrderId: this.addEditForm.purchaseOrderId
      }).then((res) => {
        this.orderMaterialList = res.result.itemList
      })
    },
    handle(type, show) {
      //打开弹窗前的统一处理
      this.Popup = {
        type: type,
        show: show
      }
    },
    changeMaterial() {
      this.allCompanyList.forEach((element) => {
        if (element.id == this.addEditForm.materialId) {
          // this.addEditForm.contractId=element.contractId
          this.addEditForm.computeUnit = element.computeUnit
        }
      })
    },
    add() {
      this.handle('add', true)
    },
    edit(obj) {
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.handle('edit', true)
    },
    deleteDev(obj) {
      console.log('删除', obj)
      this.$confirm(
        this.$t('message.personnelPosition.beaconManage.table.confirmText') +
          '【' +
          obj.wayBillNumber +
          '】?',
        this.$t('message.personnelPosition.beaconManage.table.Tips'),
        {
          confirmButtonText: this.$t(
            'message.personnelPosition.confirmButtonText'
          ),
          cancelButtonText: this.$t(
            'message.personnelPosition.cancelButtonText'
          ),
          type: 'warning'
        }
      ).then(() => {
        deletematerialPurchaseWayBillApi({ id: obj.id }).then((result) => {
          if (result.success) {
            this.$message.success(result.message)
            this.getList()
          }
        })
      })
    },
    submit() {
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.Popup.type === 'add') {
            addmaterialPurchaseWayBillApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.Popup.type === 'edit') {
            console.log('编辑信息', this.addEditForm)
            editmaterialPurchaseWayBillApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.Popup.show = false
        } else {
          return false
        }
      })
    },
    refresh() {
      this.$refs['searchForm'].resetFields()
      this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getList()
    },
    getList() {
      materialPurchaseWayBillPageApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        enterpriseId: this.searchForm.enterpriseId,
        materialName: this.searchForm.materialName
      }).then((result) => {
        if (result.success) {
          this.List = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },
    close() {
      this.addEditForm = {}
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getList()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getList()
    }
  }
}
</script>
<style lang="less" scoped>
.tables2 {
  min-height: auto;
}
// .tableBtns {
//     display: flex;
//     justify-content: center;
// }
</style>