<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-icon_personalcenter"
        @breadcrumb-click="handleClose"
      />
    </div>
    <div class="form-content">
      <el-form
        ref="formInfo"
        :model="parameter"
        label-width="auto"
        label-position="right"
        :rules="rules"
      >
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">基本信息</div>
          </div>
        </div>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="编制公司" prop="preparedOrgName">
              <el-input
                clearable
                v-model="parameter.preparedOrgName"
                placeholder="请输入编制公司"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="模板名称" prop="name">
              <el-input
                clearable
                v-model="parameter.name"
                placeholder="请输入模板名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目类型" prop="targetType">
              <dict-select
                dictCode="bdlx"
                :hasDefaultOption="true"
                clearable
                @select="changeTargetType"
                style="width: 100%"
                v-model="parameter.targetType"
                placeholder="请选择项目类型"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="付款方式" prop="quotationMethod">
              <dict-select
                dictCode="cglx-fkfs"
                :hasDefaultOption="true"
                clearable
                style="width: 100%"
                v-model="parameter.quotationMethod"
                placeholder="请选择付款方式"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否含税" prop="isTax">
              <dict-select
                dictCode="cglx-jjfs"
                :hasDefaultOption="true"
                clearable
                style="width: 100%"
                @select="changeIsTax"
                v-model="parameter.isTax"
                placeholder="请选择是否含税"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="模板说明" prop="remarks">
              <el-input
                clearable
                v-model="parameter.remarks"
                type="textarea"
                placeholder="请输入模板说明"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">明细信息</div>
          </div>
        </div>
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="addTable"
          >添加</el-button
        >
        <span style="color: red; margin-left: 10px; font-weight: bold"
          >注：投标补充说明模板自带，无需添加</span
        >
        <el-table
          ref="singleTable"
          :data="parameter.detailList"
          style="width: 100%"
          highlight-current-row
          :cell-style="{'text-align': 'center'}"
          :header-cell-style="{'text-align': 'center'}"
        >
          <el-table-column fixed type="index" width="55" label="序号" />
          <el-table-column label="报价列名称" min-width="170" prop="quoArrange">
            <template slot-scope="scope">
              <el-input
                clearable
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                v-model="scope.row.quoArrange"
                placeholder="报价列名称"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="是否必填" min-width="170" prop="isRequired">
            <template slot-scope="scope">
              <el-checkbox
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                v-model="scope.row.isRequired"
              ></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column
            label="招标录入项"
            min-width="170"
            prop="beckonEntry"
          >
            <template slot-scope="scope">
              <el-checkbox
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                v-model="scope.row.beckonEntry"
              ></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column label="投标录入项" min-width="150" prop="throwEntry">
            <template slot-scope="scope">
              <el-checkbox
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                v-model="scope.row.throwEntry"
              ></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column label="投标显示项" min-width="150" prop="throwShow">
            <template slot-scope="scope">
              <el-checkbox
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                v-model="scope.row.throwShow"
              ></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column
            label="评标显示项"
            min-width="180"
            prop="procurementMethod"
          >
            <template slot-scope="scope">
              <el-checkbox
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                v-model="scope.row.evaluationShow"
              ></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="right"
            width="90px"
            :index="999"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                circle
                :disabled="
                  scope.row.quoArrange == '含税单价(元)' ||
                  scope.row.quoArrange == '不含税单价(元)' ||
                  scope.row.quoArrange == '合计金额(元)'
                "
                icon="el-icon-close"
                @click="delTable(scope.$index, scope.row)"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <div slot="footer" class="footer">
      <el-button @click="handleClose">{{ $t('domestic.cancel') }}</el-button>
      <el-button type="primary" :loading="saveing" @click="handleSave">{{
        $t('domestic.save')
      }}</el-button>
    </div>
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import procurementManagement from '@/api/procurementManagement'
import dictUtils from '@/components/dict/DictSelectUtils.js'
export default {
  name: 'quoteTemplateAddDialog',
  components: {Breadcrumb},
  props: {
    editData: Object,
  },
  data() {
    return {
      breadcrumbOpt: [{breadcrumbName: '报价模板'}, {breadcrumbName: '新增'}],
      saveing: false,
      methodUnitsList: [],
      rules: {
        preparedOrgName: [
          {
            required: true,
            message: '请输入编制公司',
            trigger: 'blur',
          },
        ],

        name: [
          {
            required: true,
            message: '请输入模板名称',
            trigger: 'blur',
          },
        ],
        quotationMethod: [
          {
            required: true,
            message: '请选择报价方式',
            trigger: 'blur',
          },
        ],
        targetType: [
          {
            required: true,
            message: '请选择标的类型',
            trigger: 'blur',
          },
        ],
        isTax: [
          {
            required: true,
            message: '请选择是否含税价',
            trigger: 'blur',
          },
        ],
        isEnable: [
          {
            required: true,
            message: '请选择是否启用',
            trigger: 'blur',
          },
        ],
      },
      parameter: {
        preparedOrgName: '',
        remarks: '',
        quotationMethod: '1',
        targetType: '1',
        detailList: [],
        isTax: '1',
        isEnable: '1',
        name: '',
        preparedOrgId: '',
        preparedOrgCode: '',
      },
    }
  },
  created() {
    this.addModifyBtn()
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.preparedOrgName = this.currentUser.org[0].orgName
    this.parameter.preparedOrgId = this.currentUser.org[0].orgId
    this.parameter.preparedOrgCode = this.currentUser.org[0].orgCode
    let data = dictUtils.getDictItemsFromCache('cglx-jjfs')
    data.forEach((item) => {
      if (item.value == this.parameter.isTax) {
        this.parameter.quoArrange = item.name
      }
    })
    this.parameter.detailList = [
      {
        beckonEntry: '',
        evaluationShow: true,
        isRequired: true,
        priceEntry: '1',
        quoArrange: this.parameter.quoArrange + '(元)',
        sourceType: '',
        throwEntry: true,
        throwShow: true,
      },
      {
        beckonEntry: '',
        evaluationShow: true,
        isRequired: true,
        priceEntry: '2',
        quoArrange: '合计金额(元)',
        sourceType: '',
        throwEntry: false,
        throwShow: true,
      },
    ]
  },
  mounted() {},
  methods: {
    changeIsTax(val) {
      let data = dictUtils.getDictItemsFromCache('cglx-jjfs')
      data.forEach((item) => {
        if (val == item.value) {
          this.parameter.detailList[0].quoArrange = item.name + '(元)'
          this.$forceUpdate()
        }
      })
    },
    changeTargetType(val) {
      if (val == 2) {
        this.parameter.detailList = [
          {
            beckonEntry: '',
            evaluationShow: true,
            isRequired: true,
            priceEntry: '1',
            quoArrange: this.parameter.quoArrange + '(万元)',
            sourceType: '',
            throwEntry: true,
            throwShow: true,
          },
          {
            beckonEntry: '',
            evaluationShow: true,
            isRequired: true,
            priceEntry: '2',
            quoArrange: '合计金额(万元)',
            sourceType: '',
            throwEntry: false,
            throwShow: true,
          },
        ]
      }
      if (val == 1) {
        this.parameter.detailList = [
          {
            beckonEntry: '',
            evaluationShow: true,
            isRequired: true,
            priceEntry: '1',
            quoArrange: this.parameter.quoArrange + '(元)',
            sourceType: '',
            throwEntry: true,
            throwShow: true,
          },
          {
            beckonEntry: '',
            evaluationShow: true,
            isRequired: true,
            priceEntry: '2',
            quoArrange: '合计金额(元)',
            sourceType: '',
            throwEntry: false,
            throwShow: true,
          },
        ]
      }
    },
    //列表添加行
    addTable() {
      if (this.parameter.detailList == undefined) {
        this.parameter.detailList = []
        this.parameter.detailList.push({
          beckonEntry: '',
          evaluationShow: '',
          isRequired: '',
          priceEntry: '',
          quoArrange: '',
          sourceType: '',
          throwEntry: '',
          throwShow: '',
          key: new Date(),
        })
      } else {
        this.parameter.detailList.push({
          beckonEntry: '',
          evaluationShow: '',
          isRequired: '',
          priceEntry: '',
          quoArrange: '',
          sourceType: '',
          throwEntry: '',
          throwShow: '',
          key: new Date(),
        })
      }
      this.$forceUpdate()
    },
    //列表删除行
    delTable(index, row) {
      console.log(row)
      if (row.id != undefined) {
        this.parameter.detailList.splice(index, 1)
        this.$http
          .remove('${portal}/quotationTemplateDetail/v1/' + row.id)
          .then((res) => {
            if (res.data.state) {
              this.$message.success(res.data.message)
            }
          })
      } else {
        this.parameter.detailList.splice(index, 1)
      }
    },
    //编辑
    addModifyBtn() {
      if (this.$route.query.editRows) {
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
        this.modifychoData(this.$route.query.editRows)
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    //获取修改回显数据
    modifychoData(val) {
      this.$http
        .post('${portal}/quotationTemplate/v1/dataById?id=' + val.id)
        .then((res) => {
          this.parameter = res.data.value
        })
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'quoteTemplate',
        })
      } else {
        this.$router.push({
          name: 'quoteTemplate',
        })
      }
      this.parameter = {}
    },
    // 保存
    handleSave() {
      console.log(this.parameter, 'this.parameter')
      this.parameter.detailList.forEach((item) => {
        if (item.id) {
          delete item.id
        }
      })
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          this.$http
            .post(
              '${portal}/quotationTemplate/v1/saveOrUpdateData',
              this.parameter
            )
            .then((res) => {
              if (res.data.state) {
                this.$message.success(res.data.message)
                if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
                  this.$router.push({
                    name: 'quoteTemplate',
                    params: {
                      pageBean: this.$route.query.pageBean,
                    },
                  })
                } else {
                  this.$router.push({
                    name: 'quoteTemplate',
                  })
                }
              }
              this.saveing = false
            })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
// /deep/.el-row {
//   padding: 0 100px;
// }
.rows {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebebeb;
  margin-bottom: 16px;
  padding: 10px 0;
  .rows-left {
    margin-right: 10px;
    .rows-hr {
      width: 3px;
      height: 18px;
      background-color: #0e6eff;
      border-radius: 2px;
    }
  }
  .rows-title {
    .text-overflow {
      width: 100%;
      font-size: 16px;
    }
  }
}
</style>
