<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <div class="cmms-dialog-item-title">基本信息</div>
        <a-row>
          <!-- <a-col :span="12">
            <a-form-model-item label="点检内容编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contentcode">
              <a-input v-model="model.contentcode" placeholder="请输入所属点检内容编号"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentid">
              <j-search-select-tag v-model="model.equipmentid" dict="tpm_equipment,equipmentname,id" placeholder="请选择设备" @change="handleChange" :disabled="disabledEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="点检内容名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contentname">
              <a-input v-model="model.contentname" placeholder="请输入所属点检内容名称" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="所属点检内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spotcheckcontid">
              <j-search-select-tag v-model="model.spotcheckcontid" dict="cmms_spotcheck_content,contentname,id"  />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="点检结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="result">
              <j-dict-select-tag type="list" v-model="model.result" dictCode="spotcheck_result" placeholder="请选择点检结果" @change="resultChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="resultStatus == -1? '故障日期' : '点检时间'" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spotcheckdate">
              <j-date :placeholder="resultStatus == -1? '请选择故障日期':'请选择点检时间'"  v-model="model.spotcheckdate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="resultStatus == -1? '故障描述' : '点检描述'" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <!-- <a-input v-model="model.description" placeholder="请输入点检描述"  ></a-input> -->
              <a-textarea v-model="model.description" :placeholder="resultStatus == -1?'请输入故障描述':'请输入点检描述'" :auto-size="{ minRows: 2, maxRows: 3 }" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="点检备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <!-- <a-input v-model="model.remark" placeholder="请输入点检备注"  ></a-input> -->
              <a-textarea v-model="model.remark" placeholder="请输入点检备注" :auto-size="{ minRows: 2, maxRows: 3 }" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="resultStatus == -1">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repairname">
              <span slot="label">
                工单名称&nbsp;
                <a-tooltip title="点检结果为报修，需新增维修工单">
                  <a-icon type="exclamation-circle" theme="filled" />
                </a-tooltip>
              </span>
              <a-input v-model="model.repairname" placeholder="请输入维修工单名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="resultStatus == -1">
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priority">
              <span slot="label">
                优先级&nbsp;
                <a-tooltip title="点检结果为报修，需新增维修工单">
                  <a-icon type="exclamation-circle" theme="filled" />
                </a-tooltip>
              </span>
              <j-dict-select-tag  v-model="model.priority"  placeholder="请选择维修优先级" dictCode="repair_priority"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="点检人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="故障照片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <j-image-upload text="上传" v-model="model.files" isMultiple></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
        <div class="cmms-dialog-item-title u-flex-jab">
          <div>点检列表</div>
          <div>
            <a-button @click="allT" type="link">一键正常</a-button>
            <a-button @click="allF" type="link">一键异常</a-button>
          </div>
        </div>
        <a-table
          ref="table"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="itemcode"
          :columns="columns"
          :dataSource="model.cmmsSpotcheckTitemList"
          :pagination="false"
          class="j-table-force-nowrap">

          <template slot="iresult" slot-scope="text, record,index">
              <a-form-model-item :prop="'cmmsSpotcheckTitemList.'+index+'.iresult'" :rules="validatorRules.iresult" style="margin-bottom: 0px;">
                  <a-radio-group name="radioGroup" v-model="record.iresult">
                    <a-radio value="0">
                      正常
                    </a-radio>
                    <a-radio value="1">
                      异常
                    </a-radio>
                  </a-radio-group>
              </a-form-model-item>
          </template>
          <template slot="iremark" slot-scope="text, record,index">
              <a-form-model-item :prop="'cmmsSpotcheckTitemList.'+index+'.iremark'" :rules="validatorRules.iremark" style="margin-bottom: 0px;">
                  <a-input v-model="record.iremark" placeholder="请输入点检备注"></a-input>
              </a-form-model-item>
          </template>
        </a-table>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SpotcheckForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      // dataList: {
      //   type: Object,
      //   default: function(){
      //       return {} // 使用工厂函数返回默认值
      //   }
      // },
    },
    data () {
      return {
        model:{
          cmmsSpotcheckTitemList: []
        },
        modelWX:{
        },
        // 单个禁填
        disabledEdit: false,
        // 点检结果(-1为报修，1为其他结果)
        resultStatus: 1,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        columns: [
          // {
          //   title: '序号',
          //   dataIndex: '',
          //   key:'rowIndex',
          //   width:60,
          //   align:"center",
          //   customRender:function (t,r,index) {
          //     return parseInt(index)+1;
          //   }
          // },
          {
            title:'点检项编号',
            align:"center",
            dataIndex: 'itemcode'
          },
          {
            title:'点检项名称',
            align:"center",
            dataIndex: 'itemname'
          },
          {
            title:'标准',
            align:"center",
            dataIndex: 'conditions',
          },
          {
            title:'点检结果',
            align:"center",
            dataIndex: 'iresult',
            scopedSlots: { customRender: 'iresult' }
          },
          {
            title:'点检备注',
            align:"center",
            dataIndex: 'iremark',
            scopedSlots: { customRender: 'iremark' }
          }
        ],
        confirmLoading: false,
        validatorRules: {
          contentname: [
            { required: true, message: '请输入点检内容名称!'},
          ],
          equipmentid: [
            { required: true, message: '请选择设备!'},
          ],
          result: [
            { required: true, message: '请输入点检结果!'},
          ],
          description: [
            { required: true, message: '请输入点检描述/故障描述!'},
          ],
          spotcheckdate: [
            { required: true, message: '请选择点检时间!'},
          ],
          repairname: [
            { required: true, message: '请输入工单名称!'},
          ],
          priority: [
            { required: true, message: '请选择优先级!'},
          ],
        },
        url: {
          add: "/cmmsSpotcheck/cmmsSpotcheck/add",
          add2: "/cmmsRepair/cmmsRepair/add",
          edit: "/cmmsSpotcheck/cmmsSpotcheck/edit",
          queryById: "/cmmsSpotcheck/cmmsSpotcheck/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
        this.disabledEdit = false
        // 第二版
        // this.model = this.dataList
        // this.model.id = ""
      },
      // 监听选择的设备
      handleChange(value){
        getAction(`/cmmsSpotcheck/cmmsSpotcheck/getInfoByEquipmentid/${value}`).then((res) => {
          console.log(999,res)
          // this.model = res.result
          this.model.equipmentcode = res.result.equipmentcode
          this.model.equipmentname = res.result.equipmentname
          this.model.contentname = res.result.contentname
          this.model.contentcode = res.result.contentcode
          this.model.spotcheckdate = res.result.spotcheckdate
          this.model.cmmsSpotcheckTitemList = res.result.cmmsSpotcheckTitemList
          this.model.id = ""
        })
      },
      edit (record) {
        this.disabledEdit = true
        getAction(`/cmmsSpotcheck/cmmsSpotcheck/getInfoBySpotcheckid/${record.id}`).then((res) => {
            this.model = res.result
            const b = res.result.files.map((res) => {
              return res.url
            })
            this.model.files = b.join(',')
            this.visible = true;
        })
        // this.model = Object.assign({}, record);
        // this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let httpurl2 = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              // 判断点检结果是否为报修
              if (this.resultStatus == -1) {
                console.log(99,this.model)
                this.modelWX.equipmentid = this.model.equipmentid
                // 下面两句：将带有时分秒的去掉，只传年月日
                // const time = this.model.spotcheckdate
                // this.modelWX.faultdate=time.replace(/ \d+(:\d+){2}/,'');
                this.modelWX.equipmentcode = this.model.equipmentcode
                this.modelWX.equipmentname = this.model.equipmentname
                this.modelWX.faultdate = this.model.spotcheckdate
                this.modelWX.faultdesc = this.model.description
                this.modelWX.repairname = this.model.repairname
                this.modelWX.priority = this.model.priority
                const c  = this.model.files.split(',')
                this.modelWX.files = c.map((res) => {
                  return {
                    url: res
                  }
                })
                console.log("00",this.modelWX)
                httpurl2+=this.url.add2;
                httpAction(httpurl2,this.modelWX,method).then((res)=>{
                  if(res.success){
                    that.$message.success("维修工单新增成功！");
                  }else{
                    that.$message.warning(res.message);
                  }
                })
              }
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if (this.model.files.length != 0) {
              const a  = this.model.files.split(',')
              this.model.files = a.map((res) => {
                return {
                  url: res
                }
              })
            }else{
              this.model.files = []
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      allT() {
        this.model.cmmsSpotcheckTitemList = this.model.cmmsSpotcheckTitemList.map((res) => {
          var data = res
          data.iresult = "0"
          return data
        })
      },
      allF() {
        this.model.cmmsSpotcheckTitemList = this.model.cmmsSpotcheckTitemList.map((res) => {
          var data = res
          data.iresult = "1"
          return data
        })
      },
      // 监听点检结果
      resultChange(value) {
        if (value == 2) {
          this.resultStatus = -1
        }else{
          this.resultStatus = 1
        }
      }
    }
  }
</script>

<style scoped>
@import "~@/assets/less/uStyle.less";
</style>