<template>
  <div>
    <el-form>
      <el-form-item label="项目概况：" label-width="90px">
        <table border="1">
          <tr class="head">
            <td style="width: 320px">工作内容</td>
            <td style="width:120px;">等级或比例尺</td>
            <td style="width:90px;">单位</td>
            <td style="width:90px;">工作量</td>
            <td style="width: 100px">要求完成时间</td>
            <td style="width: 320px;">备注</td>
          </tr>
          <tr v-for="(work, index) in workList" :key="index">
            <td>
              {{ work.content }}
            </td>
            <td>
              {{ work.scale }}
            </td>
            <td>
              {{ work.unit }}
            </td>
            <td>
              {{ work.workload }}
            </td>
            <td>
              {{ work.deadline }}
            </td>
            <td>
              {{ work.remark }}
            </td>
          </tr>
        </table>
      </el-form-item>
      <el-form-item label="预结算：" label-width="90px">
        <table border="1">
          <tr class="head">
            <td style="width: 250px">工作简述</td>
            <td>工作类别</td>
            <td>工作项目</td>
            <!-- <td>项目细项</td> -->
            <td>比例尺/等级</td>
            <td>地类类型</td>
            <td style="width:90px;">单价</td>
            <td>单位</td>
            <td style="width:90px;">数量</td>
            <td style="width:70px;">系数</td>
            <td style="min-width:70px;">总额</td>
            <td style="min-width:70px;">备注</td>
          </tr>
          <tr v-for="(work, index) in contentList" :key="index">
            <td style="position:relative;">
              <div class="delete-btn" @click="deletWorkItem(index)">
                <i class="el-icon-circle-close"></i>
              </div>
              <el-input type="textarea" v-model="work.content"></el-input>
            </td>
            <td>
              <el-select v-model="work.workType" :style="{ width: '100%' }"
                @change="getWorkItemList(work, work.workType)">
                <el-option v-for="item in workTypeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </td>
            <td>
              <el-select v-model="work.workItem" :style="{ width: '100%' }" :remote="true"
                @change="getScaleGradeList(work, work.workItem)">
                <el-option v-for="item in work.workSelect.workItemList" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </td>
            <td>
              <el-select v-model="work.scaleGrade" :style="{ width: '100%' }"
                @change="getUnitPrice(work, work.workItem, work.subItem, work.scaleGrade, work.groundType)">
                <el-option v-for="item in work.workSelect.scaleGradeList" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </td>
            <td>
              <el-select v-model="work.groundType" :style="{ width: '100%' }"
                @change="getUnitPrice(work, work.workItem, work.subItem, work.scaleGrade, work.groundType)">
                <el-option key="一般地类" label="一般地类" value="0">
                </el-option>
                <el-option key="复杂地类" label="复杂地类" value="1">
                </el-option>
              </el-select>
            </td>
            <td>
              {{ work.price }}
            </td>
            <td>
              {{ work.unit }}
            </td>
            <td>
              <el-input v-model="work.workload" @change="getTotal(work)"></el-input>
            </td>
            <td>
              <el-input-number v-model="work.coefficient" :controls="false" :precision="2" :step="0.01" :max="10"
                @blur="getTotal(work)" style="width:60px;">
              </el-input-number>
              <el-button type="text" @click="getCoefficientRemark(work.workType)">系数备注</el-button>
            </td>
            <td>
              <!-- <el-input v-model="work.settle"></el-input> -->
              {{ work.settle }}
            </td>
            <td>
              <el-input type="textarea" v-model="work.remark"></el-input>
            </td>
          </tr>
          <tr>
            <td colspan="9">直接成本小计</td>
            <td>{{ Number(settle).toFixed(2) }}</td>
          </tr>
        </table>
        <el-button icon="el-icon-plus" plain type="primary" size="mini" @click="addWorkList"></el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getWorkTypeList, getWorkItemList, getSubItemList, getScaleGradeList, getUnitPrice } from '@/api/oa/price/price'
import { deepClone } from '@/utils'
import { getPriceRemarkByWorkType } from '@/api/oa/price/price'
export default {
  dicts: ['cmc_scale_grade', 'cmc_unit'],
  props: {
    workList: {
      type: Array,
      require: true
    },
    chooseUser: {
      type: Array,
      require: true
    },
    budgetForm: {
      type: Object,
      require: true
    },
    settleList: {
      type: Array
    }
  },
  data() {
    return {
      workTypeList: [],
      workSelect: {
        // 项目列表
        projectList: [],
        // 工作类别列表
        workTypeList: [],
        // 工作项目
        workItemList: [],
        // 项目细项
        subItemList: [],
        // 比例尺
        scaleGradeList: [],
        workTypeLoading: true,
        workItemLoading: true,
        subItemLoading: true,
        scaleGradeLoading: true,
      },
      contentList: [
        {
          content: '',
          workType: '',
          workItem: '',
          scaleGrade: '',
          groundType: '0',
          price: '',
          unit: '',
          workload: '',
          coefficient: 1,
          settle: 0,
          workSelect: {
            // 项目列表
            projectList: [],
            // 工作类别列表
            workTypeList: [],
            // 工作项目
            workItemList: [],
            // 项目细项
            subItemList: [],
            // 比例尺
            scaleGradeList: [],
            workTypeLoading: true,
            workItemLoading: true,
            subItemLoading: true,
            scaleGradeLoading: true,
          }
        }
      ],
      settle: 0,
      checkUserList: []
    }
  },
  created() {
    this.getWorkTypeList();
  },
  watch: {
    settleList(val) {
      this.initContenList();
    }
  },
  mounted() {
    this.getSettleSum();
  },
  methods: {
    initContenList() {
      if (this.settleList.length != 0) {
        this.contentList = this.settleList;
        for (let c of this.contentList) {
          this.$set(c, 'workSelect', {
            // 项目列表
            projectList: [],
            // 工作类别列表
            workTypeList: [],
            // 工作项目
            workItemList: [],
            // 项目细项
            subItemList: [],
            // 比例尺
            scaleGradeList: [],
            workTypeLoading: true,
            workItemLoading: true,
            subItemLoading: true,
            scaleGradeLoading: true,
          })
          this.$set(c, 'workType', c.cmcPrice.workType)
          this.$set(c, 'scaleGrade', c.cmcPrice.scaleGrade)
          this.$set(c, 'workItem', c.cmcPrice.workItem)
          this.$set(c, 'unit', c.cmcPrice.unit)
          if (c.groundType == '0') {
            this.$set(c, 'price', c.cmcPrice.commonPrice)
          }else{
            this.$set(c, 'price', c.cmcPrice.complexPrice)
          }
          this.getTotal(c)
        }
      }
    },
    addWorkList() {
      this.contentList.push({
        content: '',
        workType: '',
        workItem: '',
        scaleGrade: '',
        groundType: '0',
        price: '',
        unit: '',
        workload: '',
        coefficient: 1,
        settle: 0,
        workSelect: {
          // 项目列表
          projectList: [],
          // 工作类别列表
          workTypeList: [],
          // 工作项目
          workItemList: [],
          // 项目细项
          subItemList: [],
          // 比例尺
          scaleGradeList: [],
          workTypeLoading: true,
          workItemLoading: true,
          subItemLoading: true,
          scaleGradeLoading: true,
        }
      });
    },
    deletWorkItem(index) {
      let arr = this.contentList;
      if (arr.length == 1) {
        return;
      }
      if (index >= 0 && index < arr.length) {
        arr.splice(index, 1);
      }
      this.getSettleSum();
    },
    /* 获取工作类别 */
    getWorkTypeList() {
      getWorkTypeList().then(res => {
        if (res) {
          this.workTypeList = this.setArray(res);
        }
      });
    },
    /* 获取工作项目 */
    getWorkItemList(work, workType) {
      getWorkItemList({ workType: workType }).then(res => {
        if (res) {
          let workItemList = this.setArray(res);
          work.workSelect.workItemLoading = false;
          work.workSelect.workItemList = workItemList;
          work.workItem = workItemList[0].label;
          // this.getSubItemList(work, work.workItem);
          work.subItem = '';
          this.getScaleGradeList(work, work.workItem);
        }
      })
    },
    /* 获取比例尺 */
    getScaleGradeList(work, workItem) {
      if (work.scale) {
        this.$set(work, 'scaleGrade', work.scale);
        if (work.groundType == undefined) {
          this.$set(work, 'groundType', '0');
        }
        this.getUnitPrice(work, work.workItem, work.subItem, work.scaleGrade, work.groundType);
      } else {
        getScaleGradeList({ workItem }).then(res => {
          if (res) {
            let scaleGradeList = this.setArray(res);
            work.workSelect.scaleGradeLoading = false;
            work.workSelect.scaleGradeList = scaleGradeList;
            this.$set(work, 'scaleGrade', scaleGradeList[0].label)
            // work.scaleGrade = scaleGradeList[0].label;
            if (work.scaleGrade == undefined) {
              this.$set(work, 'scaleGrade', '无');
            }
            if (work.groundType == undefined) {
              this.$set(work, 'groundType', '0');
            }
            this.getUnitPrice(work, work.workItem, work.subItem, work.scaleGrade, work.groundType);
          }
        })
      }
    },
    /* 获取单价 */
    getUnitPrice(work, workItem, subItem, scaleGrade, groundType) {
      getUnitPrice({ workItem, subItem, scaleGrade, groundType: groundType }).then(res => {
        if (res.length != 0) {
          work.priceId = res.data.id;
          this.$set(work, 'price', res.data.price)
          this.$set(work, 'unit', res.data.unit)
          this.$set(work, 'coefficient', 1)
          if (work.workload != undefined || work.workload != '') {

            this.getTotal(work);
          }
        }
      })
    },
    /* 获取总额 */
    getTotal(work) {
      let total = this.calculateTotal(work.price, work.coefficient, work.workload, 1);
      if (total < 600) {
        total = 600
        this.$message.warning('不足600，按600算')
      }
      this.$set(work, 'settle', total)
      this.getSettleSum();
    },
    getSettleSum() {
      let sum = 0;
      for (let work of this.contentList) {
        sum = sum + Number(work.settle)
      }
      this.settle = sum.toFixed(2);
      let obj = {}
      obj.workList = this.contentList;
      obj.settle = this.settle;
      this.$emit('getSettle', obj);
      this.$emit('getWorkList', this.contentList)
    },
    /* 计算总额 */
    calculateTotal(price, coefficient, workload, percent) {
      return ((parseFloat(price) * parseFloat(coefficient) * parseFloat(workload)) * percent).toFixed(2);
    },
    getCoefficientRemark(workType) {
      getPriceRemarkByWorkType({ workType }).then(res => {
        if (res.code == 200) {
          if (workType != undefined)
            this.$notify({
              title: workType,
              dangerouslyUseHTMLString: true,
              message: res.msg,
              duration: 0
            });
        }

      })
    },
    // 数组去重，并格式化为label、value形式
    setArray(arr) {
      if (arr != [] && arr != undefined) {
        let datalist = [...new Set(arr)];
        let list = [];
        for (let i of datalist) {
          let obj = new Object();
          obj.label = i;
          obj.value = i;
          list.push(obj);
        }
        return list;
      } else {
        return [];
      }
    },
  },
}
</script>

<style lang="scss" scoped>
table {
  /*居中*/
  // margin: 0 auto;
  /*边框*/
  /* border: 1px solid black; */
  width: 100%;
  text-align: center;
  border-collapse: collapse;

  /*设置背景颜色*/
  /* background-color: #bfa; */
  td {
    padding: 3px;
  }
}

.head {
  font-weight: bold;
}

.delete-btn {
  position: absolute;
  left: -30px;
  top: 50%;
  transform: translate(0, -50%);
  color: #F56C6C;
  cursor: pointer;

  i {
    font-size: 25px;
  }
}
</style>