
<template>
  <div class="bg" id="index">
    <div class="row1">
      <div class="right">
        <el-button type="primary" size="small" @click="add" class="add">添加</el-button>
      </div>
    </div>
    <el-table ref="singleTable" :data="goodsList" highlight-current-row style="width: 100%;" class="table">
      <el-table-column type="index" width="50" label="序号">
      </el-table-column>
      <el-table-column property="goodsName" label="货名" width="200">
      </el-table-column>
      <el-table-column property="storage" label="剩余库存（吨）" width="200">
      </el-table-column>
      <el-table-column property="setDefault" label="默认货名" width="200">
        <template scope="scope">
          <!-- <el-checkbox  v-hasPermission="`speedEdition.goodsNameJsb.default` " v-model="scope.row.checked" @change="change($event,scope.row)">设为默认</el-checkbox> -->
          <el-checkbox  v-model="scope.row.checked" @change="change($event,scope.row)">设为默认</el-checkbox>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作"  show-overflow-tooltip>
        <template scope="scope">
          <!-- <span style="color: #409EFF;margin-right:5px" @click="edit(scope.row)">编辑</span> -->
          <!-- <span  v-hasPermission="`speedEdition.goodsNameJsb.delete` " style="color: red;" @click="del(scope.row)">删除</span> -->
          <span   style="color: red;" @click="del(scope.row)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog width="320px" :title="goodsNameTitle" :visible.sync="show" :append-to-body="true" :close="inventoryClose">
      <el-form class="goodsName" label-position="right" label-width="80px">
        <el-form-item class="customer-item" label="货名">
          <el-input placeholder="请输入货名" maxlength="100" size="small" v-model="dataObj.goodsName" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="goodsNameConfirm" type='primary'>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import {
  speedGoodsList,
  speedGoodsAdd,
  speedGoodsEdit,
  speedGoodsDel

} from '@/model/speedGoods'
import { floralwhite } from 'color-name';
export default {
  components: {
  },
  data() {
    return {
      goodsNameTitle: '新增',
      dataObj: {
        compId: localStorage.getItem('ws-pf_compId'),
        goodsName: ''
      },
      deptCircularPage: {},
      deptBudgetTotal: 0,
      currentPage: 1,
      pageSize: 10,
      show: false,
      checked: true,
      goodsList: [],
    };
  },
  filters: {
    numsFilter(msg) {
      return msg || 0;
    },
  },
  created() {
    this.getList()
  },
  mounted() {


  },
  beforeDestroy() {


  },
  methods: {
    change(val1,val2){
      console.log(val1,val2)
      if(val1){
        val2.setDefault=1
      }else{
        val2.setDefault=0
      }
      val2.flag=1
      speedGoodsEdit(val2)
          .toPromise()
          .then((response) => {
            debugger
            if (response == "ok") {
              this.show = false
              this.$message.success('操作成功！')
              this.getList()
            }
            console.log(response)
          })

    },
    getList() {
      speedGoodsList({
        compId: localStorage.getItem('ws-pf_compId')
      })
        .toPromise()
        .then((response) => {
          console.log(response)
          this.goodsList = response
          for(let i = 0;i<this.goodsList.length;i++){
            if(this.goodsList[i].setDefault==0){
              this.goodsList[i].checked = false
            }else{
              this.goodsList[i].checked = true
            }
          }
        })
    },
    inventoryClose() {
      this.show = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      // this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
      // this.getList()
    },
    add() {
      this.show = true
    },
    edit(val) {
      console.log(val)
      this.goodsNameTitle = "编辑"
      this.dataObj = val
      this.show = true
    },
    del(val) {
      if(val.storage&&val.storage>0){
        this.$message.error('当前'+val.goodsName+'有剩余库存！')
        return
      }
      this.$confirm('确定删除货名？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        speedGoodsDel({ id: val.id })
          .toPromise()
          .then((response) => {
            debugger
            if (response == "200") {
              this.show = false
              this.$message.success('删除成功！')
              this.getList()
            }
            console.log(response)
          })

      }).catch(() => {
        return false
      });
    },
    goodsNameConfirm() {
      if (this.goodsNameTitle == '新增') {
        speedGoodsAdd(this.dataObj)
          .toPromise()
          .then((response) => {
            debugger
            if (response == "ok") {
              this.$message.success('操作成功！')
              this.show = false
              this.getList()
            }
            console.log(response)
          })
      } else {
        speedGoodsEdit(this.dataObj)
          .toPromise()
          .then((response) => {
            debugger
            if (response == "ok") {
              this.show = false
              this.$message.success('操作成功！')
              this.getList()
            }
            console.log(response)
          })
      }

    }
  },
};
</script>
<style lang="scss" scoped>
.bg {
  width: 100%;
  height: 100%;
  padding: 16px 16px 10px 16px;
  box-sizing: border-box;
  // background-image: url("../../assets/images/pageBg.png");
  background-size: cover;
  background-position: center center;
}

.table {
  margin-top: 20px;
}
</style>
  