<template>
  <el-form ref="form" :model="form">
    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :datasource="form.datasource"
      class="time-form"
      max-height="300"
      :toolkit="[]"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <div class="headbox">
          <div>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="handlAdd"
              v-if="type!='view'"
            >
              新增
            </el-button>
          </div>
          <div class="pricebox">
            <span class="amount">总计：{{ allPrice }}元</span>
          </div>
        </div>
      </template>

      <template v-slot:categoryLevelPathName="scope" v-if="type!='view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.categoryLevelPathName'"
          :rules="{
            required: true,
            message: '请选择',
            trigger: 'change'
          }"
        >
          <el-input
            v-model="scope.row.categoryLevelPathName"
            disabled
            style="width: 75%"
          ></el-input>
          <el-button
            size="small"
            type="primary"
            @click.native="handParent(scope.$index)"
            >选择
          </el-button>
        </el-form-item>
      </template>
      <template v-slot:totalCount="scope" v-if="type!='view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.totalCount'"
          :rules="{
            required: true,
            message: '请输入数量',
            trigger: 'change'
          }"
        >
          <el-input
            v-model="scope.row.totalCount"
            type="number"
            placeholder="请输入"
            @input="changeCount(scope.row, scope.$index)"
          ></el-input>
        </el-form-item>
      </template>

      <template v-slot:totalPrice="scope" v-if="type!='view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.totalPrice'"
        >
          {{ (Number(scope.row.totalPrice) || 0).toFixed(2) }}元
        </el-form-item>
      </template>

      <template v-slot:singlePrice="scope" v-if="type!='view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.singlePrice'"
        >
          <el-input
            v-model="scope.row.singlePrice"
            placeholder="请输入"
            type="number"
            @input="changeCount(scope.row, scope.$index)"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </template>
      <template v-slot:headerTotalCount="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:measuringUnit="scope" v-if="type!='view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.measuringUnit'"
        >
          <DictSelection
            dictName="计量单位"
            clearable
            v-model="scope.row.measuringUnit"
          >
          </DictSelection>
        </el-form-item>
      </template>

      <!-- 操作列 -->
      <template v-slot:action="scope" v-if="type!='view'">
        <el-popconfirm
          v-if="!isContractId"
          class="ele-action"
          title="确定要删除吗？"
          @confirm="remove(scope.$index)"
        >
          <template v-slot:reference>
            <el-link type="danger" :underline="false" icon="el-icon-delete">
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>
    <CategoryDialog ref="CategoryDialogRef" @chooseCategory="chooseCategory">
    </CategoryDialog>
  </el-form>
</template>
<script>
  import dictMixins from '@/mixins/dictMixins';
  import CategoryDialog from '@/BIZComponents/CategoryDialog.vue';
  export default {
    mixins: [dictMixins],
    components: { CategoryDialog },
    props: {
      type:{
        default:''
      }
    },
    data() {
      const defaultForm = {
        measuringUnit: '立方'
      };
      return {
        allPrice: 0,
        defaultForm,
        form: {
          datasource: []
        }
      };
    },
    
    computed: {
      columns() {
        return [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },

          {
            prop: 'categoryLevelPathName',
            label: '类型',
            slot: 'categoryLevelPathName',
            align: 'center'
          },

          {
            width: 200,
            prop: 'totalCount',
            label: '数量',
            slot: 'totalCount',
            headerSlot: 'headerTotalCount',
            align: 'center'
          },
          {
            width: 160,
            prop: 'sendTotelCount',
            label: '发货数量',
            align: 'center',
            show:this.type=='view'
          },
          {
            width: 160,
            prop: 'returnTotelCount',
            label: '退货数量',
            align: 'center',
            show:this.type=='view'

          },
          // {
          //   width: 200,
          //   prop: 'totalCount',
          //   label: '受托数量',
          //   slot: 'totalCount',
          //   headerSlot: 'headerTotalCount',
          //   align: 'center'
          // },

          {
            width: 150,
            prop: 'measuringUnit',
            label: '单位',
            slot: 'measuringUnit',
            align: 'center'
          },
          {
            width: 200,
            prop: 'singlePrice',
            label: '单价',
            slot: 'singlePrice',
            headerSlot: 'headerSinglePrice',
            align: 'center'
          },

          {
            width: 180,
            prop: 'totalPrice',
            label: '合计',
            slot: 'totalPrice',
            align: 'center'
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 120,
            align: 'center',
            resizable: false,
            slot: 'action',
            fixed: 'right',
            showOverflowTooltip: true
          }
        ];
      }
    },

    methods: {
      // 返回列表数据
      getTableValue() {
        let comitDatasource = this.form.datasource;
        if (comitDatasource.length === 0) return [];

        return comitDatasource;
      },
      getPrice() {
        return [this.allPrice, this.allPrice];
      },
      //改变数量
      changeCount() {
        this.allPrice = 0;
        let totalCount = 0;
        this.form.datasource.forEach((item, index) => {
          if (item.totalCount && item.singlePrice) {
            this.$set(
              this.form.datasource[index],
              'totalPrice',
              item.totalCount * item.singlePrice
            );
          } else {
            this.$set(this.form.datasource[index], 'totalPrice', 0);
          }
          if (item.totalCount) {
            totalCount += Number(item.totalCount);
          }

          this.allPrice += item.totalPrice;
        });
        this.$emit('setCountAmount', this.allPrice);
        this.$emit('setTotalPlanNum', totalCount);
      },

      //修改回显
      async putTableValue(productList) {
        if (productList) {
          this.form.datasource = productList;
          this.$refs.table.reload();
        }
      },
      chooseCategory([current, title, PathInfo, ruleCode]) {
        this.$set(
          this.form.datasource[this.currentIndex],
          'categoryLevelId',
          current.id
        );
        this.$set(
          this.form.datasource[this.currentIndex],
          'categoryLevelName',
          current.name
        );
        this.$set(
          this.form.datasource[this.currentIndex],
          'categoryLevelPathName',
          PathInfo.categoryLevelPath
        );
        this.$set(
          this.form.datasource[this.currentIndex],
          'categoryLevelRootId',
          current.parentId
        );
      },

      //选择产品
      handParent(index) {
        this.currentIndex = index;
        this.$refs.CategoryDialogRef.open();
      },
      remove(index) {
        this.form.datasource.splice(index, 1);
        this.changeCount();
      },

      // 添加
      handlAdd() {
        let item = JSON.parse(JSON.stringify(this.defaultForm));
        this.form.datasource.push(item);
      },

      validateForm(callback) {
        //开始表单校验
        this.$refs.form.validate((valid, obj) => {
          if (obj) {
            let messages = Object.keys(obj).map((key) => obj[key][0]);
            if (messages.length > 0) {
              this.$message.warning(messages[0].message);
            }
          }
          callback(valid);
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .headbox {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .amount {
      font-size: 14px;
      font-weight: bold;
      padding-right: 30px;
    }
  }

  .time-form .el-form-item {
    margin-bottom: 0 !important;
  }

  ::v-deep .period {
    display: flex;

    .borderleftnone {
      .el-input--medium .el-input__inner {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }

    .borderrightnone {
      .el-input--medium .el-input__inner {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
      }
    }
  }

  ::v-deep .time-form tbody > tr:hover > td {
    background-color: transparent !important;
  }

  ::v-deep .time-form .el-table tr {
    background-color: #ffffff;
  }

  .pricebox {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-weight: bold;
  }
</style>
