<template>
  <div class="mod-config">
    <el-form :inline="true"  label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'点检项目编码'">
        <el-input v-model="searchData.itemNo"  style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'点检项目名称'">
        <el-input v-model="searchData.itemDesc"  style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item  :label="' '">
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button  type="primary" @click="addModal()">新增</el-button>
        <!--        <el-button  type="danger" @click="deleteEamPropertiesItem()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"

      style="width: 100%;">
      <!--      <el-table-column-->
      <!--        type="selection"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        width="50">-->
      <!--      </el-table-column>-->
      <el-table-column
        v-for="(item,index) in columnList" :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">
          <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>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="160"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="updateModal(scope.row)">修改</a>
          <a type="text" size="small" v-if="scope.row.valueChooseFlag=='Y'" @click="chooseModal(scope.row)">可选值</a>
          <a type="text" size="small" @click="deleteEamPropertiesItem(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="点检项目" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="430px">
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="点检项目编码:">
          <el-input v-model="modalData.itemNo" :disabled="modalDisableFlag" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="点检项目名称:">
          <el-input v-model="modalData.itemDesc"  style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'检测值类型'">
          <el-select filterable v-model="modalData.valueTypeDb" style="width: 120px">
            <el-option label="文本" value="T"></el-option>
            <el-option label="数字" value="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="点检方法说明:">
          <el-input v-model="modalData.itemRemark"  style="width: 254px"></el-input>
        </el-form-item>
        <el-form-item :label="'是否值可选'">
          <el-select filterable v-model="modalData.valueChooseFlag" style="width: 120px">
            <el-option label="是" value="Y"></el-option>
            <el-option label="否" value="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="参照值:">
          <el-input v-model="modalData.defaultValue"  style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="最大值:">
          <el-input v-model="modalData.maxValue"  style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="最小值:">
          <el-input v-model="modalData.minValue"  style="width: 120px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="saveData()">保存</el-button>
        <el-button type="primary" @click="modalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
    <el-dialog title="可选值" :close-on-click-modal="false" v-drag :visible.sync="chooseModalFlag" width="430px">
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="点检项目名称:">
          <el-input v-model="chooseModalData.itemDesc"  disabled style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'检测值类型'">
          <el-select filterable v-model="chooseModalData.valueTypeDb" disabled style="width: 120px">
            <el-option label="文本" value="T"></el-option>
            <el-option label="数字" value="N"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="' '">
          <el-button  type="primary" @click="addChooseModal()">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :height="300"
        :data="chooseTableData"
        border
        style="width: 100%">
        <el-table-column
          prop="availableValue"
          header-align="center"
          align="left"
          min-width="200"
          label="属性值">
        </el-table-column>
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          min-width="100"
          label="操作">
          <template slot-scope="scope">
            <a type="text" size="small" @click="updateChooseModal(scope.row)">修改</a>
            <a type="text" size="small" @click="deleteItemAvailable(scope.row)">删除</a>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog title="可选值维护" :close-on-click-modal="false" v-drag :visible.sync="itemModalFlag" width="215px" >
      <el-form :inline="true" label-position="top" style="margin-left: 22px;margin-top: -5px;" @submit.native.prevent>
        <el-form-item :label="'可选值：'">
          <el-input v-model="itemData.availableValue"  :type="itemFlag" style="width: 130px;"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="saveItemAvailable()">保存</el-button>
        <el-button type="primary" @click="itemModalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>
  import {
    eamPropertiesItemSearch,
    eamPropertiesItemSave,
    eamPropertiesItemEdit,
    eamPropertiesItemDelete,
    searchItemAvailable,
    saveItemAvailable,
    deleteItemAvailable,
  } from "@/api/eam/eam.js"
  export default {

    data () {
      return {

        searchData: {
          itemNo: '',
          itemDesc: '',
          itemType:'A',
        },
        height: 200,

        dataList: [],
        chooseTableData:[],
        dataListLoading: false,
        dataListSelections: [],
        modalFlag:false,
        modalDisableFlag:false,
        chooseModalFlag:false,
        itemModalFlag:false,
        itemFlag:'text',
        itemData:{
          itemNo:'',
          valueNo:'',
          availableValue:'',
          itemType:'A',
          createdBy: this.$store.state.user.name
        },
        modalData:{
          flag:'',
          itemNo:'',
          itemDesc:'',
          valueTypeDb:'',
          defaultValue:'',
          valueChooseFlag:'',
          createdBy:this.$store.state.user.name,
          maxValue:'',
          minValue:'',
          itemRemark:'',
          itemType:'A',
        },
        departmentList:[],
        // 展示列集
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableEamPropertiesItemID',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'itemNo',
            headerAlign: "center",
            align: "left",
            columnLabel: '点检项目编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableEamPropertiesItemDesc',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'itemDesc',
            headerAlign: "center",
            align: "left",
            columnLabel: '点检项目名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 140,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableEamPropertiesItemRemark',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'itemRemark',
            headerAlign: "center",
            align: "left",
            columnLabel: '点检方法说明',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 140,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableValueType',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'valueType',
            headerAlign: "center",
            align: "left",
            columnLabel: '检测值类型',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableDefaultValue',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'defaultValue',
            headerAlign: "center",
            align: "left",
            columnLabel: '参照值',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableMaxValue',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'maxValue',
            headerAlign: "center",
            align: "left",
            columnLabel: '最大值',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101008,
            serialNumber: '101008TableMinValue',
            tableId: "101008Table",
            tableName: "common",
            columnProp: 'minValue',
            headerAlign: "center",
            align: "left",
            columnLabel: '最小值',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
        ],
        levelList:[],
        chooseModalData:{
          itemNo: '',
          itemDesc: '',
          itemType:'A',
          valueTypeDb:'',
        },
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 180
      })
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        eamPropertiesItemSearch(this.searchData).then(({data}) => {
            this.dataList = data.rows
        })
      },

      addModal(){
        this.modalData={
          flag:'1',
          itemNo:'',
          itemDesc:'',
          valueTypeDb:'T',
          defaultValue:'',
          valueChooseFlag:'N',
          createdBy:this.$store.state.user.name,
          maxValue:'',
          minValue:'',
          itemRemark:'',
          itemType:'A',

        };
        this.modalDisableFlag=false;
        this.modalFlag=true;
      },
      chooseModal(row){
        this.chooseModalData.itemNo=row.itemNo;
        this.chooseModalData.itemDesc=row.itemDesc;
        this.chooseModalData.valueTypeDb=row.valueTypeDb;
        searchItemAvailable(this.chooseModalData).then(({data}) => {
          this.chooseTableData = data.rows
        })
        this.chooseModalFlag=true;
      },
      updateModal(row){
        this.modalData={
          flag:'2',
          itemNo:row.itemNo,
          itemDesc:row.itemDesc,
          valueTypeDb:row.valueTypeDb,
          defaultValue:row.defaultValue,
          valueChooseFlag:row.valueChooseFlag,
          createdBy:this.$store.state.user.name,
          maxValue:row.maxValue,
          minValue:row.minValue,
          itemRemark:row.itemRemark,
          itemType:'A',

        };
        this.modalDisableFlag=true;
        this.modalFlag=true;
      },
      // 删除
      deleteEamPropertiesItem (row) {
        this.$confirm(`是否删除这个点检项目?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'

        }).then(() => {
          eamPropertiesItemDelete(row).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },
      saveData(){
        if (this.modalData.itemNo == '' || this.modalData.itemNo == null) {
          this.$alert('请输入点检项目编码！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if (this.modalData.itemDesc == '' || this.modalData.itemDesc == null) {
          this.$alert('请输入点检项目名称！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }

        if(this.modalData.flag=='1'){
          eamPropertiesItemSave(this.modalData).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.modalFlag=false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }else {
          eamPropertiesItemEdit(this.modalData).then(({data}) => {
            if (data && data.code === '0') {
              this.getDataList()
              this.modalFlag=false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }
      },
      addChooseModal(){
        this.itemFlag= this.chooseModalData.valueTypeDb=='T'?'text':'number';
        this.itemData={
            itemNo:this.chooseModalData.itemNo,
            valueNo:0,
            availableValue:'',
            itemType:'A',
            createdBy: this.$store.state.user.name
          }
        this.itemModalFlag=true;
      },
      updateChooseModal(row){
        this.itemFlag= this.chooseModalData.valueTypeDb=='T'?'text':'number';
        this.itemData={
          itemNo:this.chooseModalData.itemNo,
          valueNo:row.valueNo,
          availableValue:row.availableValue,
          itemType:'A',
          createdBy: row.createdBy
        }
        this.itemModalFlag=true;
      },
      saveItemAvailable(){
        if(this.itemData.availableValue==''||this.itemData.availableValue==null){
          this.$alert("请输入可选值！",'错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        saveItemAvailable(this.itemData).then(({data}) => {
          if (data && data.code === 0) {
            searchItemAvailable(this.chooseModalData).then(({data}) => {
              this.chooseTableData = data.rows
            })
            this.itemModalFlag=false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      deleteItemAvailable(row){
        this.$confirm(`是否删除这个可选值?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'

        }).then(() => {
          deleteItemAvailable(row).then(({data}) => {
            if (data && data.code === 0) {
              searchItemAvailable(this.chooseModalData).then(({data}) => {
                this.chooseTableData = data.rows
              })
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },
    }
  }
</script>
