<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">物料编码：</span>
        <el-input
          v-model="materialCodeValue"
          placeholder="请输入物料编码"
          clearable
          size="small"
          style="width: 180px"
        />
        <span style="margin-left: 10px">物料名称：</span>
        <el-input
          v-model="materialNameValue"
          placeholder="请输入物料名称"
          clearable
          size="small"
          style="width: 180px"
        />
        <span style="margin-left: 10px">程序号：</span>
        <el-input v-model="programNoValue" placeholder="请输入程序号" clearable size="small" style="width: 140px" />
        <span style="margin-left: 10px">操作日期：</span>
        <el-date-picker
          v-model="importTime"
          :clearable="true"
          size="small"
          type="daterange"
          align="center"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-search"
          style="margin-left: 5px"
          @click="handleSearch"
          >查询</el-button
        >
        <el-button type="success" plain size="small" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        class="main"
        tooltip-effect="dark"
        style="width: 100%"
        height="700px"
        border
        size="mini"
      >
        <el-table-column align="center" type="index" width="60px" label="序号" />
        <el-table-column align="center" label="物料编码" prop="物料编码" min-width="140px" />
        <el-table-column align="center" label="物料名称" prop="物料名称" min-width="120px" />
        <el-table-column align="center" label="相机" prop="相机" min-width="110px" />
        <el-table-column align="center" label="机器人" prop="机器人" min-width="110px" />
        <el-table-column align="center" label="加工中心" prop="加工中心" min-width="110px" />
        <el-table-column align="center" label="清洗机" prop="清洗机" min-width="110px" />
        <el-table-column align="center" label="打标机" prop="打标机" min-width="110px" />
        <el-table-column align="center" label="打标模板" prop="打标模板" min-width="220px" show-overflow-tooltip />
        <el-table-column align="center" label="创建时间" prop="创建时间" min-width="150px" />
        <el-table-column align="center" label="修改时间" prop="修改时间" min-width="150px" />
        <el-table-column align="center" label="操作" width="150px" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color: #f56c6c"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[50, 100, 200]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="620px" :close-on-click-modal="false">
      <el-form ref="configForm" :model="configForm" :rules="configRules" label-width="100px">
        <el-form-item label="物料编码" prop="物料编码">
          <el-input v-model="configForm.物料编码" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="物料名称">
          <el-input v-model="configForm.物料名称" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="相机">
          <el-input v-model="configForm.相机" placeholder="请输入相机程序号" />
        </el-form-item>
        <el-form-item label="机器人">
          <el-input v-model="configForm.机器人" placeholder="请输入机器人程序号" />
        </el-form-item>
        <el-form-item label="加工中心">
          <el-input v-model="configForm.加工中心" placeholder="请输入加工中心程序号" />
        </el-form-item>
        <el-form-item label="清洗机">
          <el-input v-model="configForm.清洗机" placeholder="请输入清洗机程序号" />
        </el-form-item>
        <el-form-item label="打标机">
          <el-input v-model="configForm.打标机" placeholder="请输入打标机程序号" />
        </el-form-item>
        <el-form-item label="打标模板">
          <el-input v-model="configForm.打标模板" placeholder="请输入打标模板" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitConfig">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const emptyConfigForm = () => ({
  物料编码: '',
  物料名称: '',
  相机: '',
  机器人: '',
  加工中心: '',
  清洗机: '',
  打标机: '',
  打标模板: ''
})

export default {
  data() {
    return {
      materialCodeValue: '',
      materialNameValue: '',
      programNoValue: '',
      importTime: null,
      tableData: [],
      total: 0,
      pageSize: 50,
      pageCurrent: 1,
      loading: false,
      dialogVisible: false,
      dialogTitle: '新增程序配置',
      isEdit: false,
      editAID: null,
      configForm: emptyConfigForm(),
      configRules: {
        物料编码: [{ required: true, message: '请输入物料编码', trigger: 'blur' }],
        物料名称: [{ required: true, message: '请输入物料名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.searchData()
  },
  methods: {
    handleSearch() {
      this.pageCurrent = 1
      this.searchData()
    },
    searchData() {
      this.loading = true
      this.tableData = []
      const startTime = this.importTime && this.importTime[0] ? this.importTime[0] + ' 00:00:00' : ''
      const endTime = this.importTime && this.importTime[1] ? this.importTime[1] + ' 23:59:59' : ''
      const param = [
        ['物料编码', this.materialCodeValue],
        ['物料名称', this.materialNameValue],
        ['程序号', this.programNoValue],
        ['开始时间', startTime],
        ['结束时间', endTime],
        ['PageCurrent', this.pageCurrent],
        ['PageSize', this.pageSize],
        ['PageCount', '0', 'int', '1'],
        ['ItemCount', '0', 'int', '1']
      ]
      const Data = this.CreateData('11', '零件图号_程序配置_分页查询', param)
      this.ExecDatabase(Data)
        .then(res => {
          this.tableData = res.data.result || []
          this.total = parseInt(res.data.output[0].ItemCount) || 0
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleAdd() {
      this.dialogTitle = '新增程序配置'
      this.isEdit = false
      this.editAID = null
      this.configForm = emptyConfigForm()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.configForm && this.$refs.configForm.clearValidate()
      })
    },
    handleEdit(row) {
      this.dialogTitle = '编辑程序配置'
      this.isEdit = true
      this.editAID = row.AID
      this.configForm = {
        物料编码: row.物料编码 || '',
        物料名称: row.物料名称 || '',
        相机: row.相机 || '',
        机器人: row.机器人 || '',
        加工中心: row.加工中心 || '',
        清洗机: row.清洗机 || '',
        打标机: row.打标机 || '',
        打标模板: row.打标模板 || ''
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.configForm && this.$refs.configForm.clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该程序配置吗？', '提示', { type: 'warning' })
        .then(() => {
          const param = [['AID', row.AID]]
          const Data = this.CreateData('11', '零件图号_程序配置_删除', param)
          this.ExecDatabase(Data).then(res => {
            const result = res.data && res.data[0] ? res.data[0] : {}
            if (result.result === 1) {
              this.$message.success(result.msg || '删除成功')
              this.searchData()
            } else {
              this.$message.error(result.msg || '删除失败')
            }
          })
        })
        .catch(() => {})
    },
    submitConfig() {
      this.$refs.configForm.validate(valid => {
        if (!valid) return
        const proc = this.isEdit ? '零件图号_程序配置_修改' : '零件图号_程序配置_新增'
        const param = [
          ['物料编码', this.configForm.物料编码],
          ['物料名称', this.configForm.物料名称],
          ['相机', this.configForm.相机],
          ['机器人', this.configForm.机器人],
          ['加工中心', this.configForm.加工中心],
          ['清洗机', this.configForm.清洗机],
          ['打标机', this.configForm.打标机],
          ['打标模板', this.configForm.打标模板]
        ]
        if (this.isEdit) param.unshift(['AID', this.editAID])
        const Data = this.CreateData('11', proc, param)
        this.ExecDatabase(Data).then(res => {
          const result = res.data && res.data[0] ? res.data[0] : {}
          if (result.result === 1) {
            this.$message.success(result.msg || (this.isEdit ? '修改成功' : '新增成功'))
            this.dialogVisible = false
            this.searchData()
          } else {
            this.$message.error(result.msg || '操作失败')
          }
        })
      })
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchData()
    }
  }
}
</script>

<style scoped>
.topCard {
  margin: 5px;
}
.topDiv {
  margin-bottom: 10px;
}
</style>
