<template>
  <div>

    <el-card>
      <el-row>
        <el-radio-group v-model="radio" style="margin: 10px;" @change="radioChange">
          <el-radio :label="1">未编制</el-radio>
          <el-radio :label="2">已编制</el-radio>
        </el-radio-group>
        <span>项目名称:</span>
        <el-select v-model="entryNameValue" clearable filterable size="small" style="margin-bottom:10px" placeholder="项目名称" @change="typeChange()">
          <el-option
            v-for="item in entryName"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>机床名称:</span>
        <el-select v-model="toolNumValue" clearable filterable size="small" placeholder="机床号" style="margin-bottom:10px" @change="typeChange1()">
          <el-option
            v-for="item in toolNum"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>分组:</span>
        <el-select v-model="NumValue" clearable filterable size="small" style="margin-bottom:10px" placeholder="机床组号">
          <el-option
            v-for="item in Num"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>零件图号:</span>
        <el-input v-model="partName" placeholder="零件图号" size="small" clearable style="width:200px"/>

        <el-button size="small" icon="el-icon-search" type="success" style="margin-left: 10px" @click="pageSize2=10; pageCurrent2=1;searchPartNum()">查询</el-button>
      </el-row>

    </el-card>

    <el-card>
      <el-table v-loading="loading2" :data="tableData2" highlight-current-row height="370" style="width: 100%;" border element-loading-text="拼命加载中" @row-click="searchTable">
        <el-table-column
          label=" "
          type="index"
          align="center"
          width="50"/>
        <el-table-column label="零件图号" align="center" width="230px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" width="230px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="材料" align="center" width="220px">
          <template slot-scope="scope">
            {{ scope.row.材料 }}
          </template>
        </el-table-column>
        <el-table-column label="种类" align="center" width="180px">
          <template slot-scope="scope">
            {{ scope.row.零件类型名称 }}
          </template>
        </el-table-column>
        <el-table-column label="投产数量" align="center" width="180px">
          <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="面积" width="110" align="center">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.面积 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column label="重量" width="110" align="center">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.重量 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column label="材料费" width="110" align="center">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.材料费 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column label="操作" align="center" width="150px" fixed="right">-->
        <!--<template slot-scope="scope">-->
        <!--<el-button-->
        <!--size="mini"-->
        <!--type="primary"-->
        <!--class="el-icon-edit"-->
        <!--@click="edit(scope.$index, scope.row, 'form')">编辑</el-button>-->
        <!--</template>-->
        <!--</el-table-column>-->
      </el-table>
      <div class="block">
        <el-pagination
          v-if="!loading2"
          :current-page="pageCurrent2"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize2"
          :total="total2"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"/>
      </div>
    </el-card>

    <el-card>
      <el-table v-loading="loading0" :data="tableData" :summary-method="getSummaries" border style="width: 100%;max-height: 390px;" height="340px" stripe show-summary>
        <el-table-column label="序号" width="80" align="center" type="index"/>
        <el-table-column label="工艺名称" width="200" align="center">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].工艺名称" size="mini" placeholder="工艺名称" readonly/>
          </template>
        </el-table-column>
        <el-table-column label="工艺要求" align="center">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].工艺要求" size="mini" readonly type="textarea"/>
          </template>
        </el-table-column>
        <el-table-column label="单件" width="110" align="center">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].单件定额工时_系数" size="mini" readonly/>
          </template>
        </el-table-column>
        <el-table-column label="准结" width="110" align="center">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].准结定额工时" size="mini" readonly/>
          </template>
        </el-table-column>
        <el-table-column label="外协预算价格" width="110" align="center">
          <template slot-scope="scope">
            <el-input v-model="tableData[scope.$index].外协预算价格_计算" size="mini" />
          </template>
        </el-table-column>
      </el-table>
      <el-button size="small" type="primary" style="float:right;margin: 10px" @click="saveRowPrice">保存</el-button>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible" title="编辑" 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-form-item label="面积" prop="面积" style="margin-left: 200px">
          <el-input v-model="form.面积" placeholder="面积" size="small" style="width:200px"/>
        </el-form-item>
        <el-form-item label="重量" prop="重量" style="margin-left: 200px">
          <el-input v-model="form.重量" placeholder="重量" size="small" style="width:200px"/>
        </el-form-item>
        <el-form-item label="材料费" prop="材料费" style="margin-left: 200px">
          <el-input v-model="form.材料费" placeholder="材料费" size="small" style="width:200px"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { machine, getToolNum, getNum, searchPartNum1, searchTable, saveWeightArea, saveMaterialPrice, saveRowPrice } from '@/api/Outsourcing/InterBudgetaryBudgets'
export default {
  data() {
    return {
      entryNameValue: '',
      entryName: [], // 项目名称
      toolNumValue: '',
      toolNum: [], // 机床号
      NumValue: '',
      Num: [], // 分组
      lingjianhao: '',
      dialogFormVisible: false,
      form: {
        面积: '',
        重量: '',
        材料费: ''
      },
      rules: {
      },
      cailiaofei: '',
      result: [],
      result2: [],
      radio: 1,
      partName: '', // 零件名称
      loading0: false,
      tableData: [],
      loading2: false,
      tableData2: [], // 表格数据
      total2: null, // 总条数
      pageSize2: 10, // 每页显示条数
      pageCurrent2: 1 // 后台获取页
    }
  },
  created() {
    this.fetchData()
    this.searchPartNum()
  },
  methods: {
    radioChange() {
      this.tableData2 = []
      this.entryNameValue = ''
      this.toolNumValue = ''
      this.NumValue = ''
    },
    fetchData() {
      this.entryNameValue = ''
      this.entryName = []
      machine().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.entryName.push({
            label: response.data[i].项目名称,
            value: response.data[i].项目流水号
          })
        }
      })
    },
    typeChange() {
      this.toolNumValue = ''
      this.toolNum = []
      this.NumValue = ''
      this.Num = []
      getToolNum(this.entryNameValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.toolNum.push({
            label: response.data[i].机床图号,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    typeChange1() {
      this.NumValue = ''
      this.Num = []
      getNum(this.toolNumValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Num.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    // 查零件号
    searchPartNum() {
      if (this.partName === null) {
        this.partName = ''
      }
      if (this.entryNameValue !== null && this.entryNameValue !== '') {
        this.check_entryNameValue = 1
      } else {
        this.check_entryNameValue = 0
      }
      if (this.toolNumValue !== null && this.toolNumValue !== '') {
        this.check_toolNumValue = 1
      } else {
        this.check_toolNumValue = 0
      }
      if (this.NumValue !== null && this.NumValue !== '') {
        this.check_NumValue = 1
      } else {
        this.check_NumValue = 0
      }
      if (this.partName !== null && this.partName !== '') {
        this.check_partName = 1
      } else {
        this.check_partName = 0
      }
      this.loading2 = true
      this.tableData2 = []
      this.tableData = []
      if (this.radio === 1) {
        searchPartNum1(this.check_entryNameValue, this.entryNameValue, this.check_toolNumValue, this.toolNumValue, this.check_NumValue, this.NumValue, this.check_partName, this.partName, 0, this.pageCurrent2, this.pageSize2).then(response => {
          this.tableData2 = response.data.rows
          this.total2 = response.data.total
        }).then(() => {
          this.loading2 = false
        })
      } else {
        searchPartNum1(this.check_entryNameValue, this.entryNameValue, this.check_toolNumValue, this.toolNumValue, this.check_NumValue, this.NumValue, this.check_partName, this.partName, 1, this.pageCurrent2, this.pageSize2).then(response => {
          this.tableData2 = response.data.rows
          this.total2 = response.data.total
        }).then(() => {
          this.loading2 = false
        })
      }
    },
    // 合计
    getSummaries(param) {
      const { columns, data } = param
      const sums = ['', '', '', '', '', 0]
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[1] = '统计总价:'
          return
        } else if (index === 5) {
          for (let i = 0; i < data.length; i++) {
            sums[5] += parseFloat(data[i].外协预算价格_计算)
          }
          sums[5] = parseInt(sums[5] * 100) / 100
        }
      })
      return sums
    },
    searchTable(row) {
      this.loading0 = true
      this.tableData = []
      searchTable(row.工艺计划流水号, 1).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data[i].外协预算价格_计算 !== 0) {
            response.data[i].外协预算价格_计算 = response.data[i].外协预算价格_计算.toFixed(2)
          }
        }
        this.cailiaofei = row.材料费
        this.tableData = response.data
        this.loading0 = false
      })
    },
    // 打开编辑
    edit(index, row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.面积 = row.面积
      this.form.重量 = row.重量
      this.form.材料费 = row.材料费
      this.lingjianhao = row.工艺计划流水号
      this.dialogFormVisible = true
    },
    // 编辑确定
    submit(formName) {
      this.WithdrawingEdit()
      this.dialogFormVisible = false
    },
    // 编辑
    WithdrawingEdit() {
      if ((this.form.重量 === '' || this.form.重量 === null) && (this.form.面积 === '' || this.form.面积 === null)) {
        this.form.重量 = null
        this.form.面积 = null
      }
      saveWeightArea(this.lingjianhao, this.form.重量, this.form.面积).then(response => {
        this.WithdrawingEdit2()
      })
    },
    WithdrawingEdit2() {
      if (this.form.材料费 === '' || this.form.材料费 === null) {
        this.form.材料费 = 0
      }
      saveMaterialPrice(this.lingjianhao, this.form.材料费).then(response => {
        this.$message.success('保存成功')
        this.searchPartNum()
      })
    },
    // 关闭扣款编辑
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 保存行价格
    saveRowPrice() {
      if (this.tableData.length === 0) {
        this.$message.error('请选择零件')
      } else {
        for (let i = 0; i < this.tableData.length; i++) {
          saveRowPrice(this.tableData[i].工序流水号, parseFloat(this.tableData[i].外协预算价格_计算), this.tableData[i].工艺计划流水号, this.cailiaofei).then(response => {
          })
        }
        this.$message.success('保存成功')
        this.searchPartNum()
      }
    },
    handleSizeChange2(val) {
      this.pageCurrent2 = 1
      this.pageSize2 = val
      this.searchPartNum()
    },
    handleCurrentChange2(val) {
      this.pageCurrent2 = val
      this.searchPartNum()
    }
  }
}
</script>

<style scoped>
  span{
    margin: 10px 0 10px 10px;
  }
  .el-date-editor{
    width:150px
  }
  .el-card{
    margin-bottom: 15px;
  }
</style>
