<template>
  <div class="app-container">
    <el-card>
      <el-form ref="form1" :model="form1" label-position="right" label-width="110px" class="searchForm">
        <el-row>
          <el-col style="width: 300px">
            <el-form-item label="供应商名称">
              <el-input v-model="supplierName" placeholder="供应商名称" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="企业性质">
              <el-input v-model="businessNature" placeholder="企业性质" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="创立日期">
              <el-date-picker v-model="creationDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd" size="small" type="date" placeholder="选择日期"/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="注册资本">
              <el-input v-model="registeredCapital" placeholder="注册资本" size="small" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="width: 300px">
            <el-form-item label="税号">
              <el-input v-model="taxNumber" placeholder="税号" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="电子信箱">
              <el-input v-model="email" placeholder="电子信箱" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="电话号码">
              <el-input v-model="telephone" placeholder="电话号码" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="开户行">
              <el-input v-model="bank" placeholder="开户行" size="small" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="width: 300px">
            <el-form-item label="帐号">
              <el-input v-model="account" placeholder="帐号" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="地址">
              <el-input v-model="address" placeholder="地址" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px">
            <el-form-item label="传真">
              <el-input v-model="fax" placeholder="传真" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 350px">
            <el-form-item label="货期">
              <el-input v-model="deliveryPeriod" placeholder="货期" size="small" clearable/>
            </el-form-item>
          </el-col>
          <el-col style="width: 300px;margin-top:2px">
            <el-button type="success" size="small" icon="el-icon-search" @click="pageCurrent = 1;fetchData()">查询</el-button>
            <el-button type="primary" size="small" icon="el-icon-circle-plus-outline" style="margin-left:10px" @click="handleAdd()">增加</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card>
      <div style="display: inline-block;margin-bottom: 20px">供应商列表</div>
      <el-table :data="List" highlight-current-row border style="width: 100%;max-height: 600px;" height="600" size="mini" @row-click="rowClick">
        <el-table-column label="供应商名称" align="center" min-width="350">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="主要产品" align="center" min-width="350">
          <template slot-scope="scope">
            {{ scope.row.主要产品 }}
          </template>
        </el-table-column>
        <el-table-column label="开户行" align="center" width="250">
          <template slot-scope="scope">
            {{ scope.row.开户行 }}
          </template>
        </el-table-column>
        <el-table-column label="帐号" align="center" width="170">
          <template slot-scope="scope">
            {{ scope.row.帐号 }}
          </template>
        </el-table-column>
        <el-table-column label="税号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.税号 }}
          </template>
        </el-table-column>
        <el-table-column label="电话号码" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.电话号码 }}
          </template>
        </el-table-column>
        <el-table-column label="电子信箱" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.电子信箱 }}
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.地址 }}
          </template>
        </el-table-column>
        <el-table-column label="传真" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.传真 }}
          </template>
        </el-table-column>
        <el-table-column label="企业性质" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.企业性质 }}
          </template>
        </el-table-column>
        <el-table-column label="创立日期" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.创立日期 }}
          </template>
        </el-table-column>
        <el-table-column label="注册资本" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.注册资本 }}
          </template>
        </el-table-column>
        <el-table-column label="货期" align="center">
          <template slot-scope="scope">
            {{ scope.row.货期 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" style="margin-left:10px" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-card>
      <div style="display: inline-block;margin-bottom: 20px;width:90%">供应商联系人列表</div>
      <el-button type="primary" size="small" icon="el-icon-circle-plus-outline" style="margin-bottom:10px;float:right" @click="handleAdd2()">增加</el-button>
      <el-table :data="List1" border size="mini">
        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="职务" align="center">
          <template slot-scope="scope">
            {{ scope.row.职务 }}
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center" >
          <template slot-scope="scope">
            {{ scope.row.电话 }}
          </template>
        </el-table-column>
        <el-table-column label="手机" align="center" >
          <template slot-scope="scope">
            {{ scope.row.手机 }}
          </template>
        </el-table-column>
        <el-table-column label="传真" align="center" >
          <template slot-scope="scope">
            {{ scope.row.传真 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" style="margin-left:10px" @click="handleEdit2(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete2(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="供应商名称">
              <el-input
                v-model="form.供应商名称"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业性质" prop="企业性质">
              <el-input
                v-model="form.企业性质"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="创立日期" prop="创立日期">
              <el-date-picker
                v-model="form.创立日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="选择日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册资本" prop="注册资本">
              <el-input
                v-model="form.注册资本"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="税号" prop="税号">
              <el-input
                v-model="form.税号"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户行" prop="开户行">
              <el-input
                v-model="form.开户行"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="帐号" prop="帐号">
              <el-input
                v-model="form.帐号"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子信箱" prop="电子信箱">
              <el-input
                v-model="form.电子信箱"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="地址">
              <el-input
                v-model="form.地址"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" prop="电话号码">
              <el-input
                v-model="form.电话号码"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="传真" prop="传真">
              <el-input
                v-model="form.传真"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货期" prop="货期">
              <el-input
                v-model="form.货期"
                size="small"
                style="width: 200px;"
                clearable/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-show="title==='增加'" type="primary" @click="submitAdd('form')">确定</el-button>
        <el-button v-show="title==='编辑'" type="primary" @click="submitEdit('form')">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="dialogFormVisible2" center style="min-height: 300px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="right" label-width="80px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="姓名">
          <el-input v-model="form2.姓名" size="small" clearable/>
        </el-form-item>
        <el-form-item label="职务" prop="职务">
          <el-input v-model="form2.职务" size="small" clearable/>
        </el-form-item>
        <el-form-item label="电话" prop="电话">
          <el-input v-model="form2.电话" size="small" clearable/>
        </el-form-item>
        <el-form-item label="手机" prop="手机">
          <el-input v-model="form2.手机" size="small" clearable/>
        </el-form-item>
        <el-form-item label="传真" prop="传真">
          <el-input v-model="form2.传真" size="small" clearable/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-show="title2==='增加'" type="primary" @click="submitAdd2('form2')">确定</el-button>
        <el-button v-show="title2==='编辑'" type="primary" @click="submitEdit2('form2')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchData, addTable, editTable, deleteData, getName, addPerson, editPerson, deletePerson } from '@/api/PurchasingCenter/SupplierManagement'

export default {
  data() {
    return {
      form1: {},
      supplierName: '',
      businessNature: '',
      creationDate: '',
      registeredCapital: '',
      taxNumber: '',
      email: '',
      address: '',
      account: '',
      bank: '',
      telephone: '',
      fax: '',
      deliveryPeriod: '',
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      check5: 0,
      check6: 0,
      check7: 0,
      check8: 0,
      check9: 0,
      check10: 0,
      check11: 0,
      check12: 0,
      List: [],
      pageCurrent: 1,
      pageSize: 10,
      total: 0,
      title: '',
      dialogFormVisible: false,
      form: {
        供应商名称: '',
        企业性质: '',
        创立日期: '',
        注册资本: '',
        税号: '',
        开户行: '',
        帐号: '',
        电子信箱: '',
        地址: '',
        电话号码: '',
        传真: '',
        货期: ''
      },
      rules: {
        供应商名称: [{ required: true, message: '请填写供应商名称', change: 'blur' }]
      },
      supplierSerialNumber: '',
      List1: [],
      dialogFormVisible2: false,
      title2: '',
      form2: {
        姓名: '',
        职务: '',
        电话: '',
        手机: '',
        传真: '',
        供应商流水号: '',
        供应商联系人流水号: ''
      },
      rules2: {
        姓名: [{ required: true, message: '请填写联系人姓名', change: 'blur' }]
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.List1 = []
      this.supplierName ? this.check1 = 1 : this.check1 = 0
      this.businessNature ? this.check2 = 1 : this.check2 = 0
      this.creationDate ? this.check3 = 1 : this.check3 = 0
      this.registeredCapital ? this.check4 = 1 : this.check4 = 0
      this.taxNumber ? this.check5 = 1 : this.check5 = 0
      this.bank ? this.check6 = 1 : this.check6 = 0
      this.account ? this.check7 = 1 : this.check7 = 0
      this.email ? this.check8 = 1 : this.check8 = 0
      this.address ? this.check9 = 1 : this.check9 = 0
      this.telephone ? this.check10 = 1 : this.check10 = 0
      this.fax ? this.check11 = 1 : this.check11 = 0
      this.deliveryPeriod ? this.check12 = 1 : this.check12 = 0
      searchData(this.check1, this.supplierName, this.check2, this.businessNature, this.check3, this.creationDate, this.check4, this.registeredCapital, this.check5, this.taxNumber, this.check6, this.bank, this.check7, this.account, this.check8, this.email, this.check9, this.address, this.check10, this.telephone, this.check11, this.fax, this.check12, this.deliveryPeriod, this.pageCurrent, this.pageSize).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(' '))
          }
        }
        this.List = response.data.rows
        this.total = response.data.total
      })
    },
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible = true
      this.title = '增加'
      this.form.供应商名称 = ''
      this.form.企业性质 = ''
      this.form.创立日期 = ''
      this.form.注册资本 = ''
      this.form.税号 = ''
      this.form.开户行 = ''
      this.form.帐号 = ''
      this.form.电子信箱 = ''
      this.form.地址 = ''
      this.form.电话号码 = ''
      this.form.传真 = ''
      this.form.货期 = ''
      this.supplierSerialNumber = ''
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addTable(this.form.供应商名称, this.form.企业性质, this.form.创立日期, this.form.注册资本, this.form.税号, this.form.开户行, this.form.帐号, this.form.电子信箱, this.form.地址, this.form.电话号码, this.form.传真, this.form.货期).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.dialogFormVisible = false
              this.pageCurrent = 1
              this.pageSize = 10
              this.fetchData()
            } else { this.$message.error('增加失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleEdit(row) {
      console.log(row)
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible = true
      this.title = '编辑'
      this.form.供应商名称 = row.供应商名称
      this.form.企业性质 = row.企业性质
      this.form.创立日期 = row.创立日期
      this.form.注册资本 = row.注册资本
      this.form.税号 = row.税号
      this.form.开户行 = row.开户行
      this.form.帐号 = row.帐号
      this.form.电子信箱 = row.电子信箱
      this.form.地址 = row.地址
      this.form.电话号码 = row.电话号码
      this.form.传真 = row.传真
      this.form.货期 = row.货期
      this.supplierSerialNumber = row.供应商流水号
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editTable(this.form.供应商名称, this.form.企业性质, this.form.创立日期, this.form.注册资本, this.form.税号, this.form.开户行, this.form.帐号, this.form.电子信箱, this.form.地址, this.supplierSerialNumber, this.form.传真, this.form.货期, this.form.电话号码).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.dialogFormVisible = false
              this.pageCurrent = 1
              this.pageSize = 10
              this.fetchData()
            } else { this.$message.error('编辑失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel() { // 取消
      this.dialogFormVisible = false
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteData(row.供应商流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.pageCurrent = 1
            this.pageSize = 10
            this.fetchData()
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.fetchData()
    },
    rowClick(row) {
      this.List1 = []
      this.form2.供应商流水号 = row.供应商流水号
      getName(this.form2.供应商流水号).then(response => {
        this.List1 = response.data
      })
    },
    handleAdd2() {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.dialogFormVisible2 = true
      this.title2 = '增加'
      this.form2.姓名 = ''
      this.form2.职务 = ''
      this.form2.电话 = ''
      this.form2.手机 = ''
      this.form2.传真 = ''
    },
    submitAdd2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addPerson(this.form2.姓名, this.form2.职务, this.form2.电话, this.form2.手机, this.form2.传真, this.form2.供应商流水号).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.dialogFormVisible2 = false
              getName(this.form2.供应商流水号).then(response => {
                this.List1 = response.data
              })
            } else { this.$message.error('增加失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleEdit2(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.dialogFormVisible2 = true
      this.title2 = '编辑'
      this.form2.姓名 = row.姓名
      this.form2.职务 = row.职务
      this.form2.电话 = row.电话
      this.form2.手机 = row.手机
      this.form2.传真 = row.传真
      this.form2.供应商联系人流水号 = row.供应商联系人流水号
    },
    submitEdit2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editPerson(this.form2.姓名, this.form2.职务, this.form2.电话, this.form2.手机, this.form2.传真, this.form2.供应商联系人流水号).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.dialogFormVisible2 = false
              getName(this.form2.供应商流水号).then(response => {
                this.List1 = response.data
              })
            } else { this.$message.error('编辑失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleDelete2(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePerson(row.供应商联系人流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            getName(this.form2.供应商流水号).then(response => {
              this.List1 = response.data
            })
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

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