<template>
  <div class="app-container">
    <el-card>
      <span>发票号:</span>
      <el-input v-model="invoiceNum" placeholder="发票号" size="small" clearable/>
      <!--<span>开票时间:</span>-->
      <!--<el-date-picker v-model="invoiceTime" size="small" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>-->
      <span>采购合同号:</span>
      <el-select v-model="contractNum" placeholder="合同号" size="small" filterable clearable>
        <el-option
          v-for="item in contractNums"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
        </el-option>
      </el-select>
      <span>传送人:</span>
      <el-select v-model="transmitValue" placeholder="传送人" size="small" filterable clearable style="width:100px">
        <el-option
          v-for="item in selectPerson"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>接收人:</span>
      <el-select v-model="recipientValue" placeholder="接收人" size="small" filterable clearable style="width:100px">
        <el-option
          v-for="item in selectPerson"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="total1=0;pageCurrent1=1;pageSize1=10;searchTable1()">查询</el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        style="margin-left: 10px"
        @click="addTable1()">增加</el-button>
      <el-button
        type="warning"
        size="small"
        icon="el-icon-download"
        style="margin-left: 10px">导出</el-button>
    </el-card>

    <el-card>
      <el-table v-loading="" :data="tableData1" size="mini" style="max-height: 400px" height="400px" stripe border>
        <el-table-column align="center" label="发票号" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.发票号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="开票时间" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.开票时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="开票金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.开票金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购合同编号" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购合同名称" min-width="150px">
          <template slot-scope="scope">
            {{ scope.row.采购合同名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="供应商" min-width="200px">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="总金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.总金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣除金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.扣除金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="传送人" min-width="80px">
          <template slot-scope="scope">
            {{ scope.row.传送人姓名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="接收人" min-width="80px">
          <template slot-scope="scope">
            {{ scope.row.接收人姓名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" min-width="200px">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editTable1(scope.row)">编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteTable1(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>

    <el-dialog :title="title1" :visible.sync="dialogVisible1" center style="min-height: 300px" width="25%">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px">
        <el-form-item label="采购合同号" prop="contractNum">
          <el-select v-model="form1.contractNum" placeholder="合同号" size="small" filterable clearable>
            <el-option
              v-for="item in contractNums"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              <div style="float: left">{{ item.label }}</div>
              <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票号" prop="invoiceNum">
          <el-input v-model="form1.invoiceNum" size="small"/>
        </el-form-item>
        <el-form-item label="开票时间" prop="invoiceTime">
          <el-date-picker v-model="form1.invoiceTime" size="small" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="开票金额" prop="money">
          <el-input v-model="form1.money" size="small" placeholder="开票金额"/>
        </el-form-item>
        <el-form-item label="扣除金额" prop="money">
          <el-input v-model="form1.money1" size="small" placeholder="扣除金额"/>
        </el-form-item>
        <el-form-item label="传送人" prop="sendPerson">
          <el-select v-model="form1.sendPersonValue" placeholder="传送人" size="small" disabled filterable clearable style="width:100px">
            <el-option
              v-for="item in selectPerson"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="接收人" prop="receivePerson">
          <el-select v-model="form1.receivePersonValue" placeholder="接收人" size="small" filterable clearable style="width:100px">
            <el-option
              v-for="item in selectPerson"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form1.remark" size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible1 = false">取消</el-button>
        <el-button v-show="title1==='增加'" size="small" type="primary" @click="submitAdd1('form1')">确定</el-button>
        <el-button v-show="title1==='编辑'" size="small" type="primary" @click="submitEdit1('form1')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchInvoice, addInvoice, editInvoice, deleteInvoice, initPerson, initContract } from '@/api/WorkshopProcurement/ReceiptConnectionSheet'
import { mapGetters } from 'vuex'

export default {
  name: '', // 发票交接单
  data() {
    return {
      contractNumValue: '',
      contractNums: [],
      invoiceNum: '',
      invoiceTime: '',
      transmitValue: '',
      selectPerson: [], // 传送人接收人
      recipientValue: '',
      contractNum: '',
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      tableData1: [],
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      check5: 0,
      dialogVisible1: false,
      title1: '',
      form1: {
        contractNum: '',
        invoiceNum: '',
        invoiceNumValue: '',
        invoiceTime: '',
        money: '',
        money1: '',
        sendPersonValue: '',
        receivePersonValue: '',
        remark: ''
      },
      rules1: {
        contractNum: [
          { required: true, message: '请选择合同号', trigger: 'change' }
        ],
        invoiceNum: [
          { required: true, message: '请输入发票号', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNums.push({
            label: response.data[i].采购合同编号,
            name: response.data[i].采购合同名称,
            value: response.data[i].采购合同流水号
          })
        }
      })
      initPerson().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.selectPerson.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    searchTable1() {
      this.invoiceNum ? this.check1 = 1 : this.check1 = 0
      this.invoiceTime ? this.check2 = 1 : this.check2 = 0
      this.transmitValue ? this.check3 = 1 : this.check3 = 0
      this.recipientValue ? this.check4 = 1 : this.check4 = 0
      this.contractNum ? this.check5 = 1 : this.check5 = 0
      searchInvoice(this.pageCurrent1, this.pageSize1, this.check1, this.invoiceNum, this.check2, this.invoiceTime, this.check3, this.transmitValue, this.check4, this.recipientValue, this.check5, this.contractNum).then(response => {
        for (let j = 0; j < response.data.rows.length; j++) {
          if (response.data.rows[j].开票时间 !== null) {
            response.data.rows[j].开票时间 = response.data.rows[j].开票时间.substring(0, response.data.rows[j].开票时间.indexOf(' '))
          }
          if (response.data.rows[j].开票时间 === '1900/1/1') {
            response.data.rows[j].开票时间 = ''
          }
        }
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    addTable1() {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.title1 = '增加'
      this.dialogVisible1 = true
      this.form1.contractNum = ''
      this.form1.invoiceNum = ''
      this.form1.invoiceTime = ''
      this.form1.money = ''
      this.form1.money1 = ''
      this.form1.sendPersonValue = parseInt(this.id)
      this.form1.receivePersonValue = ''
      this.form1.remark = ''
    },
    submitAdd1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const myDate = new Date()
          const year = myDate.getFullYear()
          const month = myDate.getMonth() + 1
          const date = myDate.getDate()
          const nowDate = year + '-' + month + '-' + date
          addInvoice(this.form1.contractNum, this.form1.invoiceNum, this.form1.invoiceTime, this.form1.money, this.form1.sendPersonValue, this.form1.receivePersonValue, this.form1.remark, nowDate, this.form1.money1).then(response => {
            console.log(response.data)
            if (response.data.result[0].result === '1') {
              this.$message.success('增加成功')
              this.dialogVisible1 = false
              this.searchTable1()
            } else { this.$message.error('增加失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    editTable1(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.title1 = '编辑'
      this.dialogVisible1 = true
      this.form1.contractNum = parseInt(row.采购合同流水号)
      this.form1.invoiceNum = row.发票号
      this.form1.invoiceNumValue = row.发票流水号
      this.form1.invoiceTime = row.开票时间
      this.form1.money = row.开票金额
      this.form1.money1 = row.扣除金额
      this.form1.sendPersonValue = parseInt(row.传送人)
      this.form1.receivePersonValue = parseInt(row.接收人)
      this.form1.remark = row.备注
      console.log(row.开票时间)
      if (row.开票时间 === '1900/1/1') {
        this.form1.invoiceTime = null
      }
      if (parseInt(row.传送人) === 0) {
        this.form1.sendPersonValue = ''
      }
      if (parseInt(row.接收人) === 0) {
        this.form1.receivePersonValue = ''
      }
    },
    submitEdit1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editInvoice(this.form1.invoiceNumValue, this.form1.contractNum, this.form1.invoiceNum, this.form1.invoiceTime, this.form1.money, this.form1.sendPersonValue, this.form1.receivePersonValue, this.form1.remark, this.form1.money1).then(response => {
            console.log(this.form1.invoiceNumValue, this.form1.contractNum, this.form1.invoiceNum, this.form1.invoiceTime, this.form1.money, this.form1.sendPersonValue, this.form1.receivePersonValue, this.form1.remark)
            console.log(response.data)
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.dialogVisible1 = false
              this.searchTable1()
            } else { this.$message.error('编辑失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    deleteTable1(row) {
      console.log(row)
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteInvoice(row.发票流水号, row.采购合同流水号).then(response => {
          console.log(response.data)
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.dialogVisible1 = false
            this.searchTable1()
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:150px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .searchForm .el-form-item{
    margin-top: 1px;
    margin-bottom: 1px;
  }
</style>
