<script>
import {
  getPriceCheckPropertiesList,
  removePriceCheckPropertiesItem,
  savePriceCheckPropertiesItem,
  searchPriceCheckPropertiesItemList,
  searchPriceCheckPropertiesList,
  updatePriceCheckPropertiesList,
} from "../../../api/quotation/priceCheckProperties";
import TransferTable from "../common/transferTable.vue";

export default {
  name: "priceCheckProperties",
  components: {TransferTable},
  props:{
    dataList:{
      type: Array,
      default: ()=>[],
    },
    quotationNo:{
      type:String,
    },
    columnList:{
      type: Array,
      default: ()=>[],
    },
    height:{
      type:[Number,String],
      default:300
    },
    disabled:{
      type:Boolean,
      default:false
    }
  },
  model:{
    prop:"dataList",
    event:"change"
  },
  data(){
    return{
      attributeSaveDialog:false,
      loading:false,
      attributeDialog:true,
      copyAttributeList:[],
      productColumnList: [
        {
          userId: this.$store.state.user.name,
          functionId: 100002001,
          serialNumber: '100002001TableItemNo',
          tableId: "100002001Table",
          tableName: "属性表",
          columnProp: 'itemNo',
          headerAlign: "center",
          align: "left",
          columnLabel: '属性编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },{
          userId: this.$store.state.user.name,
          functionId: 100002001,
          serialNumber: '100002001TableItemDesc',
          tableId: "100002001Table",
          tableName: "属性表",
          columnProp: 'itemDesc',
          headerAlign: "center",
          align: "left",
          columnLabel: '属性名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          userId: this.$store.state.user.name,
          functionId: 100002001,
          serialNumber: '100002001TableValueType',
          tableId: "100002001Table",
          tableName: "属性表",
          columnProp: 'valueType',
          headerAlign: "center",
          align: "center",
          columnLabel: '值类型',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth:80,
        },
        // {
        //   userId: this.$store.state.user.name,
        //   functionId: 100002001,
        //   serialNumber: '100002001TableMinValue',
        //   tableId: "100002001Table",
        //   tableName: "属性表",
        //   columnProp: 'minValue',
        //   headerAlign: "center",
        //   align: "center",
        //   columnLabel: '最小值',
        //   columnHidden: false,
        //   columnImage: false,
        //   columnSortable: false,
        //   sortLv: 0,
        //   status: true,
        //   fixed: '',
        //   columnWidth:80,
        // },{
        //   userId: this.$store.state.user.name,
        //   functionId: 100002001,
        //   serialNumber: '100002001TableMaxValue',
        //   tableId: "100002001Table",
        //   tableName: "属性表",
        //   columnProp: 'maxValue',
        //   headerAlign: "center",
        //   align: "center",
        //   columnLabel: '最大值',
        //   columnHidden: false,
        //   columnImage: false,
        //   columnSortable: false,
        //   sortLv: 0,
        //   status: true,
        //   fixed: '',
        //   columnWidth:80,
        // },
        {
          userId: this.$store.state.user.name,
          functionId: 100002001,
          serialNumber: '100002001TableMaxValue',
          tableId: "100002001Table",
          tableName: "属性表",
          columnProp: 'textValue',
          headerAlign: "center",
          align: "left",
          columnLabel: '属性值',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth:120,
        },
        // {
        //   userId: this.$store.state.user.name,
        //   functionId: 100002001,
        //   serialNumber: '100002001TableMinValue',
        //   tableId: "100002001Table",
        //   tableName: "属性表",
        //   columnProp: 'numValue',
        //   headerAlign: "center",
        //   align: "right",
        //   columnLabel: '数字值',
        //   columnHidden: false,
        //   columnImage: false,
        //   columnSortable: false,
        //   sortLv: 0,
        //   status: true,
        //   fixed: '',
        //   columnWidth:120,
        // },
        // {
        //   userId: this.$store.state.user.name,
        //   functionId: 100002001,
        //   serialNumber: '100002001TableDefaultValue',
        //   tableId: "100002001Table",
        //   tableName: "属性表",
        //   columnProp: 'defaultValue',
        //   headerAlign: "center",
        //   align: "right",
        //   columnLabel: '参照值',
        //   columnHidden: false,
        //   columnImage: false,
        //   columnSortable: false,
        //   sortLv: 0,
        //   status: true,
        //   fixed: '',
        // },
      ],
      searchDataList:[],
    }
  },
  methods:{
    savePriceCheckPropertiesItem(params){
      let i = 0;
      let arr = params.searchTableList.map(item=>{
        item.quotationNo = this.quotationNo;
        item.seqNo = i++;
        return item
      })
      savePriceCheckPropertiesItem(arr).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg);
          this.getPriceCheckProperties(params);
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    removePriceCheckPropertiesItem(params){
      removePriceCheckPropertiesItem(params.dataTableList).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg);
          this.getPriceCheckProperties(params);
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    searchPriceCheckPropertiesItemList(value){
      let params= JSON.parse(JSON.stringify(value))
      params.site=this.$store.state.user.site;
      params.list = this.dataList;
      if (!this.attributeSaveDialog){
        this.searchDataList = [];
      }
      searchPriceCheckPropertiesItemList(params).then(({data})=>{
        if (data && data.code === 0){
          this.searchDataList = data.rows;
          this.attributeSaveDialog = true;
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    getPriceCheckProperties(searchData){
      let params = {
        site:this.$store.state.user.site,
        quotationNo: this.quotationNo
      }
      getPriceCheckPropertiesList(params).then(({data})=>{
        if (data && data.code === 0){
          this.$emit("change",data.rows)
          if (searchData){
            this.$nextTick(()=>{
              this.searchPriceCheckPropertiesItemList(searchData);
            })
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    updatePriceCheckPropertiesList(){
      updatePriceCheckPropertiesList(this.copyAttributeList).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg);
          this.$emit("change",this.copyAttributeList)
          this.getPriceCheckProperties();
          this.attributeDialog = true;
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    searchPriceCheckPropertiesList(){
      let params = {
        site:this.$store.state.user.site,
        quotationNo: this.quotationNo
      }
      this.loading = true
      searchPriceCheckPropertiesList(params).then(({data})=>{
        if (data && data.code === 0){
          this.getPriceCheckProperties();
        }else {
          this.$message.warning(data.msg)
        }
        this.loading = false
      }).catch((error)=>{
        this.loading = false
        this.$message.error(error)
      })
    },
    saveClick(){
      if (this.attributeDialog){
        this.copyAttributeList = JSON.parse(JSON.stringify(this.dataList))
        this.attributeDialog = false;
      }else {
        this.updatePriceCheckPropertiesList();
      }
    },
    saveBtnClick(){
      if (!this.attributeDialog) {
        this.$message.warning('请保存更改！')
        return
      }
      this.searchPriceCheckPropertiesItemList({})
    },
  },
}
</script>

<template>
  <div>
    <div style="margin-top: 5px">
      <el-button type="primary" :loading="loading" v-if="quotationNo && !disabled" @click="saveBtnClick">新增</el-button>
      <el-button type="primary" @click="saveClick" v-if="quotationNo && !disabled">{{ attributeDialog?'编辑':'保存' }}</el-button>
    </div>
    <div class="rq" v-if="attributeDialog">
      <el-table
        :height="height"
        :data="dataList"
        border
        style="width: 100%;margin-top: 5px">
        <el-table-column
          v-for="(item,index) in productColumnList" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed===''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
          <template slot-scope="scope">
            <div v-if="item.columnProp !== 'textValue' && item.columnProp !== 'numValue'">
              <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
            </div>
            <div v-else>
              {{scope.row.textValue?scope.row.textValue:scope.row.numValue}}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="rq" v-else>
      <el-table
        :height="height"
        :data="copyAttributeList"
        border
        style="width: 100%;margin-top: 5px">
        <el-table-column
          v-for="(item,index) in productColumnList" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed===''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
          <template slot-scope="scope">
            <div v-if="item.columnProp !== 'textValue' && item.columnProp !== 'numValue'">
              <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
            </div>
            <div v-else>
              <div v-if="scope.row.valueChooseFlag !== 'Y'">
                <el-input-number v-model="scope.row.numValue" style="padding: 0;width: 100%" v-if="scope.row.valueType === '数字'" :controls="false"></el-input-number>
                <el-input v-model="scope.row.textValue" v-else></el-input>
              </div>
              <div v-else>
                <el-select style="width: 100%;" v-if="scope.row.valueType === '文本'" v-model="scope.row.textValue">
                  <el-option :value="i.availableValue" :label="i.availableValue" v-for="(i,key) in scope.row.list" :key="key"></el-option>
                </el-select>
                <el-select style="width: 100%;" v-else v-model="scope.row.numValue">
                  <el-option :value="i.availableValue" :label="i.availableValue" v-for="(i,key) in scope.row.list" :key="key"></el-option>
                </el-select>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="属性项目" v-drag :visible.sync="attributeSaveDialog" append-to-body>
      <transfer-table v-if="attributeSaveDialog"
                      v-model="dataList"
                      :data-list="searchDataList"
                      @add="savePriceCheckPropertiesItem"
                      @delete="removePriceCheckPropertiesItem"
                      @search="searchPriceCheckPropertiesItemList"></transfer-table>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="attributeSaveDialog = false">关闭</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<style scoped>
.el-input-number /deep/ .el-input__inner {
  text-align: right;
  padding-right: 5px !important;
}

/deep/ .el-input .el-input--medium{
  line-height: 20px;
}
/deep/ .el-input-number .el-input--medium{
  line-height: 20px;
}
</style>
