<template>
  <div>
    <div>
      <p-card title="电子监管码录入">
        <p-grid-form
          ref="formTarget"
          :items="gridItems"
          @submit="
            ($event, form) => {
              handleSubmit($event, form, 'formTarget')
            }
          "
        />
        <p-table-edit-pro
          ref="logTable"
          style="margin-top: 10px"
          :columns="tableColumnsConfig"
          rowKey="id"
          :expandRowByClick="false"
          :rowSelectType="1"
          :dataSource.sync="dataSource"
          :loading="tabLoding"
          @selectChange="selectChange"
          dragle
          auto-width
          bordered
        >
        </p-table-edit-pro>
        <a slot="extra">
          <p-button type="primary" @click="add" :disabled="addDis"> 新增 </p-button>
          <p-button type="primary" style="margin-left: 10px" @click="save" :disabled="addDisabled"> 保存 </p-button>
          <p-button type="primary" style="margin-left: 10px" @click="del" :disabled="disabled"> 删除 </p-button>
        </a>
      </p-card>
    </div>
  </div>
</template>
<script>
import { setFormItems, tableColumnsConfig } from './js/config'
import { queryAllBillForSupervisionCode, addItems } from './api/index'
import { getCargoOwner } from '../../utils'

export default {
  data() {
    return {
      dataSource: [],
      tabLoding: false,
      gridItems: setFormItems(this),
      tableColumnsConfig: tableColumnsConfig,
      comments: '',
      disabled: true,
      addDis: true,
      addDisabled: true,
      packLevelName: '',
    }
  },
  created() {
    this.getList()
    this.getCargoOwnerFirst()
  },

  methods: {
    getList() {
      let warehouse = this.$store.state.user.warehouse
      this.$nextTick(() => {
        this.$refs.formTarget.form.resetFields()
        this.$refs.formTarget.form.setFieldsValue({
          userName: this.$store.state.user.name,
          warehouseName: warehouse.warehouseName,
          warehouseId: warehouse.warehouseId,
          warehouseCode: warehouse.warehouseCode,
          packLevel: '0',
        })
      })
    },

    //查询单位名称
    getName() {
      this.$refs.formTarget.form.validateFields((err, value) => {
        if (err) {
          return
        }
        let params = {
          refBillId: value.refBillId,
          warehouseId: value.warehouseId,
          supervisionType: value.supervisionType,
        }
        queryAllBillForSupervisionCode(params).then((res) => {
          if (!res.success) {
            this.$exception(res)
            return
          }
          if (res.data) {
            this.addDis = false
            this.$nextTick(() => {
              this.$refs.formTarget.form.setFieldsValue({ supplierOrShipperName: res.data[0].supplierOrShipperName })
            })
          } else {
            this.$message.warning(this.$lang('没有查询到相应单位名称'))
            this.$nextTick(() => {
              this.$refs.formTarget.form.setFieldsValue({ supplierOrShipperName: '' })
            })
          }
        })
      })
    },
    // 新增行
    add() {
      let packLevel = this.$refs.formTarget.form.getFieldsValue().packLevel
      console.log(packLevel, this.dataSource, 'packLevel')
      this.dataSource.push({
        id: +new Date(),
        packLevel: packLevel,
        supervisionCode: '',
      })
      // 点击新增，打开保存按钮
      this.addDisabled = false
    },
    // 删除
    del() {
      this.$refs.logTable.removeRow(this.comments)
      this.comments = []
      //禁用删除按钮
      if (this.comments.length > 0) {
        this.disabled = false
      } else {
        this.disabled = true
      }
    },
    selectChange(selectedRowKeys, selectedRows) {
      this.comments = selectedRowKeys
      //禁用删除按钮
      if (selectedRowKeys.length > 0) {
        this.disabled = false
      } else {
        this.disabled = true
      }
    },
    //保存
    save() {
      this.$refs.logTable.validateFields((error, values, selectData) => {
        if (error) {
          return
        }
        console.log(values,'values');
        if (!values) {
          this.$message.warning(this.$lang('数据为空，请检查'))
          return
        }
        const formData = this.$refs.formTarget.form.getFieldsValue()
        formData.stage = 4
        let params = values.map((t) => ({
          ...t,
          ...formData,
        }))
        addItems(params).then((res) => {
          if (!res.success) {
            this.$exception(res)
            return
          }
          this.$message.success(this.$lang('新增成功'))
          this.dataSource = []
          this.getList()
          this.addDisabled = true
        })
      })
    },
    
    //获取货主信息
    getCargoOwnerFirst() {
      getCargoOwner().then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        this.$nextTick(() => {
          if (res.data.list.length < 2) {
            this.$refs.table.$refs.form.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerCode: res.data.list[0].companyCode,
              cargoOwnerName: res.data.list[0].companyName,
            })
          }
        })
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-card-body {
  padding: 12px 18px 18px 18px;
}
</style>