<template>
  <div class="app-container">
    <el-card>
      <span class="show-text">工位名称:</span>
      <el-input
        v-model="RoleFunctioning"
        clearable
        placeholder="请输入工位名称"
        style="width: 280px;margin-right: 26px"/>
      <el-button icon="el-icon-search" plain type="primary" @click="searchData()">查询</el-button>
      <el-button
        icon="el-icon-circle-plus-outline"
        plain
        style="margin-left: 14px"
        type="success"
        @click="handleAdd('form')">
        新增
      </el-button>
      <div style="margin-top: 10px">
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="694"
          highlight-current-row
          size="mini"
          stripe
          style="width: 642px"
          tooltip-effect="dark">
          <el-table-column align="center" label=" " type="index" width="100"/>
          <el-table-column align="center" label="工位名称" width="100">
            <template slot-scope="scope">
              {{ scope.row.指南工位号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="工位号" width="100">
            <template slot-scope="scope">
              {{ scope.row.工位号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="编辑" width="200">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                style="font-family: MiSans Regular,serif;color: #4d77d6"
                type="text"
                @click="handleEdit(scope.row)">编辑
              </el-button>
              <el-button
                icon="el-icon-delete"
                style="font-family: MiSans Regular,serif;color: #c61a1a"
                type="text"
                @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" center title="新增工位" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="100px">
        <el-form-item label="工位名称" prop="工位名称" style="display: inline-block">
          <el-input
            v-model="form.工位名称"
            clearable
            placeholder="工位名称"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="工位号" prop="工位号" style="display: inline-block">
          <el-input
            v-model="form.工位号"
            clearable
            placeholder="工位号"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd1('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="dialogFormVisible1 = false">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" center title="编辑工位" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="100px">
        <el-form-item label="工位名称" prop="工位名称" style="display: inline-block">
          <el-input
            v-model="form2.工位名称"
            clearable
            placeholder="工位名称"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd2('form2')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="dialogFormVisible2 = false">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { postB1 } from '@/api/b1s';
import { getB1 } from '@/api/b1s';
import { patchB1 } from '@/api/b1s';
export default {
  data() {
    return {
      RoleFunctioning: '',
      loading: false,
      tableData: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      name: '',
      num: '', // 工位号
      form: {
        工位名称: ''
      },
      form2: {
        工位名称: '',
        num: '' // 工位号
      },
      rules: {
        工位名称: [{ required: true, message: '请输入工位名称', trigger: 'blur' }],
        工位号: [{ required: true, message: '请输入工位名称', trigger: 'blur' }]
      },
      rules2: {
        工位名称: [{ required: true, message: '请输入工位名称', trigger: 'blur' }]
      },
    }
  },
  created() {
    this.searchData()
    var now = new Date();
    var start = new Date(
      Date.UTC(now.getFullYear() , now.getMonth(), now.getDate())
    )
      .toISOString()
      .replace("T", " ")
      .replace(".000Z", "");
    var end = new Date(
      Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59)
    )
      .toISOString()
      .replace("T", " ")
      .replace(".000Z", "");
    this.completionTime= [start, end];
  },
  methods: {
    // 查询
    searchData() {
      this.loading = true
      this.tableData = []
      var param = []
      var Data = this.CreateData('11', 'MES_登录_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
        this.loading = false
      })
    },
    // 编辑工位
    handleEdit(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.form2.工位名称 = row.工位名称
      this.form2.工位号 = row.工位号
      this.dialogFormVisible2 = true
    },
    // 编辑工位功能
    submitAdd2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位名称', this.form2.工位名称.toString()]
          param[1] = ['工位号', this.form2.工位号.toString()]
          var Data = this.CreateData('11', '系统管理_工位管理_工位信息修改', param)
          this.ExecDatabase(Data).then( async response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })

              this.$message.success('编辑成功')
              patchB1("/Resources('"+this.form2.工位号.toString()+"')",{
                  "Name": this.form2.工位名称.toString()
              })
              this.searchData()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('存在同名工位，信息编辑失败')
            }
          })
        }
      })
    },

    // 增加工位
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible1 = true
    },
    // 确认增加工位
    submitAdd1(formName) { // 增加工位功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位名称', this.form.工位名称.toString()]
          param[1] = ['工位号', this.form.工位号.toString()]
          var Data = this.CreateData('11', '系统管理_工位管理_工位信息增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })
              postB1("/Resources",{
                  "VisCode": this.form.工位号,
                  "Name": this.form.工位名称,
                  "ForeignName": '',
                  "Type": "rtMachine",
                  "Group": 1,
                  "UnitOfMeasure": '',
                  "IssueMethod": "rimManual",
                  "Active": "tYES",
                  "Inactive": "tNO",
                  "Remarks": '',
                  "Allocation": "raOnStartDate"
              })
              this.searchData()
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('存在同名工位，信息增加失败')
            }
          })
        }
      })
    },
    // 删除工位
    handleDelete(row) {
      this.$confirm('当前工位将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['工位号', row.工位号]
        var Data = this.CreateData('11', '系统管理_工位管理_工位信息删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === 1) {
            this.$message.success('删除成功')
          // } else if (response.data[0].result === 0) {
          //   this.$message.error('删除失败,当前工位已经分配了菜单！')
          // } else if (response.data[0].result === 2) {
          //   this.$message.error('删除失败,当前工位已经分配了人员！')
          // } else {
          //   this.$message.error('删除失败')
          }
          this.searchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
<style scoped>
.el-card {
  margin: 0px;
}
</style>
