<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 7px; margin-bottom: 7px">
        <el-input v-model="supplierName" style="width: 180px" size="small" clearable placeholder="供应商名称"/>
        <el-button icon="el-icon-search" type="primary" plain size="small" style="margin-left: 10px" @click="pageCurrent = 1;pageSize = 100;searchTable()">查询</el-button>
        <el-button type="primary" icon="el-icon-plus" size="small" @click="addCustomer();flag1 = 1">供应商</el-button>
        <el-checkbox v-model="checked" style="margin-left: 10px">合作密度</el-checkbox>
      </div>
      <div style="margin-top: 10px;width: 851px">
        <el-table v-loading="loading" :data="tableData" highlight-current-row border size="small" style="width: 100%" height="750px">
          <el-table-column :width="setColumnWidth('序号')" label="序号" type="index" align="center"/>
          <el-table-column label="供应商名称" align="center" prop="供应商名称">
            <template slot-scope="scope">
              {{ scope.row.供应商名称 }}
            </template>
          </el-table-column>
          <el-table-column label="地址" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.地址 }}
            </template>
          </el-table-column>
          <el-table-column :width="setColumnWidth('联系人')" label="联系人" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.联系人 }}
            </template>
          </el-table-column>
          <el-table-column width="120px" label="电话号" align="center">
            <template slot-scope="scope">
              {{ scope.row.电话号 }}
            </template>
          </el-table-column>
          <el-table-column v-if="checked === true" width="80px" label="合作密度" align="center">
            <template slot-scope="scope">
              {{ scope.row.合作密度 }}
            </template>
          </el-table-column>
          <el-table-column width="100px" label="操作" align="center">
            <template slot-scope="scope">
              <el-tooltip :open-delay="700" effect="dark" content="编辑供应商信息" placement="top">
                <el-button type="text" icon="el-icon-edit" size="mini" style="margin-right: 10px" @click="handleEdit(scope.row)"/>
              </el-tooltip>
              <el-tooltip :open-delay="700" effect="dark" content="删除供应商信息" placement="top">
                <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChanges"
          @current-change="handleCurrentChanges"/>
      </div>
    </el-card>
    <el-dialog v-el-drag-dialog :title="title" :visible.sync="dialogFormVisible" center width="690px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="100px" class="demo-ruleForm" style="margin: auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称:" prop="供应商名称">
              <el-input v-model="form.供应商名称" clearable size="small" style="width:200px" placeholder="请输入供应商名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址:" prop="地址">
              <el-input v-model="form.地址" clearable size="small" style="width:200px" placeholder="请输入地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人:" prop="联系人">
              <el-input v-model="form.联系人" clearable size="small" style="width:200px" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号:" prop="电话号">
              <el-input v-model="form.电话号" clearable size="small" style="width:200px" placeholder="请输入电话号"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog v-el-drag-dialog :title="title1" :visible.sync="dialogFormVisible1" center width="690px">
      <el-form ref="form" :model="form1" :rules="rules1" label-position="left" label-width="100px" class="demo-ruleForm" style="margin: auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称:" prop="供应商名称">
              <el-input v-model="form1.供应商名称" clearable size="small" style="width:200px" placeholder="请输入供应商名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址:" prop="地址">
              <el-input v-model="form1.地址" clearable size="small" style="width:200px" placeholder="请输入地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人:" prop="联系人">
              <el-input v-model="form1.联系人" clearable size="small" style="width:200px" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号:" prop="电话号">
              <el-input v-model="form1.电话号" clearable size="small" style="width:200px" placeholder="请输入电话号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="checked === true" label="合作密度:" prop="合作密度">
              <el-input v-model="form1.合作密度" clearable size="small" style="width:200px" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form1')">取消</el-button>
        <el-button type="primary" size="small" @click="confirmEdit('form1')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      supplierName: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      title: '',
      title1: '',
      checked: false,
      form: {
        供应商名称: '',
        地址: '',
        联系人: '',
        电话号: '',
        设备: ''
      },
      form1: {
        供应商名称: '',
        地址: '',
        联系人: '',
        电话号: '',
        合作密度: '',
        设备: ''
      },
      rules: {
        供应商名称: [{ required: true, message: '请输入供应商名称' }],
        地址: [{ required: true, message: '请输入地址' }],
        联系人: [{ required: true, message: '请输入联系人' }],
        电话号: [{ required: true, message: '请输入电话号' }]
      },
      rules1: {
        供应商名称: [{ required: true, message: '请输入供应商名称' }],
        地址: [{ required: true, message: '请输入地址' }],
        联系人: [{ required: true, message: '请输入联系人' }],
        电话号: [{ required: true, message: '请输入电话号' }]
      },
      loading: false, // 数据加载
      total: 0, // 分页总数
      tableData: [], // 合同信息表
      pageCurrent: 1, // 分页当前页
      pageSize: 100, // 分页每页显示条数
      orderName: '', // 排序列名
      order: '' // 排序方式
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.searchTable()
    },
    handleEdit(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.title1 = '编辑供应商信息'
      this.form1.供应商名称 = row.供应商名称
      this.form1.地址 = row.地址
      this.form1.联系人 = row.联系人
      this.form1.电话号 = row.电话号
      this.form1.设备 = row.设备
      this.form1.合作密度 = row.合作密度
      this.form1.供应商流水号 = row.供应商流水号
      this.dialogFormVisible1 = true
    },
    searchTable() {
      let supplierName_check
      this.supplierName ? supplierName_check = 1 : supplierName_check = 0
      var param = []
      param[0] = ['供应商名称_check', supplierName_check]
      param[1] = ['供应商名称', this.supplierName]
      var Data = this.CreateData('11', '采购管理_供应商_查询数据', param, this.pageSize, this.pageCurrent)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    handleDelete(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['供应商流水号', row.供应商流水号]
        var Data = this.CreateData('12', '采购管理_供应商_删除数据', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.fetchData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    confirmEdit(row) {
      var param = []
      param[0] = ['供应商流水号', this.form1.供应商流水号]
      param[1] = ['供应商名称', this.form1.供应商名称]
      param[2] = ['地址', this.form1.地址]
      param[3] = ['联系人', this.form1.联系人]
      param[4] = ['电话号', this.form1.电话号]
      param[5] = ['合作密度', this.form1.合作密度]
      if (row.供应商名称 === '' || row.供应商名称 === null) {
        this.$message.error('请输入供应商名称')
      } else {
        var Data = this.CreateData('12', '采购管理_供应商_修改数据', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '信息编辑成功',
              type: 'success'
            })
            this.fetchData()
            this.dialogFormVisible1 = false
          } else {
            this.$message.error('信息编辑失败')
          }
        })
      }
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['供应商名称', this.form.供应商名称]
          param[1] = ['地址', this.form.地址]
          param[2] = ['联系人', this.form.联系人]
          param[3] = ['电话号', this.form.电话号]
          var Data = this.CreateData('11', '采购管理_供应商_增加数据', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].结果 === 1) {
              this.$message({
                message: '信息增加成功',
                type: 'success'
              })
              this.fetchData()
            } else {
              this.$message.error('信息增加失败')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    addCustomer() {
      this.form = {}
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '新增供应商'
      this.dialogFormVisible = true
    },
    callOff(formName) {
      this.dialogFormVisible = false
      this.dialogFormVisible1 = false
      this.$message.error('已取消')
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

