<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.forwardName"
            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="unitName"
            label="发货单位"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="unitChargeName"
            label="单位负责人"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="chargePhone"
            label="负责人电话"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="unitAddress"
            label="单位地址 "
            align="center"
          ></el-table-column>
          <el-table-column
            prop="socialCode"
            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>
      <el-dialog
        title="新增"
        :modal-append-to-body="false"
        :visible.sync="dialogVisibleAdd"
        width="30%"
        :before-close="handleClose"
      >
        <el-form
          ref="form"
          :model="form"
          label-width="150px"
          :rules="addEditRules"
        >
          <el-form-item label="发货单位：" prop="unitName">
            <el-input
              v-model="form.unitName"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="单位负责人：" prop="unitChargeName">
            <el-input
              v-model="form.unitChargeName"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="负责人电话：" prop="chargePhone">
            <el-input
              v-model="form.chargePhone"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="单位地址：" prop="unitAddress">
            <el-input
              v-model="form.unitAddress"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="统一社会信用代码：" prop="socialCode">
            <el-input
              v-model="form.socialCode"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </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="unitName">
            <el-input
              v-model="editForm.unitName"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="单位负责人：" prop="unitChargeName">
            <el-input
              v-model="editForm.unitChargeName"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="负责人电话：" prop="chargePhone">
            <el-input
              v-model="editForm.chargePhone"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="单位地址：" prop="unitAddress">
            <el-input
              v-model="editForm.unitAddress"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="统一社会信用代码：" prop="socialCode">
            <el-input
              v-model="editForm.socialCode"
              style="width: 350px"
              placeholder="请输入"
            ></el-input>
          </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>
  </div>
</template>
<script>
import {
  getReceiptAndShipmentList,
  addReceiptAndShipmentItem,
  editReceiptAndShipmentItem,
  deleteReceiptAndShipmentItem
} from '@/assets/js/api/quality.js'
import {
  checkPhone,
  CheckSocialCreditCode,
} from '@/assets/js/util.js'
export default {
  components: {
    //   diaryMod,
  },
  data() {
    return {
      addEditRules: {
        unitName: [{ required: true, message: '请输入发货单位', trigger: 'blur' }],
        unitChargeName: [{ required: true, message: '请输入单位负责人', trigger: 'blur' }],
        chargePhone: [{ required: true, message: '请输入负责人电话', trigger: 'blur' }],
        unitAddress: [{ required: true, message: '请输入单位地址', trigger: 'blur' }],
        socialCode: [{ required: true, message: '请输入统一社会信用代码', trigger: 'blur' }]
      },
      projectSn: '',
      formInline: {
        forwardName: ''
      },
      downloadData: {}, //下载数据
      tableList: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      dialogVisibleAdd: false,
      dialogVisibleEdit: false,
      form: {
        unitName: '',
        unitChargeName: '',
        chargePhone: '',
        unitAddress: '',
        socialCode: ''
      },
      editForm: {
        unitName: '',
        unitChargeName: '',
        chargePhone: '',
        unitAddress: '',
        socialCode: ''
      },
      equipmentId: '',
      editId: '',
      EquipmentList: [],
      devId: '',
      fuel: ''
    }
  },
  created() {
    this.projectSn = this.$store.state.projectSn
    this.getListData()
  },
  methods: {
    openAdd() {
      this.dialogVisibleAdd = true
    },
    noAdd() {
      this.dialogVisibleAdd = false
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
      })
    },
    okAdd() {
      if (this.form.chargePhone) {
        if (!checkPhone(this.form.chargePhone)) {
          this.$message.error("请输入正确的负责人电话");
          return;
        }
      }
      if (this.form.socialCode) {
        if (!CheckSocialCreditCode(this.form.socialCode)) {
          this.$message.error('请输入正确的统一社会信用代码')
          return
        }
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          let data = {
            projectSn: this.projectSn,
            unitName: this.form.unitName,
            unitChargeName: this.form.unitChargeName,
            chargePhone: this.form.chargePhone,
            unitAddress: this.form.unitAddress,
            socialCode: this.form.socialCode,
            type: 1
          }
          addReceiptAndShipmentItem(data).then(res => {
            if (res.code == 200) {
              this.$message.success('新增成功！')
              this.getListData()
              this.dialogVisibleAdd = false
              console.log('11111', this.form)
              Object.keys(this.form).forEach(item => {
                this.form[item] = ''
              })
              console.log('222222', this.form)
            } else {
              this.$message.success('新增失败！')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    okEdit() {
      if (this.editForm.chargePhone) {
        if (!checkPhone(this.editForm.chargePhone)) {
          this.$message.error("请输入正确的负责人电话");
          return;
        }
      }
      if (this.editForm.socialCode) {
        if (!CheckSocialCreditCode(this.editForm.socialCode)) {
          this.$message.error('请输入正确的统一社会信用代码')
          return
        }
      }
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let data = {
            unitName: this.editForm.unitName,
            unitChargeName: this.editForm.unitChargeName,
            chargePhone: this.editForm.chargePhone,
            unitAddress: this.editForm.unitAddress,
            socialCode: this.editForm.socialCode,
            projectSn: this.projectSn,
            type: 1,
            id: this.editId
          }
          editReceiptAndShipmentItem(data).then(res => {
            if (res.code == 200) {
              this.$message.success('编辑成功！')
              this.dialogVisibleEdit = false
              this.getListData()
            } else {
              this.$message.success('编辑失败！')
            }
          })
          this.getListData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      done()
    },
    //获取列表数据
    getListData() {
      let data = {
        unitName: this.formInline.forwardName,
        projectSn: this.projectSn,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        type: 1
      }
      getReceiptAndShipmentList(data).then(res => {
        console.log('发货', 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 = {
        forwardName: ''
      }
      this.getListData()
    },
    //编辑按钮
    editBtn(val) {
      console.log(val)
      this.editForm = { ...val }
      this.editId = val.id
      this.dialogVisibleEdit = true
    },
    //删除按钮
    deleteBtn(val) {
      console.log(val, '~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let data = {
            id: val.id
          }
          deleteReceiptAndShipmentItem(data).then(res => {
            if (res.code == 200) {
              this.$message.success('删除成功！')
              this.getListData()
            } else {
              this.$message.success('删除失败！')
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //查看条数
    handleSizeChange(val) {
      this.pageNo = 1
      this.pageSize = val
      this.getListData()
    },
    //查看页
    handleCurrentChange(val) {
      this.pageNo = val
      this.pageSize = 10
      this.getListData()
    }
  }
}
</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>
