<template>
  <div class="app-container">
    <el-card>
      <el-row style="margin-top: 10px; margin-bottom: 10px">
        <span>机床编号:</span>
        <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="searchTable1()">
          <el-option
            v-for="item in machineNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <div style="float: left">{{ item.label }}</div>
            <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
          </el-option>
        </el-select>
        <el-button size="small" icon="el-icon-circle-plus-outline" type="distribution" style="margin-left: 300px" @click="addTable()">增加装配计划</el-button>
        <el-button type="danger" plain size="small" icon="el-icon-delete" style="margin-left: 300px" @click="deleteData()">删除</el-button>
      </el-row>
    </el-card>
    <el-card>
      <div v-for="(file, key) in hadFile" :key="key">
        <div class="wrapper">
          <div class="content">
            <div class="action"/>
            <div v-if="file.suffix==='pdf'" class="icon"><svg-icon icon-class="pdf" /></div>
            <div v-else-if="file.suffix==='docx'||file.suffix === 'doc'" class="icon"><svg-icon icon-class="doc" /></div>
            <div v-else-if="file.suffix==='xlsx' || file.suffix === 'xls'" class="icon"><svg-icon icon-class="excel" /></div>
            <div v-else-if="file.suffix==='pptx' || file.suffix === 'ppt'" class="icon"><svg-icon icon-class="ppt" /></div>
            <div v-else-if="file.suffix==='jpg' || file.suffix === 'jpeg'|| file.suffix === 'png'" class="icon"><svg-icon icon-class="pic" /></div>
            <div v-else class="icon"><svg-icon icon-class="file" /></div>
            <a :href="file.url" target="view_window"><div class="info">{{ file.name }}</div></a>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="width: 47%; float: left">
      <el-table :data="tableData1" border stripe size="mini" highlight-current-row style="width: 620px;margin-top: 15px" height="720px" @row-click="searchTable2">
        <el-table-column label="项目名称" align="center" width="140px">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床编号" align="center" width="120px">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="机床名称" align="center" width="140px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床数量" align="center" width="90px">
          <template slot-scope="scope">
            {{ scope.row.机床数量 }}
          </template>
        </el-table-column>
        <el-table-column label="计划装配完成" align="center" width="110px">
          <template slot-scope="scope">
            {{ scope.row.机床装配完成时间 ? scope.row.机床装配完成时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card style="width: 51%; float: left">
      <el-table :data="tableData2" :summary-method="getSummaries" border size="mini" highlight-current-row style="width: 525px;margin-top: 15px" height="720px" show-summary @row-dblclick="changeState" @row-click="deleteData1">
        <el-table-column label="组号" align="center" width="120px">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-select v-model="scope.row.组件流水号" filterable clearable size="small" style="width: 100px" @change="groupChange(scope.row, scope.$index)">
                <el-option
                  v-for="item in groupNum"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </template>
            <template v-else>
              {{ scope.row.组号 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="任务名称" align="center" width="160px">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-input v-model="scope.row.任务名称" size="mini" style="width: 100%"/>
            </template>
            <template v-else>
              {{ scope.row.任务名称 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="计划工时(分)" align="center" width="120px" prop="计划工时">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-input v-model="scope.row.计划工时" size="mini" style="width: 100%" props="" @input="filterNuber(scope.row.计划工时, scope.$index, '计划工时')"/>
            </template>
            <template v-else>
              {{ scope.row.计划工时 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120px">
          <template slot-scope="scope">
            <!--<el-tooltip :open-delay="1000" effect="dark" content="编辑" placement="top">-->
            <!--<div style="display: inline-block">-->
            <!--<el-button v-if="scope.row.edit" type="success" plain size="small" @click="confirmEdit(scope.row)">确定</el-button>-->
            <!--<el-button v-else type="text" size="small" icon="el-icon-edit-outline" @click="scope.row.edit=!scope.row.edit"/>-->
            <!--<el-button v-if="scope.row.edit" type="warning" plain size="small" @click="cancel(scope.row)">取消</el-button>-->
            <!--</div>-->
            <!--</el-tooltip>-->
            <el-tooltip :open-delay="1000" effect="dark" content="编辑" placement="top">
              <div style="display: inline-block">
                <el-button v-if="!scope.row.edit" type="text" size="small" icon="el-icon-edit-outline" @click="scope.row.edit=!scope.row.edit"/>
                <el-button v-if="scope.row.edit" type="text" plain size="small" @click="confirmEdit(scope.row)">确定</el-button>
                <el-button v-if="scope.row.edit" type="text" plain size="small" @click="cancel(scope.row)">取消</el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>import { initProject, searchgroup, searchgroupName, getFileData, searchTable1, searchTable2, deletedata, editData, addData } from '@/api/Assembly/MakeAssemblyPlan'
import { ServerIP } from '@/utils/request'
// import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNum: [],
      tableData1: [],
      tableData2: [],
      hadFile: [],
      plan: ''
    }
  },
  created() {
    this.searchTable1()
  },
  mounted() {
    this.initProject()
  },
  methods: {
    // 保留两位小数的正实数
    filterNuber(val, index, date) {
      console.log(index, 6666)
      let value = '' + val
      value = value
        .replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
        .replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
        .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') // 只能输入两个小数
      if (value.indexOf('.') < 0 && value !== '') {
        value = parseFloat(value)
      }
      console.log(value)
      this.tableData2[index][date] = value
    },
    getFileData(machineValue) {
      this.hadFile = []
      getFileData(machineValue).then(res => {
        if (res.data.length > 0) {
          res.data.map(item => {
            this.hadFile.push({
              url: `${ServerIP}${item.文件标识}.${item.文件后缀}`,
              name: `${item.文件名称}.${item.文件后缀}`,
              suffix: item.文件后缀,
              id: item.文件标识
            })
          })
        }
      })
    },
    groupChange(row, index) {
      this.groupNumberValue = ''
      this.tableData2[index].任务名称 = ''
      if (row.组件流水号 !== '' && row.组件流水号 !== null) {
        searchgroupName(row.组件流水号).then(response => {
          console.log(row, 22222)
          this.tableData2[index].任务名称 = response.data[0].组件名称
        })
      }
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        if (index === 2) {
          const values = data.map(item => Number(item['计划工时']))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] += ' 分'
          } else {
            sums[index] = ''
          }
        }
      })
      return sums
    },
    changeState(row) {
      row.edit = !row.edit
    },
    initProject() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    confirmEdit(row) {
      console.log(row, 999)
      let obj = {}
      obj = this.groupNum.find((item) => { // 这里的userList就是上面遍历的数据源
        return item.value === row.组件流水号// 筛选出匹配数据
      })
      editData(row.计划流水号, row.组件流水号, obj.label, row.任务名称, row.计划工时).then(response => {
        if (response.data[0].result === '1') {
          row.edit = false
          this.tableData2 = []
          searchTable2(this.machineNumberValue, 1).then(response => {
            for (let i = 0; i < response.data.length; i++) {
              this.tableData2.push({
                计划流水号: response.data[i].计划流水号,
                组件流水号: response.data[i].组件流水号,
                组号: response.data[i].组号,
                任务名称: response.data[i].任务名称,
                计划工时: Number(response.data[i].计划工时).toFixed(2),
                edit: false
              })
            }
          })
          this.$message.success('编辑成功')
        } else {
          this.$message.error('编辑失败')
        }
      })
    },
    cancel(row) {
      row.edit = !row.edit
    },
    deleteData1(row) {
      this.plan = row.计划流水号
    },
    deleteData() {
      this.deleteRow(deletedata, this.plan, function() {
        this.tableData2 = []
        searchTable2(this.machineNumberValue, 1).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.tableData2.push({
              计划流水号: response.data[i].计划流水号,
              组件流水号: response.data[i].组件流水号,
              组号: response.data[i].组号,
              任务名称: response.data[i].任务名称,
              计划工时: Number(response.data[i].计划工时).toFixed(2),
              edit: false
            })
          }
        })
      })
    },
    addTable() {
      console.log(this.machineNumberValue, 888)
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) {
        addData(this.machineNumberValue, 1).then(response => {
          console.log(response)
          if (response.data[0].result === '1') {
            this.tableData2 = []
            searchTable2(this.machineNumberValue, 1).then(response => {
              for (let i = 0; i < response.data.length; i++) {
                this.tableData2.push({
                  计划流水号: response.data[i].计划流水号,
                  组件流水号: response.data[i].组件流水号,
                  组号: response.data[i].组号,
                  任务名称: response.data[i].任务名称,
                  计划工时: Number(response.data[i].计划工时).toFixed(2),
                  edit: false
                })
              }
            })
            this.$message.success('增加成功')
          } else {
            this.$message.error('增加失败')
          }
        })
      } else {
        this.$message.warning('请选择机床')
      }
    },
    searchTable1() {
      this.tableData1 = []
      this.tableData2 = []
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check2 = 1 } else { this.check2 = 0 }
      searchTable1(this.machineNumberValue, this.check2).then(response => {
        this.tableData1 = response.data
      })
    },
    searchTable2(row) {
      this.tableData2 = []
      this.machineNumberValue = row.机床流水号
      searchTable2(this.machineNumberValue, 1).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.tableData2.push({
            计划流水号: response.data[i].计划流水号,
            组件流水号: response.data[i].组件流水号,
            组号: response.data[i].组号,
            任务名称: response.data[i].任务名称,
            计划工时: Number(response.data[i].计划工时).toFixed(2),
            edit: false
          })
        }
      })
      this.groupNum = []
      searchgroup(this.machineNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groupNum.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
      this.getFileData(this.machineNumberValue)
    }
  }
}

</script>

<style scoped>

</style>

