<template>
  <div class="diaryList">
    <div class="whiteBlock title">
      <el-form :inline="true" size="medium" :model="formInline" class="demo-form-inline">
        <el-form-item label="车牌号：">
          <el-input v-model="formInline.licensePlate" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="货名：">
          <el-input v-model="formInline.goodsName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="规格：">
          <el-input v-model="formInline.specifications" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" plain>查询</el-button>
          <el-button type="primary" @click="refreshBtn" plain>刷新</el-button>
          <el-button type="primary" @click="openAdd" plain>新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content whiteBlock">
      <vue-scroll style="height: 100%">
        <el-table class="tables" :data="tableList">
          <el-table-column prop="licensePlate" label="车牌号" align="center"></el-table-column>
          <el-table-column prop="forwardingUnit" label="发货单位" align="center"></el-table-column>
          <el-table-column prop="receivingUnit" label="收货单位" align="center"></el-table-column>
          <el-table-column prop="goodsName" label="货名" align="center"></el-table-column>
          <el-table-column prop="specifications" label="规格" align="center"></el-table-column>
          <el-table-column prop="reserve" label="备注" align="center"></el-table-column>
          <el-table-column prop="weighingType" label="过磅类型" align="center">
            <template slot-scope="scope">
              <div>{{ weighTypeList[scope.row.weighingType] }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="addDateTime" label="录入时间" align="center"></el-table-column>
          <el-table-column label="操作" align="center"  width="160">
            <template slot-scope="scope">
              <div class="tableBtns">
                <div @click="editBtn(scope.row)" class="operationText">
                  <i class="el-icon-edit" style="color: #8dacfa; font-size: 16px; margin-right: 2px"></i>
                  <span>编辑</span>
                </div>
                <div @click="deleteBtn(scope.row)" class="operationText">
                  <i class="el-icon-delete" style="color: #eb5760; font-size: 16px; margin-right: 2px"></i>
                  <span>删除</span>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagerBox"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="$store.state.PAGESIZRS"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="Number(total)"
        ></el-pagination>
      </vue-scroll>
    </div>
    <el-dialog :modal-append-to-body="false" title="新增" :visible.sync="dialogVisibleAdd" width="30%" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="150px" :rules="addEditRules">
        <el-form-item label="车牌号码：" prop="licensePlate">
          <el-input v-model="form.licensePlate" style="width:350px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="发货单位：" prop="forwardingUnit">
          <el-select v-model="form.forwardingUnit" placeholder="请选择" style="width:350px">
            <el-option v-for="item in faowardList" :key="item.unitName" :label="item.unitName" :value="item.unitName"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货单位：" prop="receivingUnit">
          <el-select v-model="form.receivingUnit" placeholder="请选择" style="width:350px">
            <el-option v-for="item in receiveList" :key="item.unitName" :label="item.unitName" :value="item.unitName"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货物名称：" prop="goodsName">
          <el-select v-model="form.goodsName" placeholder="请选择" style="width:350px">
            <el-option v-for="item in tradeNameList" :key="item.goodsName" :label="item.goodsName" :value="item.goodsName"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号：" prop="specifications">
          <el-input v-model="form.specifications" style="width:350px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="备注：" prop="reserve">
          <el-input v-model="form.reserve" style="width:350px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="过磅类型：" prop="weighingType">
          <el-select v-model="form.weighingType" placeholder="请选择" style="width:350px">
            <el-option v-for="item in makeAnAppointmentList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noAdd">取 消</el-button>
        <el-button type="primary" @click="okAdd">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :modal-append-to-body="false" title="编辑" :visible.sync="dialogVisibleEdit" width="30%" :before-close="handleClose">
      <el-form ref="editForm" :model="editForm" label-width="150px" :rules="addEditRules">
        <el-form-item label="车牌号码：" prop="licensePlate">
          <el-input v-model="editForm.licensePlate" style="width:350px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="发货单位：" prop="forwardingUnit">
          <el-select v-model="editForm.forwardingUnit" placeholder="请选择" style="width:350px">
            <el-option v-for="item in faowardList" :key="item.unitName" :label="item.unitName" :value="item.unitName"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货单位：" prop="receivingUnit">
          <el-select v-model="editForm.receivingUnit" placeholder="请选择" style="width:350px">
            <el-option v-for="item in receiveList" :key="item.unitName" :label="item.unitName" :value="item.unitName"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货物名称：" prop="goodsName">
          <el-select v-model="editForm.goodsName" placeholder="请选择" style="width:350px">
            <el-option v-for="item in tradeNameList" :key="item.goodsName" :label="item.goodsName" :value="item.goodsName"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号：" prop="specifications">
          <el-input v-model="editForm.specifications" style="width:350px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="备注：" prop="reserve">
          <el-input v-model="editForm.reserve" style="width:350px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="过磅信息：" prop="weighingType">
          <el-select v-model="editForm.weighingType" placeholder="请选择" style="width:350px">
            <el-option v-for="item in makeAnAppointmentList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="okEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getMakeAnAppointmentList,
  addMakeAnAppointmentItem,
  editMakeAnAppointmentItem,
  deleteMakeAnAppointmentItem,
  getReceiptAndShipmentList,
  getTradeNameList,
  weighTypeList
} from '@/assets/js/api/quality.js'

export default {
  components: {
    //   diaryMod,
  },
  data() {
    return {
      addEditRules: {
        licensePlate: [{ required: true, message: '请输入车牌号码', trigger: 'blur' }],
        forwardingUnit: [{ required: true, message: '请选择发货单位', trigger: 'blur' }],
        receivingUnit: [{ required: true, message: '请选择收货单位', trigger: 'blur' }],
        goodsName: [{ required: true, message: '请选择货物名称', trigger: 'blur' }],
        specifications: [{ required: true, message: '请输入规格型号', trigger: 'blur' }],
        weighingType: [{ required: true, message: '请选择过磅类型', trigger: 'blur' }]
      },
      projectSn: '',
      formInline: {
        licensePlate: '',
        goodsName: '',
        specifications: ''
      },
      weighTypeList: {
        1: '采购过磅',
        2: '销售过磅',
        3: '内部过磅',
        4: '其他过磅'
      },
      downloadData: {}, //下载数据
      tableList: [],
      pageNo: 1,
      pageSize: -1,
      total: 0,
      dialogVisibleAdd: false,
      dialogVisibleEdit: false,
      form: {
        licensePlate: '',
        forwardingUnit: '',
        receivingUnit: '',
        goodsName: '',
        specifications: '',
        reserve: '',
        weighingType: ''
      },
      editForm: {
        licensePlate: '',
        forwardingUnit: '',
        receivingUnit: '',
        goodsName: '',
        specifications: '',
        reserve: '',
        weighingType: ''
      },
      editId: '',
      faowardList: [],
      receiveList: [],
      tradeNameList: [],
      makeAnAppointmentList: [],
      editId: '',
      EquipmentList: [],
      devId: '',
      fuel: ''
    }
  },
  created() {
    this.projectSn = this.$store.state.projectSn
    this.getListData()
  },
  methods: {
    getEquipmentList() {
      getAllRefuelingRecordList({ projectSn: this.projectSn }).then(res => {
        this.EquipmentList = res.result
      })
    },
    handleClose(done) {
      done()
    },
    //获取列表数据
    getListData() {
      let data = {
        licensePlate: this.formInline.licensePlate,
        goodsName: this.formInline.goodsName,
        specifications: this.formInline.specifications,
        projectSn: this.projectSn,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }
      getMakeAnAppointmentList(data).then(res => {
        this.tableList = res.result.records
        this.total = res.result.total
      })
    },
    //查询按钮
    onSubmit() {
      this.pageNo = 1
      this.getListData()
    },
    //刷新按钮
    refreshBtn() {
      this.pageNo = 1
      this.pageSize = 10
      this.formInline = {
        buildName: ''
      }
      this.getListData()
    },
    //查看条数
    handleSizeChange(val) {
      this.pageNo = val
      this.pageSize = 10
      this.getListData()
    },
    //查看页
    handleCurrentChange(val) {
      this.pageNo = val
      this.pageSize = 10
      this.getListData()
    },
    openAdd() {
      this.dialogVisibleAdd = true
      this.getEquipmentList()
    },
    getEquipmentList() {
      getReceiptAndShipmentList({ projectSn: this.projectSn, type: 1 }).then(res => {
        this.faowardList = res.result.records
      })
      getReceiptAndShipmentList({ projectSn: this.projectSn, type: 2 }).then(res => {
        this.receiveList = res.result.records
      })
      getTradeNameList({ projectSn: this.projectSn }).then(res => {
        this.tradeNameList = res.result.records
      })
      weighTypeList({ projectSn: this.projectSn }).then(res => {
        this.makeAnAppointmentList = res.result
      })
    },
    noAdd() {
      this.dialogVisibleAdd = false
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
      })
    },
    okAdd() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let data = {
            licensePlate: this.form.licensePlate,
            forwardingUnit: this.form.forwardingUnit,
            receivingUnit: this.form.receivingUnit,
            goodsName: this.form.goodsName,
            specifications: this.form.specifications,
            reserve: this.form.reserve,
            weighingType: this.form.weighingType,
            projectSn: this.projectSn
          }
          addMakeAnAppointmentItem(data).then(res => {
            if (res.code == 200) {
              this.$message.success('新增成功！')
              this.getListData()
              this.dialogVisibleAdd = false
              Object.keys(this.form).forEach(item => {
                this.form[item] = ''
              })
            } else {
              this.$message.success('新增失败！')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    okEdit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let data = {
            licensePlate: this.editForm.licensePlate,
            forwardingUnit: this.editForm.forwardingUnit,
            receivingUnit: this.editForm.receivingUnit,
            goodsName: this.editForm.goodsName,
            specifications: this.editForm.specifications,
            reserve: this.editForm.reserve,
            weighingType: this.editForm.weighingType,
            projectSn: this.projectSn,
            id: this.editId
          }
          editMakeAnAppointmentItem(data).then(res => {
            if (res.code == 200) {
              this.$message.success('编辑成功！')
              this.getListData()
              this.dialogVisibleEdit = false
            } else {
              this.$message.success('编辑失败！')
            }
          })
          this.getListData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //编辑按钮
    editBtn(val) {
      console.log(val)
      this.editForm = { ...val }
      this.editId = val.id
      this.getEquipmentList()
      this.dialogVisibleEdit = true
    },
    //删除按钮
    deleteBtn(val) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let data = {
            id: val.id
          }
          deleteMakeAnAppointmentItem(data).then(res => {
            if (res.code == 200) {
              this.$message.success('删除成功！')
              this.getListData()
            } else {
              this.$message.success('删除失败！')
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style lang="less" scoped>
.diaryList {
  width: 100%;
  height: 100%;
}
.title {
  padding: 20px 15px 0;
  width: 100%;
}
.content {
  margin-top: 15px;
  height: calc(100% - 93px);
}
.table_wrap {
  width: 100%;
  height: 100%;
  padding: 15px 20px;
}
table {
  border-collapse: collapse;
}

table,
td,
th {
  border: 1px solid #dde5f9;
  text-align: left;
  // height: 35px;
  // line-height: 35px;
  padding: 10px 15px;
}

.setTd {
  width: 25%;
}
.site {
  text-align: right;
}
.site2 {
  text-align: center;
}
.ellipsis {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
