<template>
  <el-form ref="form" :model="form">
    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :datasource="form.datasource"
      class="time-form"
      :toolkit="[]"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <div class="headbox">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="handParent()"
            v-if="type != 'view'"
          >
            新增
          </el-button>
        </div>
      </template>
      <template v-slot:unitPrice="scope"  v-if="type != 'view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.unitPrice'"
          :rules="{
            required: true,
            message: '请输入正确的单价',
            trigger: 'change'
            
          }"
        >
          <el-input
            v-model="scope.row.unitPrice"
            placeholder="请输入"
            @input="changeCount(scope.row, scope.$index)"
            type="number"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </template>
      <template v-slot:taxRate="scope"  v-if="type != 'view'">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.taxRate'"
        >
          <el-input
            v-model="scope.row.taxRate"
            placeholder="请输入"
            type="number"
            @input="changeCount(scope.row, scope.$index)"
          >
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
      </template>
      <template v-slot:priceType="scope">
        <el-form-item
          style="margin-bottom: 20px"
          :prop="'datasource.' + scope.$index + '.priceType'"
          :rules="{
            required: true,
            message: '请输入选择价格类型',
            trigger: 'change'
          }"
        >
          <DictSelection
            dictName="商品价格类型"
            clearable
            v-model="scope.row.priceType"
            :listFormatte="listFormatte"
            :disabled="type=='view'"
          >
          </DictSelection>
        </el-form-item>
      </template>

      <template v-slot:headerPriceType="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerTaxRate="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerUnitPrice="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <template v-slot:headerExcludeTaxPrice="{ column }">
        <span class="is-required">{{ column.label }}</span>
      </template>
      <!-- 操作列 -->
      <template v-slot:action="scope" v-if="type != 'view'">
        <el-popconfirm
          class="ele-action"
          title="确定要删除吗？"
          @confirm="remove(scope.$index)"
          :disabled="form.datasource.length <= 1"
        >
          <template v-slot:reference>
            <el-link
              :disabled="form.datasource.length <= 1"
              type="danger"
              :underline="false"
              icon="el-icon-delete"
            >
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>
  </el-form>
</template>
<script>
  import dictMixins from '@/mixins/dictMixins';

  import { getCode } from '@/api/login/index';

  export default {
    components: {},
    mixins: [dictMixins],
    props: { groupId: String, type: String },
    data() {
      return {
        title: '',
        visible: false,
        form: {
          datasource: []
        },
        // 提交状态
        loading: false,
        goodsCode: '' //商品编码
      };
    },
    created() {},

    computed: {
      columns() {
        return [
          {
            width: 80,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left',
            label: '序号'
          },
          {
            width: 200,
            prop: 'priceType',
            label: '价格类型',
            slot: 'priceType',
            headerSlot: 'headerPriceType',
            align: 'center'
          },
          {
            width: 200,
            prop: 'unitPrice',
            headerSlot: 'headerUnitPrice',
            label: '含税单价',
            slot: 'unitPrice',
            align: 'center'
          },
          {
            width: 200,
            prop: 'taxRate',
            slot: 'taxRate',
            label: '税率',
            align: 'center'
          },
          {
            width: 200,
            prop: 'excludeTaxPrice',
            label: '不含税单价',
            slot: 'excludeTaxPrice',
            align: 'center'
          },
          {
            columnKey: 'action',
            label: '操作',
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ];
      }
    },

    methods: {
      //改变单价或税率计算不含税单价
      changeCount(row, index) {
        if (row.unitPrice == null) return;
        let excludeTaxPrice = (
          Number(row.unitPrice) /
          (1 + (Number(row.taxRate) || 0) / 100)
        ).toFixed(2);
        this.form.datasource[index].excludeTaxPrice = excludeTaxPrice || 0;
      },
      // 返回列表数据
      getTableValue() {
        let dList = this.form.datasource;
        if (dList.length === 0) {
          this.$message.error('请新增价格信息！');
          return;
        }
        const priceTypes = dList.map((item) => item.priceType);
        const uniquePriceTypes = new Set(priceTypes);
        if (uniquePriceTypes.size !== priceTypes.length) {
          this.$message.error('价格类型不能重复！');
          return;
        }
        let priceList = dList.map((item) => {
          item['excludeTaxPrice'] = +item.excludeTaxPrice;

          return item;
        });
        return priceList;
      },

      //修改回显
      putTableList(data) {
        // let list =
        //   data.map((i) => {
        //     return { taxRate: i.taxRate * 100, ...i };
        //   }) || [];
        this.form.datasource = data || [];
      },
      listFormatte(data, value) {
        let dictCodeS = this.form.datasource
          .map((item) => item.priceType)
          .filter((item) => item != value);
        return data.filter((item) => !dictCodeS.includes(item.dictCode));
      },

      remove(i) {
        this.form.datasource.splice(i, 1);
        this.setSort();
      },
      // 清空表格
      restTable() {
        debugger;
        this.form.datasource = [];
      },
      // 重新排序
      setSort() {
        this.form.datasource.forEach((n, index) => {
          n.key = index + 1;
        });
      },
      //选择产品
      handParent() {
        let row = {};
        this.$set(row, 'unitPrice', '');
        this.$set(row, 'taxRate', '');
        this.$set(row, 'priceType', '');
        row.key = this.form.datasource.length + 1;
        this.form.datasource.push(row);
      },

      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);
        });
      },

      //查询商品编码
      async getGoodsCode(code) {
        this.goodsCode = await getCode(code);
      }
    }
  };
</script>
<style lang="scss" scoped>
  .headbox {
    display: flex;
    justify-content: flex-start;
    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>
