<template>
  <div class="app-container">
    <el-card>
      <span>发票号:</span>
      <el-input v-model="invoiceNum" placeholder="发票号" size="small" style="margin: 3px 0;width: 130px" clearable/>
      <span style="margin-left: 10px">供应商:</span>
      <el-input v-model="supplierName0" placeholder="供应商" size="small" style="width:130px" clearable/>
      <span style="margin-left: 10px">传送人:</span>
      <el-select v-model="transmitValue" placeholder="传送人" size="small" filterable clearable style="width:130px">
        <el-option
          v-for="item in selectPerson1"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span style="margin-left: 10px">接收人:</span>
      <el-select v-model="recipientValue" placeholder="接收人" size="small" filterable clearable style="width:130px">
        <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-card>
    <el-card>
      <el-table v-loading="" :data="tableData1" size="mini" style="margin-top: 3px" height="550px" stripe border highlight-current-row @expand-change="searchTable02">
        <el-table-column type="expand" label="详情">
          <template slot-scope="scope">
            <el-table :data="gridData" class="subTableHeader" stripe border size="mini" show-summary style="width: 40%">
              <el-table-column width="50" align="center" type="index" label="序号"/>
              <el-table-column min-width="150" align="center" label="采购单号">
                <template slot-scope="scope">
                  {{ scope.row.采购单号 }}
                </template>
              </el-table-column>
              <el-table-column min-width="100" align="center" label="采购单名称">
                <template slot-scope="scope">
                  {{ scope.row.采购单名称 }}
                </template>
              </el-table-column>
              <el-table-column min-width="100" align="center" label="总价" prop="总价">
                <template slot-scope="scope">
                  {{ scope.row.总价 }}
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <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="200px" show-overflow-tooltip>
          <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="备注" >
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" width="150px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top">
              <div style="display: inline-block">
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-edit-outline"
                  style="margin-left: 10px"
                  @click="editTable1(scope.row)"/>
              </div>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top">
              <div style="display: inline-block">
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-delete"
                  style="margin-left: 20px"
                  @click="deleteTable1(scope.row)"/>
              </div>
            </el-tooltip>
          </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 v-el-drag-dialog :title="title1" :visible.sync="dialogVisible1" center style="min-height: 300px" width="30%">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px">
        <el-form-item label="供应商名称" prop="供应商名称">
          <el-input v-model="form1.供应商名称" size="small" readonly>
            <el-button slot="append" icon="el-icon-search" @click="dialogVisible=true"/>
          </el-input>
        </el-form-item>
        <el-form-item label="采购单号" prop="contractNum">
          <el-select :disabled="title1==='编辑备料发票'" v-model="form1.contractNum" placeholder="采购单号" size="small" multiple filterable clearable>
            <el-option
              v-for="item in contractNums"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </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="sendPerson">
          <el-select v-model="form1.sendPersonValue" placeholder="传送人" size="small" disabled filterable clearable style="width:100px">
            <el-option
              v-for="item in selectPerson1"
              :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>
    <el-dialog v-el-drag-dialog :visible.sync="dialogVisible" title="选择供应商" center style="min-height: 300px;width: 100%">
      <el-form ref="form1" :model="form1" label-position="left" label-width="90px" class="searchForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称">
              <el-input v-model="supplierName" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业性质">
              <el-input v-model="enterpriseNature" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="货期">
              <el-input v-model="goodTime" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册资本">
              <el-input v-model="registeredCapital" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="税号">
              <el-input v-model="taxNum" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱">
              <el-input v-model="EMail" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址">
              <el-input v-model="address" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号">
              <el-input v-model="account" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开户行">
              <el-input v-model="openingBank" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码">
              <el-input v-model="phone" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="传真">
              <el-input v-model="fax" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-button
              type="success"
              size="small"
              icon="el-icon-search"
              @click="searchSupplier">查询</el-button>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-check"
              @click="submitSelectSupplier">确定</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="emptySupplierDetail">清空</el-button>
          </el-col>
        </el-row>
        <el-table :data="tableData01" size="mini" style="width: 97%;max-height: 300px;margin-top:15px" height="300px" border stripe highlight-current-row @row-click="showSupplierDetail">
          <el-table-column align="center" label="供应商名称" min-width="250">
            <template slot-scope="scope">
              {{ scope.row.供应商名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="企业性质" min-width="100">
            <template slot-scope="scope">
              {{ scope.row.企业性质 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="创立日期" min-width="100">
            <template slot-scope="scope">
              {{ scope.row.创立日期 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="注册资本" min-width="100">
            <template slot-scope="scope">
              {{ scope.row.注册资本 }}
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>import { searchInvoice, addInvoice, editInvoice, deleteInvoice, initPerson, initPerson1, searchSupplier, initPurchase, searchInvoiceDetail } from '@/api/WorkshopProcurement/OutsourcingMaterialsInvoice'
import { mapGetters } from 'vuex'
export default {
  name: '', // 发票交接单
  data() {
    return {
      contractNumValue: '',
      contractNums: [],
      invoiceNum: '',
      invoiceTime: '',
      transmitValue: this.id,
      selectPerson: [], // 接收人
      selectPerson1: [], // 传送人
      recipientValue: '',
      contractNum: '',
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      tableData1: [],
      tableData01: [],
      gridData: [],
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      check5: 0,
      supplierCheck1: 0,
      supplierCheck2: 0,
      supplierCheck3: 0,
      supplierCheck4: 0,
      supplierCheck5: 0,
      supplierCheck6: 0,
      supplierCheck7: 0,
      supplierCheck8: 0,
      supplierCheck9: 0,
      supplierCheck10: 0,
      supplierCheck11: 0,
      supplierCheck12: 0,
      supplierName0: '', // 供应商名称（外面的）
      supplierName: '', // 供应商名称
      supplierValue: '', // 供应商流水号
      goodTime: '', // 货期
      enterpriseNature: '', // 企业性质
      createDate: '', // 创立日期
      registeredCapital: '', // 注册资本
      taxNum: '', // 税号
      EMail: '', // 电子邮箱
      address: '', // 地址
      account: '', // 账号
      openingBank: '', // 开户行
      phone: '', // 电话号码
      fax: '', // 传真
      dialogVisible: false,
      dialogVisible1: false,
      title1: '',
      form1: {
        供应商名称: '',
        供应商流水号: '',
        contractNum: [],
        invoiceNumValue: '',
        invoiceTime: '',
        money: '',
        sendPersonValue: '',
        receivePersonValue: '',
        remark: ''
      },
      rules1: {
        contractNum: [
          { required: true, message: '请选择采购单号', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initPerson().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.selectPerson.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
      initPerson1().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.selectPerson1.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
      initPurchase().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNums.push({
            label: response.data[i].采购单号,
            value: response.data[i].采购计划流水号
          })
        }
      })
    },
    searchTable1() {
      this.invoiceNum ? this.check1 = 1 : this.check1 = 0
      this.supplierName0 ? this.check2 = 1 : this.check2 = 0
      this.transmitValue ? this.check3 = 1 : this.check3 = 0
      this.recipientValue ? this.check4 = 1 : this.check4 = 0
      searchInvoice(this.pageCurrent1, this.pageSize1, this.check1, this.invoiceNum, this.check2, this.supplierName0, this.check3, this.transmitValue, this.check4, this.recipientValue).then(response => {
        console.log(response.data)
        for (let j = 0; j < response.data.rows.length; j++) {
          response.data.rows[j].开票金额 = parseInt(response.data.rows[j].开票金额 * 100) / 100
          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()
    },
    searchTable02(row) { // 查看详情（发票包含的合同）
      searchInvoiceDetail(row.发票流水号).then(response => {
        this.gridData = response.data
      })
    },
    searchSupplier() { // 查询创立日期和注册资本大于符合条件的数据
      this.supplierName ? this.supplierCheck1 = 1 : (this.supplierCheck1 = 0, this.supplierName = '')
      this.enterpriseNature ? this.supplierCheck2 = 1 : (this.supplierCheck2 = 0, this.enterpriseNature = '')
      this.createDate ? this.supplierCheck3 = 1 : (this.supplierCheck3 = 0, this.createDate = '')
      this.registeredCapital ? this.supplierCheck4 = 1 : (this.supplierCheck4 = 0, this.registeredCapital = '')
      this.taxNum ? this.supplierCheck5 = 1 : (this.supplierCheck5 = 0, this.taxNum = '')
      this.EMail ? this.supplierCheck6 = 1 : (this.supplierCheck6 = 0, this.EMail = '')
      this.address ? this.supplierCheck7 = 1 : (this.supplierCheck7 = 0, this.address = '')
      this.account ? this.supplierCheck8 = 1 : (this.supplierCheck8 = 0, this.account = '')
      this.openingBank ? this.supplierCheck9 = 1 : (this.supplierCheck9 = 0, this.openingBank = '')
      this.phone ? this.supplierCheck10 = 1 : (this.supplierCheck10 = 0, this.phone = '')
      this.fax ? this.supplierCheck11 = 1 : (this.supplierCheck11 = 0, this.fax = '')
      this.goodTime ? this.supplierCheck12 = 1 : (this.supplierCheck12 = 0, this.goodTime = '')
      searchSupplier(this.supplierCheck1, this.supplierName, this.supplierCheck2, this.enterpriseNature, this.supplierCheck3, this.createDate, this.supplierCheck4, this.registeredCapital, this.supplierCheck5, this.taxNum, this.supplierCheck6, this.EMail, this.supplierCheck7, this.address, this.supplierCheck8, this.account, this.supplierCheck9, this.openingBank, this.supplierCheck10, this.phone, this.supplierCheck11, this.fax, this.supplierCheck12, this.goodTime).then(response => {
        this.tableData01 = response.data
      })
    },
    showSupplierDetail(row) {
      this.form1.供应商流水号 = row.供应商流水号
      this.form1.供应商名称 = row.供应商名称
      this.supplierValue = row.供应商流水号
      this.supplierName = row.供应商名称
      this.enterpriseNature = row.企业性质
      this.createDate = row.创立日期
      this.registeredCapital = row.注册资本
      this.taxNum = row.税号
      this.EMail = row.电子信箱
      this.address = row.地址
      this.account = row.帐号
      this.openingBank = row.开户行
      this.phone = row.电话号码
      this.fax = row.传真
      this.goodTime = row.货期
    },
    emptySupplierDetail() {
      this.supplierValue = ''
      this.supplierName = ''
      this.enterpriseNature = ''
      this.createDate = ''
      this.registeredCapital = ''
      this.taxNum = ''
      this.EMail = ''
      this.address = ''
      this.account = ''
      this.openingBank = ''
      this.phone = ''
      this.fax = ''
      this.goodTime = ''
    },
    submitSelectSupplier() { // 确定选择供应商
      this.dialogVisible = false
    },
    addTable1() {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.title1 = '新增备料发票'
      this.dialogVisible1 = true
      this.form1.供应商名称 = ''
      this.form1.供应商流水号 = ''
      this.form1.contractNum = []
      this.contractNumValue = ''
      this.form1.invoiceTime = ''
      this.form1.money = ''
      this.form1.sendPersonValue = parseInt(this.id)
      this.form1.receivePersonValue = ''
      this.form1.remark = ''
    },
    submitAdd1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.contractNumValue = this.form1.contractNum[0]
          for (let h = 1; h < this.form1.contractNum.length; h++) {
            this.contractNumValue += ',' + this.form1.contractNum[h]
          }
          addInvoice(this.form1.contractNum, this.form1.invoiceTime, this.form1.money, this.form1.sendPersonValue, this.form1.receivePersonValue, this.form1.remark, this.form1.供应商流水号, this.contractNumValue).then(response => {
            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
        }
      })
    },
    editTable1(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.title1 = '编辑备料发票'
      console.log(row)
      this.dialogVisible1 = true
      this.form1.contractNum = (row.采购计划流水号.split(',')).map(Number)
      this.form1.invoiceNumValue = row.发票流水号
      this.form1.invoiceTime = row.开票时间
      this.form1.money = row.开票金额
      this.form1.sendPersonValue = parseInt(row.传送人)
      this.form1.receivePersonValue = parseInt(row.接收人)
      this.form1.remark = row.备注
      this.form1.供应商流水号 = row.供应商
      this.form1.供应商名称 = 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) {
          console.log(this.form1.remark)
          editInvoice(this.form1.invoiceNumValue, this.form1.invoiceTime, this.form1.money, this.form1.sendPersonValue, this.form1.receivePersonValue, this.form1.remark, this.form1.供应商流水号).then(response => {
            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.发票流水号).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: 1px 0px;
  }
  .searchForm .el-form-item{
    margin-top: 1px;
    margin-bottom: 1px;
  }
</style>
<style lang="scss">
  .subTableHeader {
    th {
      background: #8ff4ea !important;
      color: black;
    }

    tr.el-table__row td {
      background-color: #d8e5f6 !important;
    }

    tr.el-table__row.el-table__row--striped td {
      background: #e9f0f9 !important;
    }
  }
</style>

